usersSorting.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. ADMIN_USERS_SORT,
  5. normalizeAdminUsersSortMode,
  6. sortAdminUsers,
  7. } from "./usersSorting.js";
  8. function mapRows(rows) {
  9. return rows.map((x) => ({
  10. id: x.id,
  11. role: x.role,
  12. branchId: x.branchId,
  13. username: x.username,
  14. }));
  15. }
  16. describe("lib/frontend/admin/users/usersSorting", () => {
  17. it("normalizes known sort modes and rejects invalid values", () => {
  18. expect(normalizeAdminUsersSortMode(undefined)).toBe(
  19. ADMIN_USERS_SORT.DEFAULT,
  20. );
  21. expect(normalizeAdminUsersSortMode("")).toBe(ADMIN_USERS_SORT.DEFAULT);
  22. expect(normalizeAdminUsersSortMode(ADMIN_USERS_SORT.ROLE_RIGHTS)).toBe(
  23. ADMIN_USERS_SORT.ROLE_RIGHTS,
  24. );
  25. expect(normalizeAdminUsersSortMode("nope")).toBe(null);
  26. });
  27. it("sorts by role rights with deterministic tie-breakers", () => {
  28. const items = [
  29. {
  30. id: "6",
  31. role: "admin",
  32. branchId: null,
  33. username: "z-admin",
  34. },
  35. {
  36. id: "1",
  37. role: "branch",
  38. branchId: "NL10",
  39. username: "branch-z",
  40. },
  41. {
  42. id: "2",
  43. role: "branch",
  44. branchId: "NL2",
  45. username: "branch-a",
  46. },
  47. {
  48. id: "3",
  49. role: "dev",
  50. branchId: null,
  51. username: "dev",
  52. },
  53. {
  54. id: "4",
  55. role: "superadmin",
  56. branchId: null,
  57. username: "root",
  58. },
  59. {
  60. id: "5",
  61. role: "admin",
  62. branchId: null,
  63. username: "a-admin",
  64. },
  65. ];
  66. const out = sortAdminUsers(items, ADMIN_USERS_SORT.ROLE_RIGHTS);
  67. expect(mapRows(out)).toEqual([
  68. { id: "4", role: "superadmin", branchId: null, username: "root" },
  69. { id: "3", role: "dev", branchId: null, username: "dev" },
  70. { id: "5", role: "admin", branchId: null, username: "a-admin" },
  71. { id: "6", role: "admin", branchId: null, username: "z-admin" },
  72. { id: "2", role: "branch", branchId: "NL2", username: "branch-a" },
  73. { id: "1", role: "branch", branchId: "NL10", username: "branch-z" },
  74. ]);
  75. });
  76. it("sorts by branch asc (numeric) with null branch at the end", () => {
  77. const items = [
  78. { id: "1", role: "branch", branchId: "NL10", username: "b1" },
  79. { id: "2", role: "branch", branchId: "NL2", username: "b2" },
  80. { id: "3", role: "admin", branchId: null, username: "admin" },
  81. { id: "4", role: "superadmin", branchId: null, username: "super" },
  82. { id: "5", role: "branch", branchId: "NL01", username: "b0" },
  83. ];
  84. const out = sortAdminUsers(items, ADMIN_USERS_SORT.BRANCH_ASC);
  85. expect(mapRows(out)).toEqual([
  86. { id: "5", role: "branch", branchId: "NL01", username: "b0" },
  87. { id: "2", role: "branch", branchId: "NL2", username: "b2" },
  88. { id: "1", role: "branch", branchId: "NL10", username: "b1" },
  89. { id: "4", role: "superadmin", branchId: null, username: "super" },
  90. { id: "3", role: "admin", branchId: null, username: "admin" },
  91. ]);
  92. });
  93. it("returns a shallow copy for default mode", () => {
  94. const items = [{ id: "1" }, { id: "2" }];
  95. const out = sortAdminUsers(items, ADMIN_USERS_SORT.DEFAULT);
  96. expect(out).toEqual(items);
  97. expect(out).not.toBe(items);
  98. });
  99. });