182 lines
4.3 KiB
JavaScript
182 lines
4.3 KiB
JavaScript
const assert = require("assert");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const vm = require("vm");
|
|
const { JSONPath } = require("jsonpath-plus");
|
|
|
|
const tests = [];
|
|
const test = (name, fn) => tests.push({ name, fn });
|
|
|
|
const helperModulePath = path.join(
|
|
__dirname,
|
|
"..",
|
|
"..",
|
|
"lib",
|
|
"i18n",
|
|
"crmDisplay",
|
|
"resolveCrmDisplayValue.js"
|
|
);
|
|
|
|
const lookupTranslations = require(
|
|
path.join(__dirname, "..", "..", "data", "lookuptranslations.json")
|
|
);
|
|
|
|
const loadHelper = () => {
|
|
const source = fs.readFileSync(helperModulePath, "utf8");
|
|
|
|
const transformedSource = `${source
|
|
.replace(
|
|
/import\s+\{\s*JSONPath\s+as\s+jsonpath\s*\}\s+from\s+"jsonpath-plus";/,
|
|
'const { JSONPath: jsonpath } = require("jsonpath-plus");'
|
|
)
|
|
.replace(/export const /g, "const ")
|
|
.replace(/export default resolveCrmDisplayValue;/g, "")}
|
|
|
|
module.exports = {
|
|
resolveCrmDisplayValue
|
|
};
|
|
`;
|
|
|
|
const sandbox = {
|
|
module: { exports: {} },
|
|
exports: {},
|
|
require: (modulePath) => {
|
|
if (modulePath === "jsonpath-plus") {
|
|
return { JSONPath };
|
|
}
|
|
|
|
return require(modulePath);
|
|
},
|
|
__dirname: path.dirname(helperModulePath),
|
|
__filename: helperModulePath,
|
|
console
|
|
};
|
|
|
|
vm.runInNewContext(transformedSource, sandbox, {
|
|
filename: helperModulePath
|
|
});
|
|
|
|
return sandbox.module.exports;
|
|
};
|
|
|
|
test("English with value returns value", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: "Cardiff Council",
|
|
locale: "en",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "Cardiff Council"
|
|
}),
|
|
"Cardiff Council"
|
|
);
|
|
});
|
|
|
|
test("Welsh with translation returns Welsh", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: "Cardiff Council",
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "Cardiff Council"
|
|
}),
|
|
"Cyngor Caerdydd"
|
|
);
|
|
});
|
|
|
|
test("Welsh without translation returns English value", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: "Not In Lookup",
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "To be confirmed"
|
|
}),
|
|
"Not In Lookup"
|
|
);
|
|
});
|
|
|
|
test("Missing value returns fallback", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "N/A"
|
|
}),
|
|
"N/A"
|
|
);
|
|
});
|
|
|
|
test("Empty string returns fallback", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: "",
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "To be confirmed"
|
|
}),
|
|
"To be confirmed"
|
|
);
|
|
});
|
|
|
|
test("Null returns fallback", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: null,
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "To be confirmed"
|
|
}),
|
|
"To be confirmed"
|
|
);
|
|
});
|
|
|
|
test("Undefined returns fallback", () => {
|
|
const { resolveCrmDisplayValue } = loadHelper();
|
|
|
|
assert.strictEqual(
|
|
resolveCrmDisplayValue({
|
|
value: undefined,
|
|
locale: "cy",
|
|
translations: lookupTranslations,
|
|
fallbackValue: "N/A"
|
|
}),
|
|
"N/A"
|
|
);
|
|
});
|
|
|
|
async function run() {
|
|
let failures = 0;
|
|
|
|
for (const { name, fn } of tests) {
|
|
try {
|
|
await fn();
|
|
console.log(`✓ ${name}`);
|
|
} catch (error) {
|
|
failures += 1;
|
|
console.error(`✗ ${name}`);
|
|
console.error(error);
|
|
}
|
|
}
|
|
|
|
if (failures > 0) {
|
|
process.exitCode = 1;
|
|
return;
|
|
}
|
|
|
|
console.log(`Passed ${tests.length} CRM display resolver tests.`);
|
|
}
|
|
|
|
run();
|