1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 01:33:33 +00:00

[ADR-0006][AC-319] Migrate all tests to use jest mock instead of substitute (#6520)

Standardize on using jest mock instead of having two mocking frameworks which can be confusing.
This commit is contained in:
Oscar Hinton
2023-10-17 19:02:33 +02:00
committed by GitHub
parent 5cacd79d8c
commit ffb67be0a2
17 changed files with 198 additions and 208 deletions

View File

@@ -1,5 +1,4 @@
// eslint-disable-next-line no-restricted-imports
import { Substitute, Arg } from "@fluffy-spoon/substitute";
import { mock } from "jest-mock-extended";
import { Jsonify } from "type-fest";
import { makeStaticByteArray, mockEnc, mockFromJson } from "../../../../spec/utils";
@@ -219,15 +218,15 @@ describe("Cipher DTO", () => {
loginView.username = "username";
loginView.password = "password";
const login = Substitute.for<Login>();
login.decrypt(Arg.any(), Arg.any()).resolves(loginView);
const login = mock<Login>();
login.decrypt.mockResolvedValue(loginView);
cipher.login = login;
const cryptoService = Substitute.for<CryptoService>();
const encryptService = Substitute.for<EncryptService>();
const cipherService = Substitute.for<CipherService>();
const cryptoService = mock<CryptoService>();
const encryptService = mock<EncryptService>();
const cipherService = mock<CipherService>();
encryptService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(64));
encryptService.decryptToBytes.mockResolvedValue(makeStaticByteArray(64));
(window as any).bitwardenContainerService = new ContainerService(
cryptoService,
@@ -343,11 +342,11 @@ describe("Cipher DTO", () => {
cipher.secureNote.type = SecureNoteType.Generic;
cipher.key = mockEnc("EncKey");
const cryptoService = Substitute.for<CryptoService>();
const encryptService = Substitute.for<EncryptService>();
const cipherService = Substitute.for<CipherService>();
const cryptoService = mock<CryptoService>();
const encryptService = mock<EncryptService>();
const cipherService = mock<CipherService>();
encryptService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(64));
encryptService.decryptToBytes.mockResolvedValue(makeStaticByteArray(64));
(window as any).bitwardenContainerService = new ContainerService(
cryptoService,
@@ -477,15 +476,15 @@ describe("Cipher DTO", () => {
cardView.cardholderName = "cardholderName";
cardView.number = "4111111111111111";
const card = Substitute.for<Card>();
card.decrypt(Arg.any(), Arg.any()).resolves(cardView);
const card = mock<Card>();
card.decrypt.mockResolvedValue(cardView);
cipher.card = card;
const cryptoService = Substitute.for<CryptoService>();
const encryptService = Substitute.for<EncryptService>();
const cipherService = Substitute.for<CipherService>();
const cryptoService = mock<CryptoService>();
const encryptService = mock<EncryptService>();
const cipherService = mock<CipherService>();
encryptService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(64));
encryptService.decryptToBytes.mockResolvedValue(makeStaticByteArray(64));
(window as any).bitwardenContainerService = new ContainerService(
cryptoService,
@@ -639,15 +638,15 @@ describe("Cipher DTO", () => {
identityView.firstName = "firstName";
identityView.lastName = "lastName";
const identity = Substitute.for<Identity>();
identity.decrypt(Arg.any(), Arg.any()).resolves(identityView);
const identity = mock<Identity>();
identity.decrypt.mockResolvedValue(identityView);
cipher.identity = identity;
const cryptoService = Substitute.for<CryptoService>();
const encryptService = Substitute.for<EncryptService>();
const cipherService = Substitute.for<CipherService>();
const cryptoService = mock<CryptoService>();
const encryptService = mock<EncryptService>();
const cipherService = mock<CipherService>();
encryptService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(64));
encryptService.decryptToBytes.mockResolvedValue(makeStaticByteArray(64));
(window as any).bitwardenContainerService = new ContainerService(
cryptoService,

View File

@@ -1,5 +1,4 @@
// eslint-disable-next-line no-restricted-imports
import { Substitute, Arg } from "@fluffy-spoon/substitute";
import { mock } from "jest-mock-extended";
import { mockEnc, mockFromJson } from "../../../../spec";
import { UriMatchType } from "../../../enums";
@@ -51,10 +50,10 @@ describe("Login DTO", () => {
});
it("Decrypts correctly", async () => {
const loginUri = Substitute.for<LoginUri>();
const loginUri = mock<LoginUri>();
const loginUriView = new LoginUriView();
loginUriView.uri = "decrypted uri";
loginUri.decrypt(Arg.any()).resolves(loginUriView);
loginUri.decrypt.mockResolvedValue(loginUriView);
const login = new Login();
login.uris = [loginUri];

View File

@@ -1,13 +1,14 @@
// eslint-disable-next-line no-restricted-imports
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { mock, MockProxy } from "jest-mock-extended";
import { BehaviorSubject, firstValueFrom } from "rxjs";
import { makeStaticByteArray } from "../../../../spec";
import { CryptoService } from "../../../platform/abstractions/crypto.service";
import { EncryptService } from "../../../platform/abstractions/encrypt.service";
import { I18nService } from "../../../platform/abstractions/i18n.service";
import { StateService } from "../../../platform/abstractions/state.service";
import { EncString } from "../../../platform/models/domain/enc-string";
import { SymmetricCryptoKey, UserKey } from "../../../platform/models/domain/symmetric-crypto-key";
import { ContainerService } from "../../../platform/services/container.service";
import { StateService } from "../../../platform/services/state.service";
import { CipherService } from "../../abstractions/cipher.service";
import { FolderData } from "../../models/data/folder.data";
import { FolderView } from "../../models/view/folder.view";
@@ -16,28 +17,36 @@ import { FolderService } from "../../services/folder/folder.service";
describe("Folder Service", () => {
let folderService: FolderService;
let cryptoService: SubstituteOf<CryptoService>;
let encryptService: SubstituteOf<EncryptService>;
let i18nService: SubstituteOf<I18nService>;
let cipherService: SubstituteOf<CipherService>;
let stateService: SubstituteOf<StateService>;
let cryptoService: MockProxy<CryptoService>;
let encryptService: MockProxy<EncryptService>;
let i18nService: MockProxy<I18nService>;
let cipherService: MockProxy<CipherService>;
let stateService: MockProxy<StateService>;
let activeAccount: BehaviorSubject<string>;
let activeAccountUnlocked: BehaviorSubject<boolean>;
beforeEach(() => {
cryptoService = Substitute.for();
encryptService = Substitute.for();
i18nService = Substitute.for();
cipherService = Substitute.for();
stateService = Substitute.for();
cryptoService = mock<CryptoService>();
encryptService = mock<EncryptService>();
i18nService = mock<I18nService>();
cipherService = mock<CipherService>();
stateService = mock<StateService>();
activeAccount = new BehaviorSubject("123");
activeAccountUnlocked = new BehaviorSubject(true);
stateService.getEncryptedFolders().resolves({
i18nService.collator = new Intl.Collator("en");
stateService.getEncryptedFolders.mockResolvedValue({
"1": folderData("1", "test"),
});
stateService.activeAccount$.returns(activeAccount);
stateService.activeAccountUnlocked$.returns(activeAccountUnlocked);
stateService.activeAccount$ = activeAccount;
stateService.activeAccountUnlocked$ = activeAccountUnlocked;
cryptoService.hasUserKey.mockResolvedValue(true);
cryptoService.getUserKeyWithLegacySupport.mockResolvedValue(
new SymmetricCryptoKey(makeStaticByteArray(32)) as UserKey
);
encryptService.decryptToUtf8.mockResolvedValue("DEC");
(window as any).bitwardenContainerService = new ContainerService(cryptoService, encryptService);
folderService = new FolderService(cryptoService, i18nService, cipherService, stateService);
@@ -48,8 +57,8 @@ describe("Folder Service", () => {
model.id = "2";
model.name = "Test Folder";
cryptoService.encrypt(Arg.any()).resolves(new EncString("ENC"));
cryptoService.decryptToUtf8(Arg.any()).resolves("DEC");
cryptoService.encrypt.mockResolvedValue(new EncString("ENC"));
cryptoService.decryptToUtf8.mockResolvedValue("DEC");
const result = await folderService.encrypt(model);
@@ -69,9 +78,9 @@ describe("Folder Service", () => {
expect(result).toEqual({
id: "1",
name: {
decryptedValue: [],
encryptedString: "test",
encryptionType: 0,
decryptedValue: "DEC",
},
revisionDate: null,
});
@@ -91,27 +100,27 @@ describe("Folder Service", () => {
{
id: "1",
name: {
decryptedValue: [],
encryptedString: "test",
encryptionType: 0,
decryptedValue: "DEC",
},
revisionDate: null,
},
{
id: "2",
name: {
decryptedValue: [],
encryptedString: "test 2",
encryptionType: 0,
decryptedValue: "DEC",
},
revisionDate: null,
},
]);
expect(await firstValueFrom(folderService.folderViews$)).toEqual([
{ id: "1", name: [], revisionDate: null },
{ id: "2", name: [], revisionDate: null },
{ id: null, name: [], revisionDate: null },
{ id: "1", name: "DEC", revisionDate: null },
{ id: "2", name: "DEC", revisionDate: null },
{ id: null, name: undefined, revisionDate: null },
]);
});
@@ -122,7 +131,7 @@ describe("Folder Service", () => {
{
id: "2",
name: {
decryptedValue: [],
decryptedValue: "DEC",
encryptedString: "test 2",
encryptionType: 0,
},
@@ -131,8 +140,8 @@ describe("Folder Service", () => {
]);
expect(await firstValueFrom(folderService.folderViews$)).toEqual([
{ id: "2", name: [], revisionDate: null },
{ id: null, name: [], revisionDate: null },
{ id: "2", name: "DEC", revisionDate: null },
{ id: null, name: undefined, revisionDate: null },
]);
});
@@ -142,7 +151,7 @@ describe("Folder Service", () => {
expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
expect(await firstValueFrom(folderService.folderViews$)).toEqual([
{ id: null, name: [], revisionDate: null },
{ id: null, name: undefined, revisionDate: null },
]);
});
@@ -166,17 +175,17 @@ describe("Folder Service", () => {
it("null userId", async () => {
await folderService.clear();
stateService.received(1).setEncryptedFolders(Arg.any(), Arg.any());
expect(stateService.setEncryptedFolders).toBeCalledTimes(1);
expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
});
it("matching userId", async () => {
stateService.getUserId().resolves("1");
stateService.getUserId.mockResolvedValue("1");
await folderService.clear("1");
stateService.received(1).setEncryptedFolders(Arg.any(), Arg.any());
expect(stateService.setEncryptedFolders).toBeCalledTimes(1);
expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
@@ -185,7 +194,7 @@ describe("Folder Service", () => {
it("missmatching userId", async () => {
await folderService.clear("12");
stateService.received(1).setEncryptedFolders(Arg.any(), Arg.any());
expect(stateService.setEncryptedFolders).toBeCalledTimes(1);
expect((await firstValueFrom(folderService.folders$)).length).toBe(1);
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(2);