@@ -30,7 +30,7 @@ test("case/getPortalModuleDetails uses BASE_URL route and encoded case reference
|
||||
assert.deepStrictEqual(normalize(result), { value: [] });
|
||||
assert.strictEqual(
|
||||
axios.calls[0].url,
|
||||
"http://example.local/api/endpoint/getportalmoduledetails_api?appealType=appeal&caseReference=REF%20A/B"
|
||||
"http://example.local/api/myportal/get-portal-module-details?appealType=appeal&caseReference=REF%20A/B"
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
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();
|
||||
@@ -101,7 +101,7 @@ test("portal/createWatchedCases posts to harmonized route helper URL", async ()
|
||||
assert.strictEqual(requestCalls[0].method, "post");
|
||||
assert.strictEqual(
|
||||
requestCalls[0].url,
|
||||
"/api/endpoint/createwatchedcases_api"
|
||||
"/api/subscriptions/create-watched-case"
|
||||
);
|
||||
assert.deepStrictEqual(normalize(requestCalls[0].data), payload);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user