Files
co-lite/solvway-admin-portal/app/roles/page.sorting.test.tsx
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +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";
// 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");
});
});