pageHelpers.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import { SEARCH_SCOPE } from "@/lib/frontend/search/urlState";
  4. import {
  5. buildSearchHref,
  6. buildSearchKey,
  7. getScopeLabel,
  8. needsMultiBranchSelectionHint,
  9. buildNextStateForScopeChange,
  10. buildNextStateForToggleBranch,
  11. buildNextStateForClearAllBranches,
  12. buildHrefForSingleBranchSwitch,
  13. } from "./pageHelpers.js";
  14. describe("lib/frontend/search/pageHelpers", () => {
  15. it("buildSearchHref builds /:branch/search with query string", () => {
  16. const href = buildSearchHref({
  17. routeBranch: "NL01",
  18. state: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 200 },
  19. });
  20. expect(href).toBe("/NL01/search?q=x&limit=200");
  21. });
  22. it("buildSearchKey includes routeBranch", () => {
  23. const key = buildSearchKey({
  24. routeBranch: "NL01",
  25. urlState: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 200 },
  26. });
  27. expect(key).toBe("NL01|q=x&limit=200");
  28. });
  29. it("getScopeLabel returns correct labels", () => {
  30. expect(
  31. getScopeLabel({
  32. routeBranch: "NL01",
  33. urlState: { scope: SEARCH_SCOPE.SINGLE },
  34. })
  35. ).toBe("Niederlassung NL01");
  36. expect(
  37. getScopeLabel({
  38. routeBranch: "NL01",
  39. urlState: { scope: SEARCH_SCOPE.ALL },
  40. })
  41. ).toBe("Alle Niederlassungen");
  42. expect(
  43. getScopeLabel({
  44. routeBranch: "NL01",
  45. urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01", "NL02"] },
  46. })
  47. ).toBe("2 Niederlassungen");
  48. });
  49. it("needsMultiBranchSelectionHint only when q exists and branches empty", () => {
  50. expect(
  51. needsMultiBranchSelectionHint({
  52. isAdminDev: true,
  53. urlState: { scope: SEARCH_SCOPE.MULTI, q: "x", branches: [] },
  54. })
  55. ).toBe(true);
  56. expect(
  57. needsMultiBranchSelectionHint({
  58. isAdminDev: true,
  59. urlState: { scope: SEARCH_SCOPE.MULTI, q: null, branches: [] },
  60. })
  61. ).toBe(false);
  62. });
  63. it("buildNextStateForScopeChange keeps branches only for MULTI", () => {
  64. const base = { q: "x", branches: ["NL01"] };
  65. expect(
  66. buildNextStateForScopeChange({
  67. urlState: base,
  68. nextScope: SEARCH_SCOPE.ALL,
  69. })
  70. ).toMatchObject({ scope: SEARCH_SCOPE.ALL, branches: [] });
  71. expect(
  72. buildNextStateForScopeChange({
  73. urlState: base,
  74. nextScope: SEARCH_SCOPE.MULTI,
  75. })
  76. ).toMatchObject({ scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] });
  77. });
  78. it("buildNextStateForToggleBranch toggles membership", () => {
  79. const s1 = buildNextStateForToggleBranch({
  80. urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] },
  81. branchId: "NL02",
  82. });
  83. expect(s1.branches.sort()).toEqual(["NL01", "NL02"]);
  84. const s2 = buildNextStateForToggleBranch({
  85. urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01", "NL02"] },
  86. branchId: "NL02",
  87. });
  88. expect(s2.branches).toEqual(["NL01"]);
  89. });
  90. it("buildNextStateForClearAllBranches clears branches", () => {
  91. const s = buildNextStateForClearAllBranches({
  92. urlState: { scope: SEARCH_SCOPE.MULTI, branches: ["NL01"] },
  93. });
  94. expect(s.branches).toEqual([]);
  95. expect(s.scope).toBe(SEARCH_SCOPE.MULTI);
  96. });
  97. it("buildHrefForSingleBranchSwitch returns null for invalid branch", () => {
  98. expect(
  99. buildHrefForSingleBranchSwitch({
  100. nextBranch: "bad",
  101. urlState: { q: "x", scope: SEARCH_SCOPE.SINGLE, limit: 100 },
  102. })
  103. ).toBe(null);
  104. });
  105. });