208 lines
6.1 KiB
JavaScript
208 lines
6.1 KiB
JavaScript
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, resolveSearchBreadcrumbLabel, isDnsRoutePath, resolveCaseBreadcrumbState };\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"
|
|
});
|
|
});
|
|
|
|
test("routing/routeState resolves breadcrumb label using key and route flags", async () => {
|
|
const mod = loadRouteStateModule();
|
|
const getViewAllLabel = (key) =>
|
|
({ myCases: "My Cases", watchedCases: "Watched Cases" })[key] || null;
|
|
|
|
const fromViewAll = mod.resolveSearchBreadcrumbLabel({
|
|
query: { va: "true", key: "myCases" },
|
|
getViewAllLabel,
|
|
advancedLabel: "Advanced",
|
|
addressLabel: "Address",
|
|
defaultLabel: "Search"
|
|
});
|
|
const fromAdvanced = mod.resolveSearchBreadcrumbLabel({
|
|
query: { adv: "true" },
|
|
getViewAllLabel,
|
|
advancedLabel: "Advanced",
|
|
addressLabel: "Address",
|
|
defaultLabel: "Search"
|
|
});
|
|
const fromDefault = mod.resolveSearchBreadcrumbLabel({
|
|
query: {},
|
|
getViewAllLabel,
|
|
advancedLabel: "Advanced",
|
|
addressLabel: "Address",
|
|
defaultLabel: "Search"
|
|
});
|
|
|
|
assert.strictEqual(fromViewAll, "My Cases");
|
|
assert.strictEqual(fromAdvanced, "Advanced");
|
|
assert.strictEqual(fromDefault, "Search");
|
|
});
|
|
|
|
test("routing/routeState identifies dns and myportal dns route prefixes", async () => {
|
|
const mod = loadRouteStateModule();
|
|
|
|
assert.strictEqual(mod.isDnsRoutePath("/dns"), true);
|
|
assert.strictEqual(mod.isDnsRoutePath("/dns/development"), true);
|
|
assert.strictEqual(mod.isDnsRoutePath("/myportal/dns"), true);
|
|
assert.strictEqual(mod.isDnsRoutePath("/myportal/dns/applications"), true);
|
|
assert.strictEqual(mod.isDnsRoutePath("/case/123"), false);
|
|
});
|
|
|
|
test("routing/routeState resolves bundled case breadcrumb state", async () => {
|
|
const mod = loadRouteStateModule();
|
|
const getViewAllLabel = (key) => ({ myCases: "My Cases" })[key] || null;
|
|
|
|
const state = mod.resolveCaseBreadcrumbState({
|
|
query: { va: "true", key: "myCases" },
|
|
hasSession: true,
|
|
getViewAllLabel,
|
|
advancedLabel: "Advanced",
|
|
addressLabel: "Address",
|
|
defaultLabel: "Search"
|
|
});
|
|
|
|
assert.deepStrictEqual(JSON.parse(JSON.stringify(state)), {
|
|
breadcrumbHref: {
|
|
pathname: "/myportal/viewall",
|
|
query: { key: "myCases" }
|
|
},
|
|
caseResultsHref: {
|
|
pathname: "/myportal/searchresults",
|
|
query: { va: "true", key: "myCases" }
|
|
},
|
|
breadcrumbLabel: "My Cases"
|
|
});
|
|
});
|
|
|
|
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);
|
|
});
|
|
}
|