1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-09-12 01:48:21 +02:00
bitwarden-browser/libs/common/spec/models/view/cipherView.spec.ts
2022-10-04 06:50:43 +10:00

77 lines
3.2 KiB
TypeScript

import { CipherType } from "@bitwarden/common/enums/cipherType";
import { AttachmentView } from "@bitwarden/common/models/view/attachmentView";
import { CardView } from "@bitwarden/common/models/view/cardView";
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { FieldView } from "@bitwarden/common/models/view/fieldView";
import { IdentityView } from "@bitwarden/common/models/view/identityView";
import { LoginView } from "@bitwarden/common/models/view/loginView";
import { PasswordHistoryView } from "@bitwarden/common/models/view/passwordHistoryView";
import { SecureNoteView } from "@bitwarden/common/models/view/secureNoteView";
import { mockFromJson } from "../../utils";
jest.mock("@bitwarden/common/models/view/loginView");
jest.mock("@bitwarden/common/models/view/attachmentView");
jest.mock("@bitwarden/common/models/view/fieldView");
jest.mock("@bitwarden/common/models/view/passwordHistoryView");
describe("CipherView", () => {
beforeEach(() => {
(LoginView as any).mockClear();
(AttachmentView as any).mockClear();
(FieldView as any).mockClear();
(PasswordHistoryView as any).mockClear();
});
describe("fromJSON", () => {
it("initializes nested objects", () => {
jest.spyOn(AttachmentView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(FieldView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(PasswordHistoryView, "fromJSON").mockImplementation(mockFromJson);
const revisionDate = new Date("2022-08-04T01:06:40.441Z");
const deletedDate = new Date("2022-09-04T01:06:40.441Z");
const actual = CipherView.fromJSON({
revisionDate: revisionDate.toISOString(),
deletedDate: deletedDate.toISOString(),
attachments: ["attachment1", "attachment2"] as any,
fields: ["field1", "field2"] as any,
passwordHistory: ["ph1", "ph2", "ph3"] as any,
});
const expected = {
revisionDate: revisionDate,
deletedDate: deletedDate,
attachments: ["attachment1_fromJSON", "attachment2_fromJSON"],
fields: ["field1_fromJSON", "field2_fromJSON"],
passwordHistory: ["ph1_fromJSON", "ph2_fromJSON", "ph3_fromJSON"],
};
expect(actual).toMatchObject(expected);
});
test.each([
// Test description, CipherType, expected output
["LoginView", CipherType.Login, { login: "myLogin_fromJSON" }],
["CardView", CipherType.Card, { card: "myCard_fromJSON" }],
["IdentityView", CipherType.Identity, { identity: "myIdentity_fromJSON" }],
["Secure Note", CipherType.SecureNote, { secureNote: "mySecureNote_fromJSON" }],
])("initializes %s", (description: string, cipherType: CipherType, expected: any) => {
jest.spyOn(LoginView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(IdentityView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(CardView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(SecureNoteView, "fromJSON").mockImplementation(mockFromJson);
const actual = CipherView.fromJSON({
login: "myLogin",
card: "myCard",
identity: "myIdentity",
secureNote: "mySecureNote",
type: cipherType,
} as any);
expect(actual).toMatchObject(expected);
});
});
});