mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 19:25:23 +00:00
Added tests for addBuffer and improved handling of empty strings files
This commit is contained in:
113
spec/PKPass.ts
113
spec/PKPass.ts
@@ -1,3 +1,4 @@
|
|||||||
|
import { filesSymbol } from "../lib/Bundle";
|
||||||
import {
|
import {
|
||||||
default as PKPass,
|
default as PKPass,
|
||||||
localizationSymbol,
|
localizationSymbol,
|
||||||
@@ -524,4 +525,116 @@ describe("PKPass", () => {
|
|||||||
expect(pass.localize("it", {})).toBeUndefined();
|
expect(pass.localize("it", {})).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("addBuffer", () => {
|
||||||
|
it("should filter out silently manifest and signature files", () => {
|
||||||
|
pass.addBuffer("manifest.json", Buffer.alloc(0));
|
||||||
|
pass.addBuffer("signature", Buffer.alloc(0));
|
||||||
|
|
||||||
|
expect(Object.keys(pass[filesSymbol]).length).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should accept a pass.json only if not yet imported", () => {
|
||||||
|
pass.addBuffer(
|
||||||
|
"pass.json",
|
||||||
|
Buffer.from(
|
||||||
|
JSON.stringify({
|
||||||
|
boardingPass: {},
|
||||||
|
serialNumber: "555555",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(Object.keys(pass[filesSymbol]).length).toBe(1);
|
||||||
|
|
||||||
|
/** Adding it again */
|
||||||
|
|
||||||
|
pass.addBuffer(
|
||||||
|
"pass.json",
|
||||||
|
Buffer.from(
|
||||||
|
JSON.stringify({
|
||||||
|
boardingPass: {},
|
||||||
|
serialNumber: "555555",
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
/** Expecting it to get ignored */
|
||||||
|
expect(Object.keys(pass[filesSymbol]).length).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should accept personalization.json only if it is a valid JSON", () => {
|
||||||
|
pass.addBuffer(
|
||||||
|
"personalization.json",
|
||||||
|
Buffer.from(
|
||||||
|
JSON.stringify({
|
||||||
|
description:
|
||||||
|
"A test description for a test personalization",
|
||||||
|
requiredPersonalizationFields: [
|
||||||
|
"PKPassPersonalizationFieldName",
|
||||||
|
"PKPassPersonalizationFieldPostalCode",
|
||||||
|
"PKPassPersonalizationFieldEmailAddress",
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(pass[filesSymbol]["personalization.json"]).toBeInstanceOf(
|
||||||
|
Buffer,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject invalid personalization.json", () => {
|
||||||
|
pass.addBuffer(
|
||||||
|
"personalization.json",
|
||||||
|
Buffer.from(
|
||||||
|
JSON.stringify({
|
||||||
|
requiredPersonalizationFields: [
|
||||||
|
"PKPassPersonalizationFieldName",
|
||||||
|
"PKPassPersonalizationFieldEmailAddressaworng",
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(pass[filesSymbol]["personalization.json"]).toBeUndefined(
|
||||||
|
Buffer,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should redirect .strings files to localization", () => {
|
||||||
|
const validTranslationStrings = `
|
||||||
|
/* Insert Element menu item */
|
||||||
|
"Insert Element" = "Insert Element";
|
||||||
|
/* Error string used for unknown error types. */
|
||||||
|
"ErrorString_1" = "An unknown error occurred.";
|
||||||
|
`;
|
||||||
|
|
||||||
|
pass.addBuffer(
|
||||||
|
"en.lproj/pass.strings",
|
||||||
|
Buffer.from(validTranslationStrings),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(pass[filesSymbol]["en.lproj/pass.string"]).toBeUndefined();
|
||||||
|
expect(pass[localizationSymbol]["en"]).toEqual({
|
||||||
|
"Insert Element": "Insert Element",
|
||||||
|
ErrorString_1: "An unknown error occurred.",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should ignore invalid .strings files", () => {
|
||||||
|
const invalidTranslationStrings = `
|
||||||
|
"Insert Element"="Insert Element
|
||||||
|
"ErrorString_1= "An unknown error occurred."
|
||||||
|
`;
|
||||||
|
|
||||||
|
pass.addBuffer(
|
||||||
|
"en.lproj/pass.strings",
|
||||||
|
Buffer.from(invalidTranslationStrings),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(pass[filesSymbol]["en.lproj/pass.string"]).toBeUndefined();
|
||||||
|
expect(pass[localizationSymbol]["en"]).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -403,9 +403,15 @@ export default class PKPass extends Bundle {
|
|||||||
if ((match = pathName.match(translationsFileRegexp))) {
|
if ((match = pathName.match(translationsFileRegexp))) {
|
||||||
const [, lang] = match;
|
const [, lang] = match;
|
||||||
|
|
||||||
|
const parsedTranslations = Strings.parse(buffer).translations;
|
||||||
|
|
||||||
|
if (!parsedTranslations.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Object.assign(
|
Object.assign(
|
||||||
(this[localizationSymbol][lang] ??= {}),
|
(this[localizationSymbol][lang] ??= {}),
|
||||||
Object.fromEntries(Strings.parse(buffer).translations),
|
Object.fromEntries(parsedTranslations),
|
||||||
);
|
);
|
||||||
|
|
||||||
return;
|
return;
|
||||||
|
|||||||
Reference in New Issue
Block a user