From 59efe84bc8d578901cc41b8d723b4e8af55427b3 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sun, 19 Sep 2021 20:09:32 +0200 Subject: [PATCH] Added implementation of setLocations along with tests --- spec/PKPass.ts | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/PKPass.ts | 28 +++++++++++++++++++++++----- 2 files changed, 73 insertions(+), 5 deletions(-) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 6027e9b..f0ed4ff 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -45,4 +45,54 @@ describe("PKPass", () => { expect(pass.props["beacons"].length).toBe(1); }); }); + + describe("setLocations", () => { + it("should reset instance.props['locations'] if 'null' is passed as value", () => { + const pass = new PKPass({}, {}); + + pass.setLocations({ + longitude: 0.25456342344, + latitude: 0.26665773234, + }); + + expect(pass.props["locations"].length).toBe(1); + + pass.setLocations(null); + + expect(pass.props["locations"]).toBeUndefined(); + }); + + it("should filter out invalid beacons objects", () => { + const pass = new PKPass({}, {}); + + pass.setLocations( + { + // @ts-expect-error + longitude: "unknown", + // @ts-expect-error + latitude: "unknown", + }, + { + altitude: "say hello from here", + longitude: 0.25456342344, + }, + { + longitude: 0.25456342344, + latitude: 0.26665773234, + altitude: 12552.31233321, + relevantText: + /** Hi mom, see how do I fly! */ + "Ciao mamma, guarda come volooo!", + }, + ); + + expect(pass.props["locations"].length).toBe(1); + expect(pass.props["locations"][0].longitude).toBe(0.25456342344); + expect(pass.props["locations"][0].latitude).toBe(0.26665773234); + expect(pass.props["locations"][0].altitude).toBe(12552.31233321); + expect(pass.props["locations"][0].relevantText).toBe( + "Ciao mamma, guarda come volooo!", + ); + }); + }); }); diff --git a/src/PKPass.ts b/src/PKPass.ts index 3694af8..f1d7b69 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -318,15 +318,33 @@ export default class PKPass extends Bundle { * Allows setting some locations the OS should * react to and show this pass. * + * Pass `null` to remove them at all. + * + * @example + * ```ts + * PKPassInstance.setLocations(null) + * PKPassInstance.setLocations({ + * latitude: 0.5333245342 + * longitude: 0.2135332252 + * }); + * ``` + * * @param locations * @returns */ - setLocations(...locations: Schemas.Location[]): this { - /** - * @TODO implement - * @TODO specify a way to get current ones deleted - */ + setLocations(locations: null): this; + setLocations(...locations: Schemas.Location[]): this; + setLocations(...locations: (Schemas.Location | null)[]): this { + if (locations[0] === null) { + delete this[propsSymbol]["locations"]; + return; + } + + this[propsSymbol]["locations"] = Schemas.filterValid( + locations, + Schemas.Location, + ); return this; }