Files
co-lite/solvway-admin-portal/app/companyScope.latecodes.test.tsx
T
ngqileandClaude Opus 5.5 df70306c9a fix(portal): fill the Company column when the companies load after the rows
The table caches accessor values per row, so a code looked up before the
companies list arrived stayed blank; render it in the cell instead.

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

55 lines
2.8 KiB
TypeScript

import { render, screen } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
// The companies list usually arrives after the first page of rows; the Company column must still fill in.
const mocks = vi.hoisted(() => ({
listAdminEvents: vi.fn(),
listEmails: vi.fn(),
searchAuditLog: vi.fn(),
useAdminAuth: vi.fn(),
useCompanyScope: vi.fn(),
}));
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/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 EventsPage from "./events/page";
import EmailsPage from "./emails/page";
import AuditLogPage from "./audit-log/page";
const scope = (codes: Record<number, string>) => ({
companyId: undefined,
companies: [],
showCompanyColumn: true,
codeOf: (id?: number) => (id ? codes[id] ?? "" : ""),
setCompanyId: vi.fn(),
});
const PAGES = [
{ 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) p.list.mockReset().mockResolvedValue({ items: [p.row], total: 1 });
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, isSuper: true, hasModule: () => true });
});
describe.each(PAGES)("$name Company column", ({ Page, row }) => {
it("shows the code once the companies arrive after the rows", async () => {
mocks.useCompanyScope.mockReturnValue(scope({}));
const { rerender } = render(<Page />);
await screen.findAllByText(String("name" in row ? row.name : "subject" in row ? row.subject : row.actor));
mocks.useCompanyScope.mockReturnValue(scope({ 2: "CV" }));
rerender(<Page />);
expect(await screen.findByText("CV")).toBeInTheDocument();
});
});