Files
co-lite/solvway-admin-portal/app/roles/page.sorting.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

92 lines
4.2 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
import { render, screen, within, fireEvent } from "@testing-library/react";
// BABYBOO-54 task 1.1: the /roles list sorts in the browser; the Actions column does not sort.
const mocks = vi.hoisted(() => ({
listAdminRoles: vi.fn(),
apiGet: vi.fn(),
apiPost: vi.fn(),
apiPut: vi.fn(),
useAdminAuth: vi.fn(),
}));
vi.mock("@/app/lib/clientApi", () => ({ apiGet: mocks.apiGet, apiPost: mocks.apiPost, apiPut: mocks.apiPut }));
vi.mock("@/app/lib/services/adminRoles", async (importOriginal) => {
const mod = await importOriginal<typeof import("@/app/lib/services/adminRoles")>();
return { ...mod, listAdminRoles: mocks.listAdminRoles };
});
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
import RolesPage from "./page";
// Labels: Super admin (no parent), HQ (parent Super admin), Branch manager (parent HQ), Auditor (parent Super admin).
// API order is deliberately sorted by neither role nor parent.
const ROLES = [
{ id: 4, name: "ROLE_HQ", parent: { id: 1 } },
{ id: 7, name: "ROLE_AUDITOR", parent: { id: 1 } },
{ id: 1, name: "ROLE_SUPER_ADMIN" },
{ id: 9, name: "ROLE_BRANCH_MANAGER", parent: { id: 4 } },
];
beforeEach(() => {
Object.values(mocks).forEach((m) => m.mockReset());
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => true });
mocks.listAdminRoles.mockResolvedValue(ROLES);
});
async function table() {
await screen.findByText("ROLE_HQ");
return within(screen.getByRole("table"));
}
/** First line of the Role cell is the label; the raw code sits beneath, so read codes for order. */
function codes(t: ReturnType<typeof within>) {
return t.getAllByRole("row").slice(1).map((r: HTMLElement) => /ROLE_[A-Z]+(?:_[A-Z]+)*(?=[A-Z][a-z]|$)/.exec(within(r).getAllByRole("cell")[0].textContent ?? "")?.[0]);
}
const click = (t: ReturnType<typeof within>, header: string) => fireEvent.click(t.getByRole("button", { name: header }));
describe("/roles - client-side header sorting", () => {
it("Role header reorders by displayed label, asc then desc", async () => {
render(<RolesPage />);
const t = await table();
expect(codes(t)).toEqual(["ROLE_HQ", "ROLE_AUDITOR", "ROLE_SUPER_ADMIN", "ROLE_BRANCH_MANAGER"]);
click(t, "Role");
expect(codes(t)).toEqual(["ROLE_AUDITOR", "ROLE_BRANCH_MANAGER", "ROLE_HQ", "ROLE_SUPER_ADMIN"]);
expect(t.getByRole("columnheader", { name: "Role" })).toHaveAttribute("aria-sort", "ascending");
click(t, "Role");
expect(codes(t)).toEqual(["ROLE_SUPER_ADMIN", "ROLE_HQ", "ROLE_BRANCH_MANAGER", "ROLE_AUDITOR"]);
expect(t.getByRole("columnheader", { name: "Role" })).toHaveAttribute("aria-sort", "descending");
});
it("Parent header orders by the parent's displayed label", async () => {
render(<RolesPage />);
const t = await table();
const idx = t.getAllByRole("columnheader").findIndex((h: HTMLElement) => h.textContent?.trim() === "Parent");
const parents = () => t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getAllByRole("cell")[idx].textContent);
click(t, "Parent");
const asc = parents();
// "HQ" < "Super admin" < "—" is not guaranteed for the dash, so only assert the two named parents' order.
expect(asc.indexOf("HQ")).toBeLessThan(asc.indexOf("Super admin"));
click(t, "Parent");
const desc = parents();
expect(desc.indexOf("Super admin")).toBeLessThan(desc.indexOf("HQ"));
});
it("Actions column has no sort button; sorting makes no new request", async () => {
render(<RolesPage />);
const t = await table();
const heads = t.getAllByRole("columnheader");
expect(within(heads[heads.length - 1]).queryByRole("button")).toBeNull();
click(t, "Role");
expect(mocks.listAdminRoles).toHaveBeenCalledTimes(1);
});
it("a role with no parent (dash placeholder) sorts last when Parent is ascending", async () => {
render(<RolesPage />);
const t = await table();
const idx = t.getAllByRole("columnheader").findIndex((h: HTMLElement) => h.textContent?.trim() === "Parent");
click(t, "Parent");
const parents = t.getAllByRole("row").slice(1).map((r: HTMLElement) => within(r).getAllByRole("cell")[idx].textContent);
expect(parents[parents.length - 1]).toBe("\u2014");
});
});