From c49aad4098028a929d2843fb49a2a9a02eca83b7 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 21 Sep 2021 21:34:28 +0200 Subject: [PATCH] Added setBarcodes implementation along with tests --- spec/PKPass.ts | 117 +++++++++++++++++++++++++++++++++++++++++++++++++ src/PKPass.ts | 51 ++++++++++++++++++--- 2 files changed, 162 insertions(+), 6 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 1135466..5c1783b 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -221,4 +221,121 @@ describe("PKPass", () => { expect(pass.props["relevantDate"]).toBe(undefined); }); }); + + describe("setBarcodes", () => { + it("shouldn't apply changes if no data is passed", () => { + const pass = new PKPass({}, {}); + + const props = pass.props["barcodes"] || []; + const oldAmountOfBarcodes = props?.length ?? 0; + + pass.setBarcodes(); + expect(pass.props["barcodes"]?.length || 0).toBe( + oldAmountOfBarcodes, + ); + }); + + it("should throw error if a boolean parameter is received", () => { + const pass = new PKPass({}, {}); + + // @ts-expect-error + expect(() => pass.setBarcodes(true)).toThrowError( + TypeError, + "Expected Schema.Barcode in setBarcodes but no one is valid.", + ); + }); + + it("should ignore if a number parameter is received", () => { + const pass = new PKPass({}, {}); + + // @ts-expect-error + expect(() => pass.setBarcodes(42)).toThrowError( + TypeError, + "Expected Schema.Barcode in setBarcodes but no one is valid.", + ); + }); + + it("should autogenerate all the barcodes objects if a string is provided as message", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes("28363516282"); + expect(pass.props["barcodes"].length).toBe(4); + }); + + it("should save changes if object conforming to Schemas.Barcode are provided", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes({ + message: "28363516282", + format: "PKBarcodeFormatPDF417", + messageEncoding: "utf8", + }); + + expect(pass.props["barcodes"].length).toBe(1); + }); + + it("should add 'messageEncoding' if missing in valid Schema.Barcode parameters", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes({ + message: "28363516282", + format: "PKBarcodeFormatPDF417", + }); + + expect(pass.props["barcodes"][0].messageEncoding).toBe( + "iso-8859-1", + ); + }); + + it("should ignore objects without 'message' property in Schema.Barcode", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes( + { + format: "PKBarcodeFormatCode128", + message: "No one can validate meeee", + }, + // @ts-expect-error + { + format: "PKBarcodeFormatPDF417", + }, + ); + + expect(pass.props["barcodes"].length).toBe(1); + }); + + it("should ignore objects and values that not comply with Schema.Barcodes", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes( + // @ts-expect-error + 5, + 10, + 15, + { + message: "28363516282", + format: "PKBarcodeFormatPDF417", + }, + 7, + 1, + ); + + expect(pass.props["barcodes"].length).toBe(1); + }); + + it("should reset barcodes content if parameter is null", () => { + const pass = new PKPass({}, {}); + + pass.setBarcodes({ + message: "28363516282", + format: "PKBarcodeFormatPDF417", + messageEncoding: "utf8", + }); + + expect(pass.props["barcodes"].length).toBe(1); + + pass.setBarcodes(null); + expect(pass.props["barcodes"]).toBe(undefined); + }); + }); }); diff --git a/src/PKPass.ts b/src/PKPass.ts index 6214fc5..ba1b6a2 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -392,12 +392,51 @@ export default class PKPass extends Bundle { * @returns */ - setBarcodes(...barcodes: Schemas.Barcode[]): this { - /** - * @TODO implement - * @TODO implement data completion - * @TODO specify a way to get current ones deleted - */ + setBarcodes(barcodes: null): this; + setBarcodes(message: string): this; + setBarcodes(...barcodes: Schemas.Barcode[]): this; + setBarcodes(...barcodes: (Schemas.Barcode | string | null)[]): this { + if (!barcodes.length) { + return this; + } + + if (barcodes[0] === null) { + delete this[propsSymbol]["barcodes"]; + return this; + } + + let finalBarcodes: Schemas.Barcode[]; + + if (typeof barcodes[0] === "string") { + /** A string has been received instead of objects. We can only auto-fill them all with the same data. */ + + const supportedFormats: Array = [ + "PKBarcodeFormatQR", + "PKBarcodeFormatPDF417", + "PKBarcodeFormatAztec", + "PKBarcodeFormatCode128", + ]; + + finalBarcodes = supportedFormats.map((format) => + Schemas.getValidated( + { format, message: barcodes[0] } as Schemas.Barcode, + Schemas.Barcode, + ), + ); + } else { + finalBarcodes = Schemas.filterValid( + barcodes as Schemas.Barcode[], + Schemas.Barcode, + ); + + if (!finalBarcodes.length) { + throw new TypeError( + "Expected Schema.Barcode in setBarcodes but no one is valid.", + ); + } + } + + this[propsSymbol]["barcodes"] = finalBarcodes; return this; }