normalizeState.test.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. SEARCH_SCOPE,
  5. DEFAULT_SEARCH_LIMIT,
  6. } from "@/lib/frontend/search/urlState";
  7. import { normalizeSearchUrlStateForUser } from "./normalizeState.js";
  8. describe("lib/frontend/search/normalizeState", () => {
  9. it("forces SINGLE for branch users on the route branch", () => {
  10. const normalized = normalizeSearchUrlStateForUser(
  11. {
  12. q: "x",
  13. scope: SEARCH_SCOPE.ALL,
  14. branch: "NL99",
  15. branches: ["NL01", "NL02"],
  16. limit: 200,
  17. from: null,
  18. to: null,
  19. },
  20. { routeBranch: "NL01", user: { role: "branch", branchId: "NL01" } }
  21. );
  22. expect(normalized).toEqual({
  23. q: "x",
  24. scope: SEARCH_SCOPE.SINGLE,
  25. branch: "NL01",
  26. branches: [],
  27. limit: 200,
  28. from: null,
  29. to: null,
  30. });
  31. });
  32. it("admin/dev: SINGLE uses route branch even if URL carries a different branch", () => {
  33. const normalized = normalizeSearchUrlStateForUser(
  34. {
  35. q: "x",
  36. scope: SEARCH_SCOPE.SINGLE,
  37. branch: "NL77",
  38. branches: [],
  39. limit: DEFAULT_SEARCH_LIMIT,
  40. from: null,
  41. to: null,
  42. },
  43. { routeBranch: "NL01", user: { role: "admin", branchId: null } }
  44. );
  45. expect(normalized.branch).toBe("NL01");
  46. expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
  47. expect(normalized.branches).toEqual([]);
  48. });
  49. it("admin/dev: MULTI keeps branches and clears branch", () => {
  50. const normalized = normalizeSearchUrlStateForUser(
  51. {
  52. q: "x",
  53. scope: SEARCH_SCOPE.MULTI,
  54. branch: "NL01",
  55. branches: ["NL06", "NL20"],
  56. limit: 50,
  57. from: null,
  58. to: null,
  59. },
  60. { routeBranch: "NL99", user: { role: "dev", branchId: null } }
  61. );
  62. expect(normalized.scope).toBe(SEARCH_SCOPE.MULTI);
  63. expect(normalized.branch).toBe(null);
  64. expect(normalized.branches).toEqual(["NL06", "NL20"]);
  65. expect(normalized.limit).toBe(50);
  66. });
  67. it("admin/dev: ALL clears branch and branches", () => {
  68. const normalized = normalizeSearchUrlStateForUser(
  69. {
  70. q: "x",
  71. scope: SEARCH_SCOPE.ALL,
  72. branch: "NL01",
  73. branches: ["NL06"],
  74. limit: 200,
  75. from: null,
  76. to: null,
  77. },
  78. { routeBranch: "NL99", user: { role: "admin", branchId: null } }
  79. );
  80. expect(normalized).toEqual({
  81. q: "x",
  82. scope: SEARCH_SCOPE.ALL,
  83. branch: null,
  84. branches: [],
  85. limit: 200,
  86. from: null,
  87. to: null,
  88. });
  89. });
  90. it("unknown roles: fail-safe to SINGLE on route branch", () => {
  91. const normalized = normalizeSearchUrlStateForUser(
  92. {
  93. q: "x",
  94. scope: SEARCH_SCOPE.ALL,
  95. branch: "NL01",
  96. branches: ["NL06"],
  97. limit: 999, // invalid -> normalized to default
  98. from: null,
  99. to: null,
  100. },
  101. { routeBranch: "NL02", user: { role: "user", branchId: "NL02" } }
  102. );
  103. expect(normalized.scope).toBe(SEARCH_SCOPE.SINGLE);
  104. expect(normalized.branch).toBe("NL02");
  105. expect(normalized.limit).toBe(DEFAULT_SEARCH_LIMIT);
  106. });
  107. });