From 1fd8d316a15ba015c3509afd75abb4f4bcb8e12b Mon Sep 17 00:00:00 2001 From: Robyn MacCallum Date: Fri, 14 Oct 2022 09:21:36 -0400 Subject: [PATCH] [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 --- .../trial-initiation.component.spec.ts | 102 ++++++++---------- 1 file changed, 47 insertions(+), 55 deletions(-) diff --git a/apps/web/src/app/accounts/trial-initiation/trial-initiation.component.spec.ts b/apps/web/src/app/accounts/trial-initiation/trial-initiation.component.spec.ts index c69a1bf1f0..8e186aac06 100644 --- a/apps/web/src/app/accounts/trial-initiation/trial-initiation.component.spec.ts +++ b/apps/web/src/app/accounts/trial-initiation/trial-initiation.component.spec.ts @@ -5,19 +5,19 @@ import { ComponentFixture, fakeAsync, TestBed, tick } from "@angular/core/testin import { FormBuilder, UntypedFormBuilder } from "@angular/forms"; import { ActivatedRoute, Router } from "@angular/router"; import { RouterTestingModule } from "@angular/router/testing"; -// eslint-disable-next-line no-restricted-imports -import { Substitute } from "@fluffy-spoon/substitute"; +import { mock, MockProxy } from "jest-mock-extended"; import { BehaviorSubject, of } from "rxjs"; 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 { LogService } from "@bitwarden/common/abstractions/log.service"; import { PolicyApiServiceAbstraction } from "@bitwarden/common/abstractions/policy/policy-api.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 { 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"; @@ -32,23 +32,15 @@ describe("TrialInitiationComponent", () => { const formBuilder: FormBuilder = new FormBuilder(); let routerSpy: jest.SpyInstance; - let stateServiceMock: any; - let apiServiceMock: any; - let policyServiceMock: any; + let stateServiceMock: MockProxy; + let policyApiServiceMock: MockProxy; + let policyServiceMock: MockProxy; beforeEach(() => { - // only mock functions we use in this component - stateServiceMock = { - getOrganizationInvitation: jest.fn(), - }; - - apiServiceMock = { - getPoliciesByToken: jest.fn(), - }; - - policyServiceMock = { - masterPasswordPolicyOptions$: jest.fn(), - }; + // only define services directly that we want to mock return values in this component + stateServiceMock = mock(); + policyApiServiceMock = mock(); + policyServiceMock = mock(); TestBed.configureTestingModule({ imports: [ @@ -73,23 +65,19 @@ describe("TrialInitiationComponent", () => { queryParams: mockQueryParams.asObservable(), }, }, - { provide: BaseStateService, useValue: stateServiceMock }, + { provide: StateService, useValue: stateServiceMock }, { provide: PolicyService, useValue: policyServiceMock }, - { provide: ApiService, useValue: apiServiceMock }, - { provide: LogService, useClass: Substitute.for() }, - { provide: I18nService, useClass: Substitute.for() }, - { provide: TitleCasePipe, useClass: Substitute.for() }, - { - provide: PolicyApiServiceAbstraction, - useClass: Substitute.for(), - }, + { provide: PolicyApiServiceAbstraction, useValue: policyApiServiceMock }, + { provide: LogService, useValue: mock() }, + { provide: I18nService, useValue: mock() }, + { provide: TitleCasePipe, useValue: mock() }, { provide: VerticalStepperComponent, useClass: VerticalStepperStubComponent, }, { provide: RouterService, - useClass: Substitute.for(), + useValue: mock(), }, ], schemas: [NO_ERRORS_SCHEMA], // Allows child components to be ignored (such as register component) @@ -119,32 +107,36 @@ describe("TrialInitiationComponent", () => { }); it("should set enforcedPolicyOptions if state service returns an invite", async () => { // Set up service method mocks - stateServiceMock.getOrganizationInvitation.mockReturnValueOnce({ - organizationId: testOrgId, - token: "token", - email: "testEmail", - organizationUserId: "123", - }); - apiServiceMock.getPoliciesByToken.mockReturnValueOnce({ - data: [ - { - id: "345", - organizationId: testOrgId, - type: 1, - data: [ - { - minComplexity: 4, - minLength: 10, - requireLower: null, - requireNumbers: null, - requireSpecial: null, - requireUpper: null, - }, - ], - enabled: true, - }, - ], - }); + stateServiceMock.getOrganizationInvitation.mockReturnValueOnce( + Promise.resolve({ + organizationId: testOrgId, + token: "token", + email: "testEmail", + organizationUserId: "123", + }) + ); + policyApiServiceMock.getPoliciesByToken.mockReturnValueOnce( + Promise.resolve({ + data: [ + { + id: "345", + organizationId: testOrgId, + type: 1, + data: [ + { + minComplexity: 4, + minLength: 10, + requireLower: null, + requireNumbers: null, + requireSpecial: null, + requireUpper: null, + }, + ], + enabled: true, + }, + ], + } as ListResponse) + ); policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue( of({ minComplexity: 4,