bitwarden-estensione-browser/libs/common/spec/models/view/cipherView.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.4 KiB
TypeScript
Raw Normal View History

import { CipherRepromptType } from "@bitwarden/common/enums/cipherRepromptType";
import { CipherType } from "@bitwarden/common/enums/cipherType";
2022-06-28 02:28:26 +02:00
import { AttachmentView } from "@bitwarden/common/models/view/attachmentView";
import { CardView } from "@bitwarden/common/models/view/cardView";
2022-06-28 01:42:35 +02:00
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { FieldView } from "@bitwarden/common/models/view/fieldView";
import { LoginView } from "@bitwarden/common/models/view/loginView";
import { PasswordHistoryView } from "@bitwarden/common/models/view/passwordHistoryView";
2022-06-28 01:42:35 +02:00
jest.mock("@bitwarden/common/models/view/loginView");
2022-06-28 02:28:26 +02:00
jest.mock("@bitwarden/common/models/view/attachmentView");
jest.mock("@bitwarden/common/models/view/fieldView");
jest.mock("@bitwarden/common/models/view/passwordHistoryView");
2022-06-28 01:42:35 +02:00
describe("CipherView", () => {
2022-06-28 03:36:45 +02:00
const obj = {
id: "myId",
organizationId: "myOrgId",
folderId: "myFolderId",
name: "my Cipher",
notes: "lorem ipsum",
type: CipherType.Login,
favorite: true,
organizationUseTotp: true,
edit: true,
viewPassword: false,
localData: { lastUsedDate: "123" },
login: "myLogin",
attachments: ["attachment1", "attachment2"],
fields: ["field1", "field2"],
passwordHistory: ["ph1", "ph2", "ph3"],
collectionIds: ["collection1", "collection2"],
revisionDate: new Date(),
deletedDate: new Date(),
reprompt: CipherRepromptType.Password,
};
2022-06-28 01:42:35 +02:00
beforeEach(() => {
(LoginView as any).mockClear();
2022-06-28 01:42:35 +02:00
(AttachmentView as any).mockClear();
(FieldView as any).mockClear();
(PasswordHistoryView as any).mockClear();
2022-06-28 01:42:35 +02:00
});
2022-06-28 03:36:45 +02:00
it("toJSON() creates object for serialization", () => {
const cipher = new CipherView();
2022-06-28 03:36:45 +02:00
Object.assign(cipher, obj);
expect(cipher.toJSON()).toEqual(obj);
});
it("fromJSON() populates from deserialized object", () => {
const expected = new CipherView();
Object.assign(expected, obj);
const mockCtor = (key: string) => key as any;
jest.spyOn(LoginView, "fromJSON").mockImplementation(mockCtor);
jest.spyOn(AttachmentView, "fromJSON").mockImplementation(mockCtor);
jest.spyOn(FieldView, "fromJSON").mockImplementation(mockCtor);
jest.spyOn(PasswordHistoryView, "fromJSON").mockImplementation(mockCtor);
const newCipher = CipherView.fromJSON(obj);
expect(newCipher).toEqual(expected);
expect(newCipher).toBeInstanceOf(CipherView);
2022-06-28 01:42:35 +02:00
});
});