detail-navigation.test.mjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import test from "node:test";
  2. import assert from "node:assert/strict";
  3. import { detailBack, detailLinkState, formBack } from "../src/lib/detail-navigation.ts";
  4. test("列表到详情再返回保留筛选条件与角色菜单名称", () => {
  5. assert.deepEqual(detailBack({ from: "/contracts?q=CL&status=生效中" }, "/contracts", "返回合同清单"), { to: "/contracts?q=CL&status=生效中", state: null, label: "返回合同清单" });
  6. assert.equal(detailBack({ from: "/workspace/progress?status=已完成" }, "/work-orders", "返回工单列表").label, "返回工单进度");
  7. assert.equal(detailBack({ from: "/workspace/queue?q=LG" }, "/work-orders", "返回工单列表").to, "/workspace/queue?q=LG");
  8. assert.equal(detailBack({ from: "/workspace/plan" }, "/orders", "返回订单管理").label, "返回我的套餐");
  9. // 页码在 URL 上:返回时连页带筛选一起送回(⛔ 退回第一页)
  10. assert.equal(detailBack({ from: "/customers?query=跨境&page=3" }, "/customers", "返回客户管理").to, "/customers?query=跨境&page=3");
  11. });
  12. test("合同、客户、订单详情逐级返回保留前一级历史", () => {
  13. const list = { from: "/contracts?q=CL" };
  14. const contract = detailLinkState("/contracts/HT-CL", list);
  15. const customer = detailLinkState("/customers/KL-1", contract);
  16. const back = detailBack(customer, "/orders", "返回订单管理");
  17. assert.equal(back.to, "/customers/KL-1");
  18. const previous = detailBack(back.state, "/customers", "返回客户清单");
  19. assert.equal(previous.to, "/contracts/HT-CL");
  20. assert.deepEqual(previous.state, list);
  21. });
  22. test("返回路径只接受已知业务列表与详情,拒绝外部或任意路径", () => {
  23. for (const from of ["https://example.com", "//example.com", "/login", "/workspace/other", "/contracts-evil?q=x", 123, null]) {
  24. assert.equal(detailBack({ from }, "/contracts", "返回合同管理").to, "/contracts");
  25. }
  26. });
  27. const editor = (state) => formBack({ state, detail: "/customers/KL-1", detailLabel: "返回客户档案", list: "/customers", listLabel: "返回客户管理" });
  28. test("客户编辑页从客户清单进 ⇒ 返回清单并保留筛选与页码", () => {
  29. const entry = detailLinkState("/customers?query=跨境&status=正常&page=2", null);
  30. assert.deepEqual(editor(entry), { to: "/customers?query=跨境&status=正常&page=2", state: null, label: "返回客户管理" });
  31. });
  32. test("客户编辑页从客户档案进 ⇒ 返回档案,并带回档案自己的返回链", () => {
  33. // 清单 → 档案 → 编辑:返回档案后,档案的「返回」仍指向原来的清单(不是兜底路径)
  34. const detailState = detailLinkState("/customers?query=跨境", null);
  35. const entry = detailLinkState("/customers/KL-1", detailState);
  36. const back = editor(entry);
  37. assert.deepEqual(back, { to: "/customers/KL-1", state: detailState, label: "返回客户档案" });
  38. assert.deepEqual(detailBack(back.state, "/customers", "返回客户管理"), { to: "/customers?query=跨境", state: null, label: "返回客户管理" });
  39. });
  40. test("客户编辑页直接敲 URL 或无历史 ⇒ 兜底回客户档案", () => {
  41. for (const state of [null, undefined, {}, { from: "https://example.com" }, { from: "/customers-evil" }, { from: "/customers/KL-1/edit" }]) {
  42. assert.deepEqual(editor(state), { to: "/customers/KL-1", state: null, label: "返回客户档案" });
  43. }
  44. });