mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 21:25:26 +00:00
Added tests for FieldsArray shift, unshift and one more for splice
This commit is contained in:
@@ -108,5 +108,88 @@ describe("FieldsArray", () => {
|
||||
expect(pool.has("t1")).toBe(false);
|
||||
expect(pool.has("k1")).toBe(true);
|
||||
});
|
||||
|
||||
it("should log a warning if key already exists and omit that object", () => {
|
||||
fa.push({ key: "t2", value: "v2" });
|
||||
fa.push({ key: "t3", value: "v3" });
|
||||
fa.push({ key: "t4", value: "v4" });
|
||||
|
||||
console.warn = jasmine.createSpy("log");
|
||||
|
||||
fa.splice(0, 1, { key: "t2", value: "v2" });
|
||||
|
||||
expect(console.warn).toHaveBeenCalledWith(
|
||||
Messages.FIELDS.REPEATED_KEY.replace("%s", "t2"),
|
||||
);
|
||||
|
||||
expect(fa.length).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("shift", () => {
|
||||
beforeEach(() => {
|
||||
fa.push({ key: "t1", value: "v1" });
|
||||
fa.push({ key: "t2", value: "v2" });
|
||||
});
|
||||
|
||||
it("should prevent popping out fields if pass is frozen", () => {
|
||||
frozen = true;
|
||||
|
||||
expect(() => fa.shift()).toThrowError(
|
||||
Error,
|
||||
Messages.BUNDLE.CLOSED,
|
||||
);
|
||||
});
|
||||
|
||||
it("should shift out fields", () => {
|
||||
expect(fa.shift()).toEqual({ key: "t1", value: "v1" });
|
||||
expect(fa.length).toBe(1);
|
||||
expect(fa[0]).toEqual({ key: "t2", value: "v2" });
|
||||
});
|
||||
|
||||
it("should remove the key from the pool", () => {
|
||||
expect(pool.has("t1")).toBe(true);
|
||||
|
||||
fa.shift();
|
||||
|
||||
expect(pool.has("t1")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unshift", () => {
|
||||
it("should prevent adding new fields if pass is frozen", () => {
|
||||
frozen = true;
|
||||
|
||||
expect(() => fa.unshift({ key: "t1", value: "v1" })).toThrowError(
|
||||
Error,
|
||||
Messages.BUNDLE.CLOSED,
|
||||
);
|
||||
});
|
||||
|
||||
it("should allow adding fields", () => {
|
||||
expect(fa.unshift({ key: "t1", value: "v1" })).toBe(1);
|
||||
expect(fa.length).toBe(1);
|
||||
expect(fa[0]).toEqual({ key: "t1", value: "v1" });
|
||||
});
|
||||
|
||||
it("should add the key to the pool", () => {
|
||||
fa.unshift({ key: "t1", value: "v1" });
|
||||
|
||||
expect(pool.has("t1")).toBe(true);
|
||||
});
|
||||
|
||||
it("should log a warning if key already exists and omit that object", () => {
|
||||
fa.push({ key: "t1", value: "v1" });
|
||||
|
||||
console.warn = jasmine.createSpy("log");
|
||||
|
||||
fa.unshift({ key: "t1", value: "v1" });
|
||||
|
||||
expect(console.warn).toHaveBeenCalledWith(
|
||||
Messages.FIELDS.REPEATED_KEY.replace("%s", "t1"),
|
||||
);
|
||||
|
||||
expect(fa.length).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user