Files
co-lite/solvway-admin-portal/app/companyScope.readonly.test.tsx
T

93 lines
3.8 KiB
TypeScript

import { render, screen, waitFor } 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(),
listInterests: vi.fn(),
apiGet: vi.fn(),
useAdminAuth: vi.fn(),
}));
vi.mock("@/app/lib/clientApi", async (i) => ({ ...(await i<object>()), apiGet: mocks.apiGet, apiPost: vi.fn(), apiPut: vi.fn() }));
vi.mock("@/app/lib/services/adminRoles", async (i) => ({ ...(await i<object>()), listAdminRoles: mocks.listAdminRoles }));
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
import { memoryRouter } from "@/test/memoryRouter";
import RolesPage from "./roles/page";
import InterestsPage from "./interests/page";
import LoginSecurityPage from "./login-security/page";
const as = (isSuper: boolean) => mocks.useAdminAuth.mockReturnValue({ loading: false, isSuper, hasModule: () => true });
beforeEach(() => {
memoryRouter.reset("/x");
mocks.listAdminRoles.mockReset().mockResolvedValue([{ id: 4, name: "ROLE_HQ", parent: { id: 1 } }]);
mocks.apiGet.mockReset().mockImplementation(async (path: string) => {
if (path.includes("/interests")) return [{ id: 3, name: "Product A", active: true }];
if (path.includes("/config")) return { enabled: true, maxAttempts: 5, period: 15, unit: "M" };
if (path.includes("/password-rules")) return [];
return [];
});
mocks.useAdminAuth.mockReset();
});
describe("Roles read-only for staff", () => {
it("hides Add role, Edit and Manage access for staff", async () => {
as(false);
render(<RolesPage />);
await screen.findByText("ROLE_HQ");
expect(screen.queryByRole("button", { name: /add role/i })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /^edit/i })).not.toBeInTheDocument();
expect(screen.queryByRole("link", { name: /manage access/i })).not.toBeInTheDocument();
});
it("keeps them for a super admin", async () => {
as(true);
render(<RolesPage />);
await screen.findByText("ROLE_HQ");
expect(screen.getByRole("button", { name: /add role/i })).toBeInTheDocument();
});
});
describe("Interests read-only for staff", () => {
it("hides Add interest and row actions for staff", async () => {
as(false);
render(<InterestsPage />);
await screen.findByText("Product A");
expect(screen.queryByRole("button", { name: /add interest/i })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /^edit/i })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /deactivate/i })).not.toBeInTheDocument();
});
it("keeps Add interest for a super admin", async () => {
as(true);
render(<InterestsPage />);
await screen.findByText("Product A");
expect(screen.getByRole("button", { name: /add interest/i })).toBeInTheDocument();
});
});
describe("Login security Settings read-only for staff", () => {
async function openSettings() {
const user = userEvent.setup();
render(<LoginSecurityPage />);
await user.click(await screen.findByRole("tab", { name: /settings/i }));
return user;
}
it("hides the Save buttons for staff", async () => {
as(false);
await openSettings();
await waitFor(() => expect(mocks.apiGet).toHaveBeenCalledWith(expect.stringContaining("/config")));
expect(screen.queryByRole("button", { name: /save lockout settings/i })).not.toBeInTheDocument();
expect(screen.queryByRole("button", { name: /save password rules/i })).not.toBeInTheDocument();
});
it("shows the Save buttons to a super admin", async () => {
as(true);
await openSettings();
expect(await screen.findByRole("button", { name: /save lockout settings/i })).toBeInTheDocument();
});
});