Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
120 lines
5.4 KiB
TypeScript
120 lines
5.4 KiB
TypeScript
import { render, screen, waitFor, within } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
listAdminUsers: vi.fn(),
|
|
listAdminEvents: vi.fn(),
|
|
listEmails: vi.fn(),
|
|
searchAuditLog: vi.fn(),
|
|
listLeads: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
useCompanyScope: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminUsers", async (i) => ({ ...(await i<object>()), listAdminUsers: mocks.listAdminUsers }));
|
|
vi.mock("@/app/lib/services/adminEvents", async (i) => ({ ...(await i<object>()), listAdminEvents: mocks.listAdminEvents }));
|
|
vi.mock("@/app/lib/services/adminEmails", async (i) => ({ ...(await i<object>()), listEmails: mocks.listEmails }));
|
|
vi.mock("@/app/lib/services/auditLog", async (i) => ({ ...(await i<object>()), searchAuditLog: mocks.searchAuditLog }));
|
|
vi.mock("@/app/lib/services/adminLeads", async (i) => ({ ...(await i<object>()), listLeads: mocks.listLeads }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("@/app/lib/CompanyScope", () => ({ useCompanyScope: mocks.useCompanyScope }));
|
|
vi.mock("next/navigation", async () => (await import("@/test/memoryRouter")).navigationMock);
|
|
|
|
import { memoryRouter } from "@/test/memoryRouter";
|
|
import UsersPage from "./users/page";
|
|
import EventsPage from "./events/page";
|
|
import EmailsPage from "./emails/page";
|
|
import AuditLogPage from "./audit-log/page";
|
|
import LeadsPage from "./leads/page";
|
|
|
|
const CODES: Record<number, string> = { 1: "INS", 2: "CV" };
|
|
const scope = (companyId?: number, isShown = companyId === undefined) => ({
|
|
companyId,
|
|
companies: [],
|
|
showCompanyColumn: isShown,
|
|
codeOf: (id?: number) => (id ? CODES[id] ?? "" : ""),
|
|
setCompanyId: vi.fn(),
|
|
});
|
|
const headers = async () => within(await screen.findByRole("table")).getAllByRole("columnheader").map((h) => h.textContent);
|
|
|
|
const PAGES = [
|
|
{
|
|
name: "users",
|
|
Page: UsersPage,
|
|
list: mocks.listAdminUsers,
|
|
row: { id: 5, username: "jane", name: "Jane", email: "j@x.test", enabled: true, status: "A", roles: [], companyId: 2, companyCode: "CV", loginName: "jane@cv" },
|
|
},
|
|
{
|
|
name: "events",
|
|
Page: EventsPage,
|
|
list: mocks.listAdminEvents,
|
|
row: { uuid: "e1", name: "Expo", location: "KL", startDate: "2026-12-01", endDate: "2026-12-02", enabled: true, companyId: 2 },
|
|
},
|
|
{
|
|
name: "emails",
|
|
Page: EmailsPage,
|
|
list: mocks.listEmails,
|
|
row: { id: 9, datetime: "2026-11-30T11:44:26", recipients: ["a@x.test"], subject: "Hello", status: "Y", companyId: 2 },
|
|
},
|
|
{
|
|
name: "audit log",
|
|
Page: AuditLogPage,
|
|
list: mocks.searchAuditLog,
|
|
row: { id: 2, companyId: 2, occurredAt: "2026-11-30T15:52:00", actor: "sales1", module: "BO_USERS", action: "USER_UPDATE", httpMethod: "PUT", pathTemplate: "/x", targetId: "4", outcome: "SUCCESS", httpStatus: 200, clientIp: "10.0.0.5", changes: null },
|
|
},
|
|
];
|
|
|
|
beforeEach(() => {
|
|
memoryRouter.reset("/x");
|
|
for (const p of [...PAGES.map((x) => x.list), mocks.listLeads]) p.mockReset();
|
|
for (const p of PAGES) p.list.mockResolvedValue({ items: [p.row], total: 1 });
|
|
mocks.listLeads.mockResolvedValue({ items: [], total: 0 });
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, isSuper: true, hasModule: () => true });
|
|
mocks.useCompanyScope.mockReset().mockReturnValue(scope());
|
|
});
|
|
|
|
describe.each(PAGES)("$name list company scope", ({ Page, list, row }) => {
|
|
it("shows the Company column with the code for a super admin on All companies, and sends no companyId", async () => {
|
|
render(<Page />);
|
|
expect(await headers()).toContain("Company");
|
|
expect(await screen.findByText("CV")).toBeInTheDocument();
|
|
expect(list.mock.calls.at(-1)?.[0]).not.toHaveProperty("companyId", expect.anything());
|
|
expect(row).toBeTruthy();
|
|
});
|
|
|
|
it("hides the Company column and sends the picked companyId when one company is chosen", async () => {
|
|
mocks.useCompanyScope.mockReturnValue(scope(2));
|
|
render(<Page />);
|
|
expect(await headers()).not.toContain("Company");
|
|
await waitFor(() => expect(list).toHaveBeenLastCalledWith(expect.objectContaining({ companyId: 2 })));
|
|
});
|
|
|
|
it("hides the Company column for staff and never sends companyId", async () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, isSuper: false, hasModule: () => true });
|
|
mocks.useCompanyScope.mockReturnValue(scope(undefined, false));
|
|
render(<Page />);
|
|
expect(await headers()).not.toContain("Company");
|
|
expect(list.mock.calls.every((c) => (c[0] as { companyId?: number }).companyId === undefined)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("users list names", () => {
|
|
it("shows the local username, not the login name with @code", async () => {
|
|
render(<UsersPage />);
|
|
expect(await screen.findByText("jane")).toBeInTheDocument();
|
|
expect(screen.queryByText("jane@cv")).not.toBeInTheDocument();
|
|
});
|
|
});
|
|
|
|
describe("leads list company scope", () => {
|
|
it("adds a tenant Company column only on All companies, and sends the picked companyId", async () => {
|
|
const count = async () => (await headers()).filter((h) => h === "Company").length;
|
|
const { unmount } = render(<LeadsPage />);
|
|
const withAll = await count();
|
|
unmount();
|
|
mocks.useCompanyScope.mockReturnValue(scope(2));
|
|
render(<LeadsPage />);
|
|
expect(withAll).toBe((await count()) + 1);
|
|
await waitFor(() => expect(mocks.listLeads).toHaveBeenLastCalledWith(expect.objectContaining({ companyId: 2 })));
|
|
});
|
|
});
|