mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 05:43:41 +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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user