Files
co-lite/solvway-admin-portal/app/roles/page.test.tsx
T
ngqileandClaude Opus 5.5 49966586ca fix(admin-portal): match the screens to the Co-Lite backend after the browser check
- Login copy describes the event CRM instead of orders and payouts.
- Role dialog has no parent field; the role routes require a trimmed name and forward only {name}.
- New-user banner says a temporary password was emailed; TEMP_PASSWORD_NOT_SENT shows a message and a link back to users.
- Username is read-only when editing.
- Password hint and an 8-character minimum match the backend admin password rules.

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

139 lines
5.9 KiB
TypeScript

import { render, screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, it, expect, vi, beforeEach } from "vitest";
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,
}));
// Keep the real saveAdminRole (so the edit flow's PUT to /api/roles/{id} is exercised) but
// stub the list fetch.
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";
beforeEach(() => {
mocks.listAdminRoles.mockReset();
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
mocks.apiPut.mockReset();
mocks.useAdminAuth.mockReset();
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => true });
});
describe("RolesPage", () => {
it("shows the empty state when there are no roles", async () => {
mocks.listAdminRoles.mockResolvedValue([]);
render(<RolesPage />);
await waitFor(() => expect(screen.getByText(/no roles/i)).toBeInTheDocument());
});
it("shows a role label with the code as secondary text, and the parent by label", async () => {
mocks.listAdminRoles.mockResolvedValue([
{ id: 1, name: "ROLE_SUPER_ADMIN" },
{ id: 4, name: "ROLE_HQ", parent: { id: 1 } },
]);
render(<RolesPage />);
const row = (await screen.findByText("ROLE_HQ")).closest("tr")!;
expect(within(row).getByText("HQ")).toBeInTheDocument();
expect(within(row).getByText("Super admin")).toBeInTheDocument();
expect(screen.getByRole("heading", { level: 1, name: "Roles" })).toBeInTheDocument();
});
it("has no parent field and posts only the name on create", async () => {
mocks.listAdminRoles.mockResolvedValue([{ id: 4, name: "ROLE_HQ" }]);
mocks.apiPost.mockResolvedValue({ id: 5, name: "ROLE_NEW" });
render(<RolesPage />);
await waitFor(() => expect(screen.getByText("ROLE_HQ")).toBeInTheDocument());
await userEvent.click(screen.getByRole("button", { name: /add role/i }));
expect(screen.queryByLabelText(/parent/i)).not.toBeInTheDocument();
await userEvent.type(screen.getByLabelText(/role name/i), "ROLE_NEW");
await userEvent.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() => expect(mocks.apiPost).toHaveBeenCalledWith("/api/roles", { name: "ROLE_NEW" }));
});
it("requires a role name before submitting", async () => {
mocks.listAdminRoles.mockResolvedValue([{ id: 4, name: "ROLE_HQ" }]);
render(<RolesPage />);
await waitFor(() => expect(screen.getByText("ROLE_HQ")).toBeInTheDocument());
await userEvent.click(screen.getByRole("button", { name: /add role/i }));
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(screen.getByText("Role name is required.")).toBeInTheDocument();
expect(mocks.apiPost).not.toHaveBeenCalled();
});
it("opens the edit dialog prefilled and PUTs to /api/roles/{id} on save", async () => {
mocks.listAdminRoles.mockResolvedValue([
{ id: 4, name: "ROLE_HQ", parent: { id: 1 } },
{ id: 200, name: "ROLE_NEW", parent: { id: 4 } },
]);
mocks.apiPut.mockResolvedValue({ id: 200, name: "ROLE_NEW_EDIT", parent: { id: 4 } });
render(<RolesPage />);
await waitFor(() => expect(screen.getByText("ROLE_NEW")).toBeInTheDocument());
const newRoleRow = screen.getByText("ROLE_NEW").closest("tr")!;
await userEvent.click(within(newRoleRow).getByRole("button", { name: /edit/i }));
// The dialog renders in edit mode, prefilled with the row's values.
expect(screen.getByRole("dialog")).toBeInTheDocument();
expect(screen.getByText("Edit role")).toBeInTheDocument();
expect(screen.getByLabelText(/role name/i)).toHaveValue("ROLE_NEW");
await userEvent.clear(screen.getByLabelText(/role name/i));
await userEvent.type(screen.getByLabelText(/role name/i), "ROLE_NEW_EDIT");
await userEvent.click(screen.getByRole("button", { name: /save/i }));
await waitFor(() =>
expect(mocks.apiPut).toHaveBeenCalledWith(
"/api/roles/200",
{ id: 200, name: "ROLE_NEW_EDIT" },
),
);
});
it("keeps the dialog open and shows the error when a save fails", async () => {
mocks.listAdminRoles.mockResolvedValue([{ id: 4, name: "ROLE_HQ", parent: { id: 1 } }]);
mocks.apiPost.mockRejectedValue(new Error("The role could not be saved. Please try again."));
render(<RolesPage />);
await waitFor(() => expect(screen.getByText("ROLE_HQ")).toBeInTheDocument());
await userEvent.click(screen.getByRole("button", { name: /add role/i }));
await userEvent.type(screen.getByLabelText(/role name/i), "ROLE_NEW");
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByText(/could not be saved/i)).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
it("shows the backend ROLE_NAME_TAKEN message in the dialog on a rejected create", async () => {
const message = "A role with this name already exists";
mocks.listAdminRoles.mockResolvedValue([{ id: 4, name: "ROLE_HQ", parent: { id: 1 } }]);
mocks.apiPost.mockRejectedValue(new Error(message));
render(<RolesPage />);
await waitFor(() => expect(screen.getByText("ROLE_HQ")).toBeInTheDocument());
await userEvent.click(screen.getByRole("button", { name: /add role/i }));
await userEvent.type(screen.getByLabelText(/role name/i), "ROLE_HQ");
await userEvent.click(screen.getByRole("button", { name: /save/i }));
expect(await screen.findByText(message)).toBeInTheDocument();
expect(screen.getByRole("dialog")).toBeInTheDocument();
});
});