[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
1 changed files with 47 additions and 55 deletions

View File

@ -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<StateService>;
let policyApiServiceMock: MockProxy<PolicyApiServiceAbstraction>;
let policyServiceMock: MockProxy<PolicyService>;
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<StateService>();
policyApiServiceMock = mock<PolicyApiServiceAbstraction>();
policyServiceMock = mock<PolicyService>();
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<LogService>() },
{ provide: I18nService, useClass: Substitute.for<I18nService>() },
{ provide: TitleCasePipe, useClass: Substitute.for<TitleCasePipe>() },
{
provide: PolicyApiServiceAbstraction,
useClass: Substitute.for<PolicyApiServiceAbstraction>(),
},
{ provide: PolicyApiServiceAbstraction, useValue: policyApiServiceMock },
{ provide: LogService, useValue: mock<LogService>() },
{ provide: I18nService, useValue: mock<I18nService>() },
{ provide: TitleCasePipe, useValue: mock<TitleCasePipe>() },
{
provide: VerticalStepperComponent,
useClass: VerticalStepperStubComponent,
},
{
provide: RouterService,
useClass: Substitute.for<RouterService>(),
useValue: mock<RouterService>(),
},
],
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<PolicyResponse>)
);
policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue(
of({
minComplexity: 4,