mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 18:25:24 +00:00
Added implementation for setBeacons along with some tests
This commit is contained in:
48
spec/PKPass.ts
Normal file
48
spec/PKPass.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { default as PKPass } from "../lib/PKPass";
|
||||
|
||||
describe("PKPass", () => {
|
||||
describe("setBeacons", () => {
|
||||
it("should reset instance.props['beacons'] if 'null' is passed as value", () => {
|
||||
const pass = new PKPass({}, {});
|
||||
|
||||
pass.setBeacons({
|
||||
proximityUUID: "0000000000-00000000",
|
||||
major: 4,
|
||||
minor: 3,
|
||||
relevantText: "This is not the Kevin you are looking for.",
|
||||
});
|
||||
|
||||
expect(pass.props["beacons"].length).toBe(1);
|
||||
|
||||
pass.setBeacons(null);
|
||||
|
||||
expect(pass.props["beacons"]).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should filter out invalid beacons objects", () => {
|
||||
const pass = new PKPass({}, {});
|
||||
|
||||
/** This is invalid, major should be greater than minor */
|
||||
pass.setBeacons(
|
||||
{
|
||||
proximityUUID: "0000000000-00000000",
|
||||
major: 2,
|
||||
minor: 3,
|
||||
relevantText: "This is not the Kevin you are looking for.",
|
||||
},
|
||||
// @ts-expect-error
|
||||
{
|
||||
major: 2,
|
||||
minor: 3,
|
||||
},
|
||||
{
|
||||
proximityUUID: "0000000000-00000",
|
||||
major: 2,
|
||||
minor: 1,
|
||||
},
|
||||
);
|
||||
|
||||
expect(pass.props["beacons"].length).toBe(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -284,15 +284,32 @@ export default class PKPass extends Bundle {
|
||||
* Allows setting some beacons the OS should
|
||||
* react to and show this pass.
|
||||
*
|
||||
* Pass `null` to remove them at all.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* PKPassInstance.setBeacons(null)
|
||||
* PKPassInstance.setBeacons({
|
||||
* proximityUUID: "00000-000000-0000-00000000000",
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @param beacons
|
||||
* @returns
|
||||
*/
|
||||
|
||||
setBeacons(...beacons: Schemas.Beacon[]): this {
|
||||
/**
|
||||
* @TODO implement
|
||||
* @TODO specify a way to get current ones deleted
|
||||
*/
|
||||
setBeacons(beacons: null): this;
|
||||
setBeacons(...beacons: Schemas.Beacon[]): this;
|
||||
setBeacons(...beacons: (Schemas.Beacon | null)[]): this {
|
||||
if (beacons[0] === null) {
|
||||
delete this[propsSymbol]["beacons"];
|
||||
return;
|
||||
}
|
||||
|
||||
this[propsSymbol]["beacons"] = Schemas.filterValid(
|
||||
beacons,
|
||||
Schemas.Beacon,
|
||||
);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -245,3 +245,14 @@ export function getValidated<T extends Object>(
|
||||
|
||||
return validation.value;
|
||||
}
|
||||
|
||||
export function filterValid<T extends Object>(
|
||||
source: T[],
|
||||
schema: AvailableSchemas,
|
||||
): T[] {
|
||||
if (!source) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return source.filter((current) => isValid(current, schema));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user