mirror of
https://github.com/bitwarden/browser
synced 2025-12-15 15:53:27 +00:00
[EC-281] Add de/serialization methods to CipherView objects (#2970)
This commit is contained in:
76
libs/common/spec/models/view/cipherView.spec.ts
Normal file
76
libs/common/spec/models/view/cipherView.spec.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
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";
|
||||
|
||||
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", () => {
|
||||
const mockFromJson = (stub: any) => (stub + "_fromJSON") as any;
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user