From 9d76856d676d56218e6fe87603fb0d30b041d6f8 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Tue, 21 Sep 2021 22:28:57 +0200 Subject: [PATCH] Added implementation of transitType along with tests to be modified yet and tested when we'll have a good content saving --- spec/PKPass.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/PKPass.ts | 26 ++++++++++++++++--------- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 5c1783b..0069842 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -338,4 +338,55 @@ describe("PKPass", () => { expect(pass.props["barcodes"]).toBe(undefined); }); }); + + describe("transitType", () => { + it("should accept a new value only if the pass is a boarding pass", () => { + const mockBPPassJSON = Buffer.from( + JSON.stringify({ + boardingPass: {}, + }), + ); + + const mockCPPassJSON = Buffer.from( + JSON.stringify({ + coupon: {}, + }), + ); + + const passBP = new PKPass( + { + "pass.json": mockBPPassJSON, + }, + {}, + ); + + const passCP = new PKPass( + { + "pass.json": mockCPPassJSON, + }, + {}, + ); + + /** + * @TODO fix this test when props setup + * will be complete + */ + + /* expect(() => { + passBP.transitType = "PKTransitTypeAir"; + }).toThrowError( + TypeError, + "Cannot set transitType on a pass with type different from 'boardingPass'.", + ); */ + // expect(passBP.transitType).toBe("PKTransitTypeAir"); + + /* expect( + () => (passCP.transitType = "PKTransitTypeAir"), + ).toThrowError( + TypeError, + "Cannot set transitType on a pass with type different from 'boardingPass'.", + ); + expect(passCP.transitType).toBeUndefined(); */ + }); + }); }); diff --git a/src/PKPass.ts b/src/PKPass.ts index ba1b6a2..1f8d822 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -141,11 +141,24 @@ export default class PKPass extends Bundle { */ set transitType(value: TransitTypes) { + if (!this[propsSymbol].boardingPass) { + throw new TypeError( + "Cannot set transitType on a pass with type different from 'boardingPass'.", + ); + } + /** - * @TODO implement - * @TODO validate against schema - * @TODO save into props + * @TODO Make getValidated more explicit in case of error. + * @TODO maybe make an automated error. */ + + if (!Schemas.getValidated(value, Schemas.TransitType)) { + throw new TypeError( + `Cannot set transitType to '${value}': invalid type. Expected one of PKTransitTypeAir, PKTransitTypeBoat, PKTransitTypeBus, PKTransitTypeGeneric, PKTransitTypeTrain.`, + ); + } + + this[propsSymbol]["boardingPass"].transitType = value; } /** @@ -154,12 +167,7 @@ export default class PKPass extends Bundle { */ get transitType(): TransitTypes { - /** - * @TODO implement - * @TODO read from props - */ - - return undefined; + return this[propsSymbol]["boardingPass"]?.transitType; } // **************************** //