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:
parent
de13097a89
commit
1fd8d316a1
@ -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,13 +107,16 @@ describe("TrialInitiationComponent", () => {
|
||||
});
|
||||
it("should set enforcedPolicyOptions if state service returns an invite", async () => {
|
||||
// Set up service method mocks
|
||||
stateServiceMock.getOrganizationInvitation.mockReturnValueOnce({
|
||||
stateServiceMock.getOrganizationInvitation.mockReturnValueOnce(
|
||||
Promise.resolve({
|
||||
organizationId: testOrgId,
|
||||
token: "token",
|
||||
email: "testEmail",
|
||||
organizationUserId: "123",
|
||||
});
|
||||
apiServiceMock.getPoliciesByToken.mockReturnValueOnce({
|
||||
})
|
||||
);
|
||||
policyApiServiceMock.getPoliciesByToken.mockReturnValueOnce(
|
||||
Promise.resolve({
|
||||
data: [
|
||||
{
|
||||
id: "345",
|
||||
@ -144,7 +135,8 @@ describe("TrialInitiationComponent", () => {
|
||||
enabled: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
} as ListResponse<PolicyResponse>)
|
||||
);
|
||||
policyServiceMock.masterPasswordPolicyOptions$.mockReturnValue(
|
||||
of({
|
||||
minComplexity: 4,
|
||||
|
Loading…
Reference in New Issue
Block a user