From 57c6021dca6f7f698615aee600358657033a9fd8 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 12 Oct 2021 22:44:20 +0200 Subject: [PATCH] Added tests for type getter and setter --- spec/PKPass.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 4d780ef..3713a8b 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -5,6 +5,7 @@ import { localizationSymbol, certificatesSymbol, propsSymbol, + passTypeSymbol, } from "../lib/PKPass"; describe("PKPass", () => { @@ -527,6 +528,69 @@ describe("PKPass", () => { }); }); + describe("type", () => { + describe("getter", () => { + it("should return undefined if no type have been setted", () => { + expect(pass.type).toBeUndefined(); + }); + + it("should return a type if set through pass.json", () => { + pass.addBuffer( + "pass.json", + Buffer.from( + JSON.stringify({ + boardingPass: {}, + }), + ), + ); + + expect(pass.type).toBe("boardingPass"); + }); + }); + + describe("setter", () => { + it("should throw error if a non recognized type is assigned", () => { + expect( + () => + // @ts-expect-error + (pass.type = "asfdg"), + ).toThrow(); + }); + + it("should save the new type under a Symbol in class instance", () => { + pass.type = "boardingPass"; + expect(pass[passTypeSymbol]).toBe("boardingPass"); + }); + + it("should reset fields if they have been previously set", () => { + pass.type = "boardingPass"; + + const { + primaryFields, + secondaryFields, + auxiliaryFields, + headerFields, + backFields, + } = pass; + + pass.type = "coupon"; + + expect(pass.primaryFields).not.toBe(primaryFields); + expect(pass.secondaryFields).not.toBe(secondaryFields); + expect(pass.auxiliaryFields).not.toBe(auxiliaryFields); + expect(pass.headerFields).not.toBe(headerFields); + expect(pass.backFields).not.toBe(backFields); + }); + + it("should delete the previous type if previously setted", () => { + pass.type = "boardingPass"; + pass.type = "coupon"; + + expect(pass["boardingPass"]).toBeUndefined(); + }); + }); + }); + describe("localize", () => { it("should fail throw if lang is not a string", () => { expect(() => pass.localize(null)).toThrowError(