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(); 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(); 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(); 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(); 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(); 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(); 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(); 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(); 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(); }); });