Fixed tests, added tests, fixed ducked up things

This commit is contained in:
Alexander Cerutti
2021-10-11 23:04:09 +02:00
parent cba4d10fd0
commit 928081ecfc
6 changed files with 216 additions and 134 deletions

View File

@@ -43,11 +43,27 @@ describe("Bundle", () => {
expect(bundle.getAsStream()).toBeInstanceOf(Stream); expect(bundle.getAsStream()).toBeInstanceOf(Stream);
}); });
it("should freeze the bundle when using 'getAsStream'", () => {
bundle.getAsStream();
expect(bundle.isFrozen).toBe(true);
});
it("should return a buffer with 'getAsBuffer'", async () => { it("should return a buffer with 'getAsBuffer'", async () => {
addEmptyFilesToBundle(bundle); addEmptyFilesToBundle(bundle);
expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer); expect(await bundle.getAsBuffer()).toBeInstanceOf(Buffer);
}); });
it("should freeze the bundle when using 'getAsBuffer'", async () => {
await bundle.getAsBuffer();
expect(bundle.isFrozen).toBe(true);
});
it("freezables should expose freezable and bundle itself to be frozen", () => {
const [bundle, freeze] = Bundle.freezable("any/any");
freeze();
expect(bundle.isFrozen).toBe(true);
});
}); });
function addEmptyFilesToBundle(bundle: Bundle) { function addEmptyFilesToBundle(bundle: Bundle) {

View File

@@ -5,10 +5,24 @@ import {
} from "../lib/PKPass"; } from "../lib/PKPass";
describe("PKPass", () => { describe("PKPass", () => {
let pass: PKPass;
beforeEach(() => {
pass = new PKPass(
{},
/** @ts-ignore - We don't need certificates here*/
{
signerCert: "",
signerKey: "",
wwdr: "",
signerKeyPassphrase: "p477w0rb",
},
{},
);
});
describe("setBeacons", () => { describe("setBeacons", () => {
it("should reset instance.props['beacons'] if 'null' is passed as value", () => { it("should reset instance.props['beacons'] if 'null' is passed as value", () => {
const pass = new PKPass({}, {});
pass.setBeacons({ pass.setBeacons({
proximityUUID: "0000000000-00000000", proximityUUID: "0000000000-00000000",
major: 4, major: 4,
@@ -24,8 +38,6 @@ describe("PKPass", () => {
}); });
it("should filter out invalid beacons objects", () => { it("should filter out invalid beacons objects", () => {
const pass = new PKPass({}, {});
/** This is invalid, major should be greater than minor */ /** This is invalid, major should be greater than minor */
pass.setBeacons( pass.setBeacons(
{ {
@@ -48,12 +60,22 @@ describe("PKPass", () => {
expect(pass.props["beacons"].length).toBe(1); expect(pass.props["beacons"].length).toBe(1);
}); });
it("should always return undefined", () => {
expect(pass.setBeacons(null)).toBeUndefined();
expect(
pass.setBeacons({
proximityUUID: "0000000000-00000000",
major: 2,
minor: 3,
relevantText: "This is not the Kevin you are looking for.",
}),
).toBeUndefined();
});
}); });
describe("setLocations", () => { describe("setLocations", () => {
it("should reset instance.props['locations'] if 'null' is passed as value", () => { it("should reset instance.props['locations'] if 'null' is passed as value", () => {
const pass = new PKPass({}, {});
pass.setLocations({ pass.setLocations({
longitude: 0.25456342344, longitude: 0.25456342344,
latitude: 0.26665773234, latitude: 0.26665773234,
@@ -67,8 +89,6 @@ describe("PKPass", () => {
}); });
it("should filter out invalid beacons objects", () => { it("should filter out invalid beacons objects", () => {
const pass = new PKPass({}, {});
pass.setLocations( pass.setLocations(
{ {
// @ts-expect-error // @ts-expect-error
@@ -98,12 +118,21 @@ describe("PKPass", () => {
"Ciao mamma, guarda come volooo!", "Ciao mamma, guarda come volooo!",
); );
}); });
it("should always return undefined", () => {
expect(pass.setLocations(null)).toBeUndefined();
expect(
pass.setLocations({
longitude: 0.25456342344,
latitude: 0.26665773234,
altitude: 12552.31233321,
}),
).toBeUndefined();
});
}); });
describe("setNFCCapability", () => { describe("setNFCCapability", () => {
it("should reset instance.props['nfc'] if 'null' is passed as value", () => { it("should reset instance.props['nfc'] if 'null' is passed as value", () => {
const pass = new PKPass({}, {});
pass.setNFCCapability({ pass.setNFCCapability({
encryptionPublicKey: "mimmo", encryptionPublicKey: "mimmo",
message: "No message for you here", message: "No message for you here",
@@ -119,23 +148,29 @@ describe("PKPass", () => {
expect(pass.props["nfc"]).toBeUndefined(); expect(pass.props["nfc"]).toBeUndefined();
}); });
it("should not accept invalid objects", () => { it("should throw on invalid objects received", () => {
const pass = new PKPass({}, {}); expect(() =>
pass.setNFCCapability({
// @ts-expect-error
requiresAuth: false,
encryptionPublicKey: "Nope",
}),
).toThrow();
});
pass.setNFCCapability({ it("should always return undefined", () => {
// @ts-expect-error expect(pass.setNFCCapability(null)).toBeUndefined();
requiresAuth: false, expect(
encryptionPublicKey: "Nope", pass.setNFCCapability({
}); encryptionPublicKey: "mimmo",
message: "No message for you here",
expect(pass.props["nfc"]).toBeUndefined(); }),
).toBeUndefined();
}); });
}); });
describe("setExpirationDate", () => { describe("setExpirationDate", () => {
it("should reset instance.props['expirationDate'] if 'null' is passed as value", () => { it("should reset instance.props['expirationDate'] if 'null' is passed as value", () => {
const pass = new PKPass({}, {});
pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0)); pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0));
// Month starts from 0 in Date Object when used this way, therefore // Month starts from 0 in Date Object when used this way, therefore
// we expect one month more // we expect one month more
@@ -146,44 +181,48 @@ describe("PKPass", () => {
expect(pass.props["expirationDate"]).toBeUndefined(); expect(pass.props["expirationDate"]).toBeUndefined();
}); });
it("Won't apply changes without a valid argument", () => {
const pass = new PKPass({}, {});
// @ts-expect-error
pass.setExpirationDate();
expect(pass.props["expirationDate"]).toBe(undefined);
// @ts-expect-error
pass.setExpirationDate(42);
expect(pass.props["expirationDate"]).toBe(undefined);
});
it("expects a Date object as the only argument", () => { it("expects a Date object as the only argument", () => {
const pass = new PKPass({}, {});
pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0)); pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0));
// Month starts from 0 in Date Object when used this way, therefore // Month starts from 0 in Date Object when used this way, therefore
// we expect one month more // we expect one month more
expect(pass.props["expirationDate"]).toBe("2020-07-01T00:00:00Z"); expect(pass.props["expirationDate"]).toBe("2020-07-01T00:00:00Z");
}); });
it("An invalid date, will not apply changes", () => { it("should throw if an invalid date is received", () => {
const pass = new PKPass({}, {}); // @ts-expect-error
expect(() => pass.setExpirationDate("32/18/228317")).toThrowError(
TypeError,
"Cannot set expirationDate. Invalid date 32/18/228317",
);
expect(() => pass.setExpirationDate(undefined)).toThrowError(
TypeError,
"Cannot set expirationDate. Invalid date undefined",
);
// @ts-expect-error // @ts-expect-error
pass.setExpirationDate("32/18/228317"); expect(() => pass.setExpirationDate(5)).toThrowError(
expect(pass.props["expirationDate"]).toBe(undefined); TypeError,
"Cannot set expirationDate. Invalid date 5",
);
// @ts-expect-error // @ts-expect-error
pass.setExpirationDate("32/18/228317"); expect(() => pass.setExpirationDate({})).toThrowError(
expect(pass.props["expirationDate"]).toBe(undefined); TypeError,
"Cannot set expirationDate. Invalid date [object Object]",
);
});
it("should always return undefined", () => {
expect(pass.setExpirationDate(null)).toBeUndefined();
expect(
pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0)),
).toBeUndefined();
}); });
}); });
describe("setRelevantDate", () => { describe("setRelevantDate", () => {
it("should reset instance.props['relevantDate'] if 'null' is passed as value", () => { it("should reset instance.props['relevantDate'] if 'null' is passed as value", () => {
const pass = new PKPass({}, {});
pass.setRelevantDate(new Date(2020, 6, 1, 0, 0, 0, 0)); pass.setRelevantDate(new Date(2020, 6, 1, 0, 0, 0, 0));
// Month starts from 0 in Date Object when used this way, therefore // Month starts from 0 in Date Object when used this way, therefore
// we expect one month more // we expect one month more
@@ -194,42 +233,46 @@ describe("PKPass", () => {
expect(pass.props["relevantDate"]).toBeUndefined(); expect(pass.props["relevantDate"]).toBeUndefined();
}); });
it("Won't apply changes without a valid argument", () => {
const pass = new PKPass({}, {});
// @ts-expect-error
pass.setRelevantDate();
expect(pass.props["relevantDate"]).toBe(undefined);
// @ts-expect-error
pass.setRelevantDate(42);
expect(pass.props["relevantDate"]).toBe(undefined);
});
it("expects a Date object as the only argument", () => { it("expects a Date object as the only argument", () => {
const pass = new PKPass({}, {});
pass.setRelevantDate(new Date("10-04-2021")); pass.setRelevantDate(new Date("10-04-2021"));
expect(pass.props["relevantDate"]).toBe("2021-10-04T00:00:00Z"); expect(pass.props["relevantDate"]).toBe("2021-10-04T00:00:00Z");
}); });
it("An invalid date, will not apply changes", () => { it("should throw if an invalid date is received", () => {
const pass = new PKPass({}, {}); // @ts-expect-error
expect(() => pass.setRelevantDate("32/18/228317")).toThrowError(
TypeError,
"Cannot set relevantDate. Invalid date 32/18/228317",
);
expect(() => pass.setRelevantDate(undefined)).toThrowError(
TypeError,
"Cannot set relevantDate. Invalid date undefined",
);
// @ts-expect-error // @ts-expect-error
pass.setRelevantDate("32/18/228317"); expect(() => pass.setRelevantDate(5)).toThrowError(
expect(pass.props["relevantDate"]).toBe(undefined); TypeError,
"Cannot set relevantDate. Invalid date 5",
);
// @ts-expect-error // @ts-expect-error
pass.setRelevantDate("32/18/228317"); expect(() => pass.setRelevantDate({})).toThrowError(
expect(pass.props["relevantDate"]).toBe(undefined); TypeError,
"Cannot set relevantDate. Invalid date [object Object]",
);
});
it("should always return undefined", () => {
expect(pass.setRelevantDate(null)).toBeUndefined();
expect(
pass.setRelevantDate(new Date(2020, 6, 1, 0, 0, 0, 0)),
).toBeUndefined();
}); });
}); });
describe("setBarcodes", () => { describe("setBarcodes", () => {
it("shouldn't apply changes if no data is passed", () => { it("shouldn't apply changes if no data is passed", () => {
const pass = new PKPass({}, {});
const props = pass.props["barcodes"] || []; const props = pass.props["barcodes"] || [];
const oldAmountOfBarcodes = props?.length ?? 0; const oldAmountOfBarcodes = props?.length ?? 0;
@@ -240,8 +283,6 @@ describe("PKPass", () => {
}); });
it("should throw error if a boolean parameter is received", () => { it("should throw error if a boolean parameter is received", () => {
const pass = new PKPass({}, {});
// @ts-expect-error // @ts-expect-error
expect(() => pass.setBarcodes(true)).toThrowError( expect(() => pass.setBarcodes(true)).toThrowError(
TypeError, TypeError,
@@ -250,8 +291,6 @@ describe("PKPass", () => {
}); });
it("should ignore if a number parameter is received", () => { it("should ignore if a number parameter is received", () => {
const pass = new PKPass({}, {});
// @ts-expect-error // @ts-expect-error
expect(() => pass.setBarcodes(42)).toThrowError( expect(() => pass.setBarcodes(42)).toThrowError(
TypeError, TypeError,
@@ -260,15 +299,11 @@ describe("PKPass", () => {
}); });
it("should autogenerate all the barcodes objects if a string is provided as message", () => { it("should autogenerate all the barcodes objects if a string is provided as message", () => {
const pass = new PKPass({}, {});
pass.setBarcodes("28363516282"); pass.setBarcodes("28363516282");
expect(pass.props["barcodes"].length).toBe(4); expect(pass.props["barcodes"].length).toBe(4);
}); });
it("should save changes if object conforming to Schemas.Barcode are provided", () => { it("should save changes if object conforming to Schemas.Barcode are provided", () => {
const pass = new PKPass({}, {});
pass.setBarcodes({ pass.setBarcodes({
message: "28363516282", message: "28363516282",
format: "PKBarcodeFormatPDF417", format: "PKBarcodeFormatPDF417",
@@ -279,8 +314,6 @@ describe("PKPass", () => {
}); });
it("should add 'messageEncoding' if missing in valid Schema.Barcode parameters", () => { it("should add 'messageEncoding' if missing in valid Schema.Barcode parameters", () => {
const pass = new PKPass({}, {});
pass.setBarcodes({ pass.setBarcodes({
message: "28363516282", message: "28363516282",
format: "PKBarcodeFormatPDF417", format: "PKBarcodeFormatPDF417",
@@ -292,8 +325,6 @@ describe("PKPass", () => {
}); });
it("should ignore objects without 'message' property in Schema.Barcode", () => { it("should ignore objects without 'message' property in Schema.Barcode", () => {
const pass = new PKPass({}, {});
pass.setBarcodes( pass.setBarcodes(
{ {
format: "PKBarcodeFormatCode128", format: "PKBarcodeFormatCode128",
@@ -309,8 +340,6 @@ describe("PKPass", () => {
}); });
it("should ignore objects and values that not comply with Schema.Barcodes", () => { it("should ignore objects and values that not comply with Schema.Barcodes", () => {
const pass = new PKPass({}, {});
pass.setBarcodes( pass.setBarcodes(
// @ts-expect-error // @ts-expect-error
5, 5,
@@ -328,8 +357,6 @@ describe("PKPass", () => {
}); });
it("should reset barcodes content if parameter is null", () => { it("should reset barcodes content if parameter is null", () => {
const pass = new PKPass({}, {});
pass.setBarcodes({ pass.setBarcodes({
message: "28363516282", message: "28363516282",
format: "PKBarcodeFormatPDF417", format: "PKBarcodeFormatPDF417",
@@ -341,6 +368,17 @@ describe("PKPass", () => {
pass.setBarcodes(null); pass.setBarcodes(null);
expect(pass.props["barcodes"]).toBe(undefined); expect(pass.props["barcodes"]).toBe(undefined);
}); });
it("should always return undefined", () => {
expect(pass.setBarcodes(null)).toBeUndefined();
expect(
pass.setBarcodes({
message: "28363516282",
format: "PKBarcodeFormatPDF417",
messageEncoding: "utf8",
}),
).toBeUndefined();
});
}); });
describe("transitType", () => { describe("transitType", () => {
@@ -361,6 +399,12 @@ describe("PKPass", () => {
{ {
"pass.json": mockBPPassJSON, "pass.json": mockBPPassJSON,
}, },
{
signerCert: "",
signerKey: "",
wwdr: "",
signerKeyPassphrase: "p477w0rb",
},
{}, {},
); );
@@ -368,34 +412,58 @@ describe("PKPass", () => {
{ {
"pass.json": mockCPPassJSON, "pass.json": mockCPPassJSON,
}, },
{
signerCert: "",
signerKey: "",
wwdr: "",
signerKeyPassphrase: "p477w0rb",
},
{}, {},
); );
/** passBP.transitType = "PKTransitTypeAir";
* @TODO fix this test when props setup expect(passBP.transitType).toBe("PKTransitTypeAir");
* will be complete
*/
/* expect(() => { 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"), () => (passCP.transitType = "PKTransitTypeAir"),
).toThrowError( ).toThrowError(
TypeError, TypeError,
"Cannot set transitType on a pass with type different from 'boardingPass'.", "Cannot set transitType on a pass with type different from 'boardingPass'.",
); );
expect(passCP.transitType).toBeUndefined(); */ expect(passCP.transitType).toBeUndefined();
}); });
}); });
describe("localize", () => { describe("localize", () => {
const pass = new PKPass({}, {}, {}); it("should fail throw if lang is not a string", () => {
expect(() => pass.localize(null)).toThrowError(
TypeError,
"Cannot set localization. Expected a string for 'lang' but received a object",
);
expect(() => pass.localize(undefined)).toThrowError(
TypeError,
"Cannot set localization. Expected a string for 'lang' but received a undefined",
);
// @ts-expect-error
expect(() => pass.localize(5)).toThrowError(
TypeError,
"Cannot set localization. Expected a string for 'lang' but received a number",
);
// @ts-expect-error
expect(() => pass.localize(true)).toThrowError(
TypeError,
"Cannot set localization. Expected a string for 'lang' but received a boolean",
);
// @ts-expect-error
expect(() => pass.localize({})).toThrowError(
TypeError,
"Cannot set localization. Expected a string for 'lang' but received a object",
);
});
it("should create a new language record inside class props", () => { it("should create a new language record inside class props", () => {
pass.localize("en"); pass.localize("en");
@@ -403,19 +471,12 @@ describe("PKPass", () => {
expect(pass[localizationSymbol]["en"]).toEqual({}); expect(pass[localizationSymbol]["en"]).toEqual({});
}); });
it("should save some translations to be exported later", () => { it("should accept later translations and merge them with existing ones", () => {
pass.localize("it", { pass.localize("it", {
say_hi: "ciao", say_hi: "ciao",
say_gb: "arrivederci", say_gb: "arrivederci",
}); });
expect(pass[localizationSymbol]["it"]).toEqual({
say_hi: "ciao",
say_gb: "arrivederci",
});
});
it("should accept later translations and merge them with existing ones", () => {
pass.localize("it", { pass.localize("it", {
say_good_morning: "buongiorno", say_good_morning: "buongiorno",
say_good_evening: "buonasera", say_good_evening: "buonasera",
@@ -436,5 +497,11 @@ describe("PKPass", () => {
expect(pass[localizationSymbol]["it"]).toBeUndefined(); expect(pass[localizationSymbol]["it"]).toBeUndefined();
expect(pass[localizationSymbol]["en"]).toBeUndefined(); expect(pass[localizationSymbol]["en"]).toBeUndefined();
}); });
it("should always return undefined", () => {
expect(pass.localize("it", undefined)).toBeUndefined();
expect(pass.localize("it", null)).toBeUndefined();
expect(pass.localize("it", {})).toBeUndefined();
});
}); });
}); });

View File

@@ -1,11 +1,11 @@
import { createPass, Pass } from ".."; /* import { createPass, Pass } from "..";
import * as path from "path"; import * as path from "path";
*/
/** /**
* Tests created upon Jasmine testing suite. * Tests created upon Jasmine testing suite.
*/ */
describe("Passkit-generator", function () { /*describe("Passkit-generator", function () {
let pass: Pass; let pass: Pass;
beforeEach(async () => { beforeEach(async () => {
pass = await createPass({ pass = await createPass({
@@ -315,4 +315,4 @@ describe("Passkit-generator", function () {
expect(pass.props["barcode"]).toEqual(oldBarcode); expect(pass.props["barcode"]).toEqual(oldBarcode);
}); });
}); });
}); });*/

View File

@@ -51,7 +51,7 @@ export default class Bundle {
mimeType: `${Mime.type}/${Mime.subtype}`, mimeType: `${Mime.type}/${Mime.subtype}`,
): [Bundle, Function] { ): [Bundle, Function] {
const bundle = new Bundle(mimeType); const bundle = new Bundle(mimeType);
return [bundle, bundle[freezeSymbol]]; return [bundle, () => bundle[freezeSymbol]()];
} }
/** /**
@@ -69,8 +69,8 @@ export default class Bundle {
} }
/** /**
* Tells if this bundle still allows files to be added * Tells if this bundle still allows files to be added.
* @returns * @returns false if files are allowed, true otherwise
*/ */
public get isFrozen() { public get isFrozen() {

View File

@@ -1,12 +1,24 @@
import Joi from "joi"; import Joi from "joi";
import { Field } from "./PassFieldContent"; import { Field } from "./PassFieldContent";
export type TransitType =
| "PKTransitTypeAir"
| "PKTransitTypeBoat"
| "PKTransitTypeBus"
| "PKTransitTypeGeneric"
| "PKTransitTypeTrain";
export const TransitType = Joi.string().regex(
/(PKTransitTypeAir|PKTransitTypeBoat|PKTransitTypeBus|PKTransitTypeGeneric|PKTransitTypeTrain)/,
);
export interface PassFields { export interface PassFields {
auxiliaryFields: (Field & { row?: number })[]; auxiliaryFields: (Field & { row?: number })[];
backFields: Field[]; backFields: Field[];
headerFields: Field[]; headerFields: Field[];
primaryFields: Field[]; primaryFields: Field[];
secondaryFields: Field[]; secondaryFields: Field[];
transitType?: TransitType;
} }
export const PassFields = Joi.object<PassFields>().keys({ export const PassFields = Joi.object<PassFields>().keys({
@@ -21,15 +33,5 @@ export const PassFields = Joi.object<PassFields>().keys({
headerFields: Joi.array().items(Field), headerFields: Joi.array().items(Field),
primaryFields: Joi.array().items(Field), primaryFields: Joi.array().items(Field),
secondaryFields: Joi.array().items(Field), secondaryFields: Joi.array().items(Field),
transitType: TransitType,
}); });
export type TransitType =
| "PKTransitTypeAir"
| "PKTransitTypeBoat"
| "PKTransitTypeBus"
| "PKTransitTypeGeneric"
| "PKTransitTypeTrain";
export const TransitType = Joi.string().regex(
/(PKTransitTypeAir|PKTransitTypeBoat|PKTransitTypeBus|PKTransitTypeGeneric|PKTransitTypeTrain)/,
);

View File

@@ -103,13 +103,11 @@ export const PassPropsFromMethods = Joi.object<PassPropsFromMethods>({
}); });
export const PassKindsProps = Joi.object<PassKindsProps>({ export const PassKindsProps = Joi.object<PassKindsProps>({
coupon: Joi.array().items(Field), coupon: PassFields.disallow("transitType"),
generic: Joi.array().items(Field), generic: PassFields.disallow("transitType"),
storeCard: Joi.array().items(Field), storeCard: PassFields.disallow("transitType"),
eventTicket: Joi.array().items(Field), eventTicket: PassFields.disallow("transitType"),
boardingPass: Joi.array().items( boardingPass: PassFields,
Field.concat(Joi.object({ transitType: TransitType })),
),
}); });
export const PassType = Joi.string().regex( export const PassType = Joi.string().regex(
@@ -144,11 +142,10 @@ export const OverridablePassProps = Joi.object<OverridablePassProps>({
export const PassProps = Joi.object< export const PassProps = Joi.object<
OverridablePassProps & PassKindsProps & PassPropsFromMethods OverridablePassProps & PassKindsProps & PassPropsFromMethods
>({ >()
...OverridablePassProps, .concat(OverridablePassProps)
...PassKindsProps, .concat(PassKindsProps)
...PassPropsFromMethods, .concat(PassPropsFromMethods);
});
export interface Template { export interface Template {
model: string; model: string;