branchSwitch.test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. readRouteBranchFromPathname,
  5. isSearchRoutePathname,
  6. replaceBranchInPathnameOrFallback,
  7. buildNextSearchQueryString,
  8. buildNextUrlForBranchSwitch,
  9. } from "./branchSwitch.js";
  10. describe("lib/frontend/quickNav/branchSwitch", () => {
  11. it("readRouteBranchFromPathname returns the first segment when valid", () => {
  12. expect(readRouteBranchFromPathname("/NL01")).toBe("NL01");
  13. expect(readRouteBranchFromPathname("/NL01/2025/12/31")).toBe("NL01");
  14. expect(readRouteBranchFromPathname("/search")).toBe(null);
  15. expect(readRouteBranchFromPathname("/nl01")).toBe(null);
  16. });
  17. it("isSearchRoutePathname detects /:branch/search", () => {
  18. expect(isSearchRoutePathname("/NL01/search")).toBe(true);
  19. expect(isSearchRoutePathname("/NL01/search/extra")).toBe(true);
  20. expect(isSearchRoutePathname("/NL01")).toBe(false);
  21. expect(isSearchRoutePathname("/search")).toBe(false);
  22. });
  23. it("replaceBranchInPathnameOrFallback replaces only the first segment", () => {
  24. expect(replaceBranchInPathnameOrFallback("/NL01/2025/12/31", "NL20")).toBe(
  25. "/NL20/2025/12/31"
  26. );
  27. // No valid branch -> fallback to branch root
  28. expect(replaceBranchInPathnameOrFallback("/search", "NL20")).toBe("/NL20");
  29. });
  30. it("buildNextSearchQueryString drops legacy SINGLE branch= and keeps other params", () => {
  31. const qs = buildNextSearchQueryString({
  32. currentSearch: "?q=x&branch=NL01&limit=200",
  33. currentRouteBranch: "NL01",
  34. nextBranch: "NL20",
  35. });
  36. expect(qs).toBe("?q=x&limit=200");
  37. });
  38. it("buildNextSearchQueryString keeps MULTI branches (sorted deterministically)", () => {
  39. const qs = buildNextSearchQueryString({
  40. currentSearch: "?q=x&scope=multi&branches=NL20,NL06",
  41. currentRouteBranch: "NL01",
  42. nextBranch: "NL02",
  43. });
  44. expect(qs).toBe("?q=x&scope=multi&branches=NL06%2CNL20");
  45. });
  46. it("buildNextUrlForBranchSwitch preserves deep path and normalizes Search URLs", () => {
  47. expect(
  48. buildNextUrlForBranchSwitch({
  49. pathname: "/NL01/2025/12/31",
  50. search: "",
  51. nextBranch: "NL20",
  52. })
  53. ).toBe("/NL20/2025/12/31");
  54. expect(
  55. buildNextUrlForBranchSwitch({
  56. pathname: "/NL01/search",
  57. search: "?q=x&branch=NL01&limit=200",
  58. nextBranch: "NL20",
  59. })
  60. ).toBe("/NL20/search?q=x&limit=200");
  61. });
  62. });