22541 extract breadcrumb route-state helper and align phase22 auth locale test
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const vm = require("vm");
|
||||
const assert = require("assert");
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..", "..");
|
||||
|
||||
const loadRouteStateModule = () => {
|
||||
const filePath = path.join(rootDir, "lib", "routing", "routeState.js");
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source +=
|
||||
"\nmodule.exports = { normalizeRouteStateQuery, resolveSearchResultsHref };\n";
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const tests = [];
|
||||
const test = (name, fn) => tests.push({ name, fn });
|
||||
|
||||
test("routing/routeState normalizes va/adv/ads flags and key", async () => {
|
||||
const mod = loadRouteStateModule();
|
||||
|
||||
const normalized = mod.normalizeRouteStateQuery({
|
||||
va: "true",
|
||||
adv: "false",
|
||||
ads: "true",
|
||||
key: "myCases"
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(normalized)), {
|
||||
viewAll: true,
|
||||
advanced: false,
|
||||
address: true,
|
||||
key: "myCases"
|
||||
});
|
||||
});
|
||||
|
||||
test("routing/routeState resolves view-all href when va=true", async () => {
|
||||
const mod = loadRouteStateModule();
|
||||
|
||||
const href = mod.resolveSearchResultsHref({
|
||||
query: { va: "true", key: "myCases", adv: "true" },
|
||||
hasSession: true
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(href)), {
|
||||
pathname: "/myportal/viewall",
|
||||
query: { key: "myCases" }
|
||||
});
|
||||
});
|
||||
|
||||
test("routing/routeState resolves advanced/address/default result hrefs", async () => {
|
||||
const mod = loadRouteStateModule();
|
||||
|
||||
const advancedHref = mod.resolveSearchResultsHref({
|
||||
query: { adv: "true", q: "abc" },
|
||||
hasSession: false
|
||||
});
|
||||
const addressHref = mod.resolveSearchResultsHref({
|
||||
query: { ads: "true", q: "abc" },
|
||||
hasSession: true
|
||||
});
|
||||
const defaultHref = mod.resolveSearchResultsHref({
|
||||
query: { q: "abc" },
|
||||
hasSession: true
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(advancedHref)), {
|
||||
pathname: "/advancedsearchresults",
|
||||
query: { adv: "true", q: "abc" }
|
||||
});
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(addressHref)), {
|
||||
pathname: "/myportal/addresssearchresults",
|
||||
query: { ads: "true", q: "abc" }
|
||||
});
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(defaultHref)), {
|
||||
pathname: "/myportal/searchresults",
|
||||
query: { q: "abc" }
|
||||
});
|
||||
});
|
||||
|
||||
test("routing/routeState supports fallback to /myportal when no route flags", async () => {
|
||||
const mod = loadRouteStateModule();
|
||||
|
||||
const href = mod.resolveSearchResultsHref({
|
||||
query: { q: "abc" },
|
||||
hasSession: false,
|
||||
fallbackToMyPortalWhenNoFlags: true
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(href)), {
|
||||
pathname: "/myportal"
|
||||
});
|
||||
});
|
||||
|
||||
test("routing/routeState supports DNS route override", async () => {
|
||||
const mod = loadRouteStateModule();
|
||||
|
||||
const href = mod.resolveSearchResultsHref({
|
||||
query: { adv: "true", q: "abc" },
|
||||
hasSession: true,
|
||||
isDnsRoute: true
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(JSON.parse(JSON.stringify(href)), {
|
||||
pathname: "/myportal/dnsapplications"
|
||||
});
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 22 route-state helper tests passed (${passed}/${tests.length}).`
|
||||
);
|
||||
};
|
||||
|
||||
module.exports = run;
|
||||
|
||||
if (require.main === module) {
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user