1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-29 12:55:21 +01:00

[SG-657] Use jest mock extended for unit test service mocks (#3690)

* Use jest mock extended for unit test service mocks

* Replace substitute with mock

* Fix merge issues
This commit is contained in:
Robyn MacCallum 2022-10-14 09:21:36 -04:00 committed by GitHub
parent de13097a89
commit 1fd8d316a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,19 +5,19 @@ import { ComponentFixture, fakeAsync, TestBed, tick } from "@angular/core/testin
import { FormBuilder, UntypedFormBuilder } from "@angular/forms"; import { FormBuilder, UntypedFormBuilder } from "@angular/forms";
import { ActivatedRoute, Router } from "@angular/router"; import { ActivatedRoute, Router } from "@angular/router";
import { RouterTestingModule } from "@angular/router/testing"; import { RouterTestingModule } from "@angular/router/testing";
// eslint-disable-next-line no-restricted-imports import { mock, MockProxy } from "jest-mock-extended";
import { Substitute } from "@fluffy-spoon/substitute";
import { BehaviorSubject, of } from "rxjs"; import { BehaviorSubject, of } from "rxjs";
import { I18nPipe } from "@bitwarden/angular/pipes/i18n.pipe"; import { I18nPipe } from "@bitwarden/angular/pipes/i18n.pipe";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service"; import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/abstractions/log.service"; import { LogService } from "@bitwarden/common/abstractions/log.service";
import { PolicyApiServiceAbstraction } from "@bitwarden/common/abstractions/policy/policy-api.service.abstraction"; import { PolicyApiServiceAbstraction } from "@bitwarden/common/abstractions/policy/policy-api.service.abstraction";
import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/abstractions/policy/policy.service.abstraction";
import { StateService as BaseStateService } from "@bitwarden/common/abstractions/state.service"; import { StateService } from "@bitwarden/common/abstractions/state.service";
import { PlanType } from "@bitwarden/common/enums/planType"; import { PlanType } from "@bitwarden/common/enums/planType";
import { MasterPasswordPolicyOptions } from "@bitwarden/common/models/domain/masterPasswordPolicyOptions"; import { MasterPasswordPolicyOptions } from "@bitwarden/common/models/domain/masterPasswordPolicyOptions";
import { ListResponse } from "@bitwarden/common/models/response/listResponse";
import { PolicyResponse } from "@bitwarden/common/models/response/policyResponse";
import { RouterService } from "../../core"; import { RouterService } from "../../core";
@ -32,23 +32,15 @@ describe("TrialInitiationComponent", () => {
const formBuilder: FormBuilder = new FormBuilder(); const formBuilder: FormBuilder = new FormBuilder();
let routerSpy: jest.SpyInstance; let routerSpy: jest.SpyInstance;
let stateServiceMock: any; let stateServiceMock: MockProxy<StateService>;
let apiServiceMock: any; let policyApiServiceMock: MockProxy<PolicyApiServiceAbstraction>;
let policyServiceMock: any; let policyServiceMock: MockProxy<PolicyService>;
beforeEach(() => { beforeEach(() => {
// only mock functions we use in this component // only define services directly that we want to mock return values in this component
stateServiceMock = { stateServiceMock = mock<StateService>();
getOrganizationInvitation: jest.fn(), policyApiServiceMock = mock<PolicyApiServiceAbstraction>();
}; policyServiceMock = mock<PolicyService>();
apiServiceMock = {
getPoliciesByToken: jest.fn(),
};
policyServiceMock = {
masterPasswordPolicyOptions$: jest.fn(),
};
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [ imports: [
@ -73,23 +65,19 @@ describe("TrialInitiationComponent", () => {
queryParams: mockQueryParams.asObservable(), queryParams: mockQueryParams.asObservable(),
}, },
}, },
{ provide: BaseStateService, useValue: stateServiceMock }, { provide: StateService, useValue: stateServiceMock },
{ provide: PolicyService, useValue: policyServiceMock }, { provide: PolicyService, useValue: policyServiceMock },
{ provide: ApiService, useValue: apiServiceMock }, { provide: PolicyApiServiceAbstraction, useValue: policyApiServiceMock },
{ provide: LogService, useClass: Substitute.for<LogService>() }, { provide: LogService, useValue: mock<LogService>() },
{ provide: I18nService, useClass: Substitute.for<I18nService>() }, { provide: I18nService, useValue: mock<I18nService>() },
{ provide: TitleCasePipe, useClass: Substitute.for<TitleCasePipe>() }, { provide: TitleCasePipe, useValue: mock<TitleCasePipe>() },
{
provide: PolicyApiServiceAbstraction,
useClass: Substitute.for<PolicyApiServiceAbstraction>(),
},
{ {
provide: VerticalStepperComponent, provide: VerticalStepperComponent,
useClass: VerticalStepperStubComponent, useClass: VerticalStepperStubComponent,
}, },
{ {
provide: RouterService, provide: RouterService,
useClass: Substitute.for<RouterService>(), useValue: mock<RouterService>(),
}, },
], ],
schemas: [NO_ERRORS_SCHEMA], // Allows child components to be ignored (such as register component) schemas: [NO_ERRORS_SCHEMA], // Allows child components to be ignored (such as register component)
@ -119,13 +107,16 @@ describe("TrialInitiationComponent", () => {
}); });
it("should set enforcedPolicyOptions if state service returns an invite", async () => { it("should set enforcedPolicyOptions if state service returns an invite", async () => {
// Set up service method mocks // Set up service method mocks
stateServiceMock.getOrganizationInvitation.mockReturnValueOnce({ stateServiceMock.getOrganizationInvitation.mockReturnValueOnce(
Promise.resolve({
organizationId: testOrgId, organizationId: testOrgId,
token: "token", token: "token",
email: "testEmail", email: "testEmail",
organizationUserId: "123", organizationUserId: "123",
}); })
apiServiceMock.getPoliciesByToken.mockReturnValueOnce({ );
policyApiServiceMock.getPoliciesByToken.mockReturnValueOnce(
Promise.resolve({
data: [ data: [
{ {
id: "345", id: "345",
@ -144,7 +135,8 @@ describe("TrialInitiationComponent", () => {
enabled: true, enabled: true,
}, },
], ],
}); } as ListResponse<PolicyResponse>)
);
policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue( policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue(
of({ of({
minComplexity: 4, minComplexity: 4,