bitwarden-estensione-browser/libs/common/spec/models/domain/field.spec.ts

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

87 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-06-14 17:10:53 +02:00
import { FieldType } from "@bitwarden/common/enums/fieldType";
import { FieldData } from "@bitwarden/common/models/data/fieldData";
import { EncString } from "@bitwarden/common/models/domain/encString";
2022-06-14 17:10:53 +02:00
import { Field } from "@bitwarden/common/models/domain/field";
2022-04-16 17:18:12 +02:00
import { mockEnc, mockFromJson } from "../../utils";
2022-04-16 17:18:12 +02:00
describe("Field", () => {
let data: FieldData;
beforeEach(() => {
data = {
type: FieldType.Text,
name: "encName",
value: "encValue",
linkedId: null,
};
});
it("Convert from empty", () => {
const data = new FieldData();
const field = new Field(data);
expect(field).toEqual({
type: undefined,
name: null,
value: null,
linkedId: undefined,
});
});
it("Convert", () => {
const field = new Field(data);
expect(field).toEqual({
type: FieldType.Text,
name: { encryptedString: "encName", encryptionType: 0 },
value: { encryptedString: "encValue", encryptionType: 0 },
linkedId: null,
});
});
it("toFieldData", () => {
const field = new Field(data);
expect(field.toFieldData()).toEqual(data);
});
it("Decrypt", async () => {
const field = new Field();
field.type = FieldType.Text;
field.name = mockEnc("encName");
field.value = mockEnc("encValue");
const view = await field.decrypt(null);
expect(view).toEqual({
type: 0,
name: "encName",
value: "encValue",
newField: false,
showCount: false,
showValue: false,
});
});
describe("fromJSON", () => {
it("initializes nested objects", () => {
jest.spyOn(EncString, "fromJSON").mockImplementation(mockFromJson);
const actual = Field.fromJSON({
name: "myName",
value: "myValue",
});
expect(actual).toEqual({
name: "myName_fromJSON",
value: "myValue_fromJSON",
});
expect(actual).toBeInstanceOf(Field);
});
it("returns null if object is null", () => {
expect(Field.fromJSON(null)).toBeNull();
});
});
2022-04-16 17:18:12 +02:00
});