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
113 lines
4.7 KiB
TypeScript
113 lines
4.7 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("requires a parent to be chosen before submitting the add-role form", 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.type(screen.getByLabelText(/role name/i), "ROLE_NEW");
|
|
await userEvent.click(screen.getByRole("button", { name: /save/i }));
|
|
|
|
expect(screen.getByText(/parent role is required/i)).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",
|
|
expect.objectContaining({ 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.getByLabelText(/parent/i));
|
|
await userEvent.click(await screen.findByRole("option", { name: "HQ" }));
|
|
await userEvent.click(screen.getByRole("button", { name: /save/i }));
|
|
|
|
expect(await screen.findByText(/could not be saved/i)).toBeInTheDocument();
|
|
expect(screen.getByRole("dialog")).toBeInTheDocument();
|
|
});
|
|
});
|