userManagementUx.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* @vitest-environment node */
  2. import { describe, it, expect } from "vitest";
  3. import {
  4. normalizeUsernameForConfirmation,
  5. isUsernameConfirmationMatch,
  6. normalizeBranchIdInput,
  7. normalizeBranchNumberInput,
  8. formatBranchIdFromNumberInput,
  9. extractBranchNumberInputFromBranchId,
  10. isValidBranchIdFormat,
  11. evaluateBranchExistence,
  12. } from "./userManagementUx.js";
  13. describe("lib/frontend/admin/users/userManagementUx", () => {
  14. describe("username confirmation", () => {
  15. it("normalizes to lowercase and trims", () => {
  16. expect(normalizeUsernameForConfirmation(" Alice.Admin ")).toBe(
  17. "alice.admin",
  18. );
  19. });
  20. it("matches typed username case-insensitively after trimming", () => {
  21. expect(
  22. isUsernameConfirmationMatch({
  23. expectedUsername: "branch.user",
  24. typedUsername: " BRANCH.USER ",
  25. }),
  26. ).toBe(true);
  27. });
  28. it("returns false for empty or mismatching values", () => {
  29. expect(
  30. isUsernameConfirmationMatch({
  31. expectedUsername: "branch.user",
  32. typedUsername: "other.user",
  33. }),
  34. ).toBe(false);
  35. expect(
  36. isUsernameConfirmationMatch({
  37. expectedUsername: "",
  38. typedUsername: "",
  39. }),
  40. ).toBe(false);
  41. });
  42. });
  43. describe("branch formatting", () => {
  44. it("normalizes branchId input to uppercase + trim", () => {
  45. expect(normalizeBranchIdInput(" nl01 ")).toBe("NL01");
  46. });
  47. it("normalizes numeric branch input and strips non-digits", () => {
  48. expect(normalizeBranchNumberInput(" 001 ")).toBe("1");
  49. expect(normalizeBranchNumberInput(" 32a ")).toBe("32");
  50. expect(normalizeBranchNumberInput("000")).toBe("0");
  51. expect(normalizeBranchNumberInput("abc")).toBe("");
  52. });
  53. it("formats NL branchId with 2+ digit policy", () => {
  54. expect(formatBranchIdFromNumberInput("1")).toBe("NL01");
  55. expect(formatBranchIdFromNumberInput("32")).toBe("NL32");
  56. expect(formatBranchIdFromNumberInput("200")).toBe("NL200");
  57. });
  58. it("extracts numeric branch input from an existing branchId", () => {
  59. expect(extractBranchNumberInputFromBranchId("NL01")).toBe("01");
  60. expect(extractBranchNumberInputFromBranchId("nl200")).toBe("200");
  61. expect(extractBranchNumberInputFromBranchId("XX1")).toBe("");
  62. });
  63. it("validates branchId format", () => {
  64. expect(isValidBranchIdFormat("NL01")).toBe(true);
  65. expect(isValidBranchIdFormat("nl200")).toBe(true);
  66. expect(isValidBranchIdFormat("XX1")).toBe(false);
  67. expect(isValidBranchIdFormat("NL")).toBe(false);
  68. });
  69. });
  70. describe("branch existence decision", () => {
  71. it("blocks submit for unknown branch only when list is ready", () => {
  72. expect(
  73. evaluateBranchExistence({
  74. role: "branch",
  75. branchId: "NL99",
  76. branchesStatus: "ready",
  77. availableBranchIds: ["NL01", "NL02"],
  78. }).shouldBlockSubmit,
  79. ).toBe(true);
  80. });
  81. it("allows submit when branch exists in ready list", () => {
  82. expect(
  83. evaluateBranchExistence({
  84. role: "branch",
  85. branchId: "NL02",
  86. branchesStatus: "ready",
  87. availableBranchIds: ["NL01", "NL02"],
  88. }).shouldBlockSubmit,
  89. ).toBe(false);
  90. });
  91. it("is fail-open when list fetch failed", () => {
  92. const result = evaluateBranchExistence({
  93. role: "branch",
  94. branchId: "NL99",
  95. branchesStatus: "error",
  96. availableBranchIds: null,
  97. });
  98. expect(result.listError).toBe(true);
  99. expect(result.shouldBlockSubmit).toBe(false);
  100. });
  101. it("is irrelevant for non-branch roles", () => {
  102. const result = evaluateBranchExistence({
  103. role: "admin",
  104. branchId: "NL99",
  105. branchesStatus: "ready",
  106. availableBranchIds: ["NL01"],
  107. });
  108. expect(result.isBranchRole).toBe(false);
  109. expect(result.shouldBlockSubmit).toBe(false);
  110. });
  111. });
  112. });