mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Added setExpirationDate implementation along with tests
This commit is contained in:
@@ -126,5 +126,42 @@ describe("PKPass", () => {
|
|||||||
|
|
||||||
expect(pass.props["nfc"]).toBeUndefined();
|
expect(pass.props["nfc"]).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("setExpirationDate", () => {
|
||||||
|
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", () => {
|
||||||
|
const pass = new PKPass({}, {});
|
||||||
|
|
||||||
|
pass.setExpirationDate(new Date(2020, 6, 1, 0, 0, 0, 0));
|
||||||
|
// Month starts from 0 in Date Object when used this way, therefore
|
||||||
|
// we expect one month more
|
||||||
|
expect(pass.props["expirationDate"]).toBe(
|
||||||
|
"2020-07-01T00:00:00Z",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("An invalid date, will not apply changes", () => {
|
||||||
|
const pass = new PKPass({}, {});
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
pass.setExpirationDate("32/18/228317");
|
||||||
|
expect(pass.props["expirationDate"]).toBe(undefined);
|
||||||
|
|
||||||
|
// @ts-expect-error
|
||||||
|
pass.setExpirationDate("32/18/228317");
|
||||||
|
expect(pass.props["expirationDate"]).toBe(undefined);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -256,10 +256,17 @@ export default class PKPass extends Bundle {
|
|||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
|
|
||||||
setExpiration(date: Date | null): this {
|
setExpirationDate(date: Date | null): this {
|
||||||
/**
|
if (date === null) {
|
||||||
* @TODO implement
|
delete this[propsSymbol]["expirationDate"];
|
||||||
*/
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parsedDate = processDate("expirationDate", date);
|
||||||
|
|
||||||
|
if (parsedDate) {
|
||||||
|
this[propsSymbol]["expirationDate"] = parsedDate;
|
||||||
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/pass.ts
16
src/pass.ts
@@ -16,6 +16,7 @@ import {
|
|||||||
getAllFilesWithName,
|
getAllFilesWithName,
|
||||||
} from "./utils";
|
} from "./utils";
|
||||||
import * as Signature from "./signature";
|
import * as Signature from "./signature";
|
||||||
|
import { processDate } from "./processDate";
|
||||||
|
|
||||||
const barcodeDebug = debug("passkit:barcode");
|
const barcodeDebug = debug("passkit:barcode");
|
||||||
const genericDebug = debug("passkit:generic");
|
const genericDebug = debug("passkit:generic");
|
||||||
@@ -687,18 +688,3 @@ function getValidInArray<T>(
|
|||||||
Object.keys(current).length && Schemas.isValid(current, schemaName),
|
Object.keys(current).length && Schemas.isValid(current, schemaName),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function processDate(key: string, date: Date): string | null {
|
|
||||||
if (!(date instanceof Date)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const dateParse = dateToW3CString(date);
|
|
||||||
|
|
||||||
if (!dateParse) {
|
|
||||||
genericDebug(formatMessage(DEBUG.DATE_FORMAT_UNMATCH, key));
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return dateParse;
|
|
||||||
}
|
|
||||||
|
|||||||
17
src/processDate.ts
Normal file
17
src/processDate.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { dateToW3CString } from "./utils";
|
||||||
|
import formatMessage, { ERROR, DEBUG } from "./messages";
|
||||||
|
|
||||||
|
export function processDate(key: string, date: Date): string | null {
|
||||||
|
if (!(date instanceof Date)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const dateParse = dateToW3CString(date);
|
||||||
|
|
||||||
|
if (!dateParse) {
|
||||||
|
console.warn(formatMessage(DEBUG.DATE_FORMAT_UNMATCH, key));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return dateParse;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user