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
This commit is contained in:
1 parent
869d7d5894
commit
df70306c9a
7 files changed
+60
-6
No files matched your search
@@ -53,7 +53,7 @@ function AuditLogContent() {
|
||||
const cut = (text: string | null | undefined, max = "max-w-40") => <span className={`block truncate ${max}`} title={text ?? undefined}>{text}</span>;
|
||||
const columns = useMemo<ColumnDef<AuditEvent, unknown>[]>(
|
||||
() => [
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (r: AuditEvent) => codeOf(r.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AuditEvent, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AuditEvent, unknown>] : []),
|
||||
{ id: "time", header: t("LsTime"), enableSorting: false, cell: ({ row }) => <span className="whitespace-nowrap">{formatDate(row.original.occurredAt)}</span> },
|
||||
{ id: "actor", header: t("AuditWho"), enableSorting: false, cell: ({ row }) => cut(row.original.actor, "max-w-32") },
|
||||
{ id: "module", header: t("AuditModule"), enableSorting: false, cell: ({ row }) => (row.original.module ? t(row.original.module) : "—") },
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
@@ -67,7 +67,7 @@ function EmailsPageContent() {
|
||||
},
|
||||
{ id: "recipients", header: t("EmlRecipient"), enableSorting: false, cell: ({ row }) => row.original.recipients.join(", ") },
|
||||
{ id: "datetime", header: t("EmlDate"), enableSorting: false, cell: ({ row }) => formatEmailDate(row.original.datetime) },
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (r: EmailRow) => codeOf(r.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<EmailRow, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<EmailRow, unknown>] : []),
|
||||
{ id: "status", header: t("Status"), enableSorting: false, cell: ({ row }) => t(EMAIL_STATUS_KEYS[row.original.status] ?? row.original.status) },
|
||||
],
|
||||
[t, showCompanyColumn, codeOf],
|
||||
|
||||
@@ -138,7 +138,7 @@ function EventLeadsDetailPageContent() {
|
||||
|
||||
const columns = useMemo<ColumnDef<EventLeadRow, unknown>[]>(
|
||||
() => [
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (r: EventLeadRow) => codeOf(r.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<EventLeadRow, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<EventLeadRow, unknown>] : []),
|
||||
{
|
||||
accessorKey: "name",
|
||||
header: t("LeadName"),
|
||||
|
||||
@@ -79,7 +79,7 @@ function EventsPageContent() {
|
||||
</Link>
|
||||
),
|
||||
},
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (e: AdminEvent) => codeOf(e.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AdminEvent, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AdminEvent, unknown>] : []),
|
||||
{ accessorKey: "location", header: t("Location"), enableSorting: false },
|
||||
{
|
||||
accessorKey: "startDate",
|
||||
|
||||
@@ -76,7 +76,7 @@ function LeadsPageContent() {
|
||||
</Link>
|
||||
),
|
||||
},
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (r: LeadRow) => codeOf(r.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<LeadRow, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<LeadRow, unknown>] : []),
|
||||
{ accessorKey: "mobile", header: t("LeadMobile"), enableSorting: false },
|
||||
{ accessorKey: "email", header: t("Email"), enableSorting: false },
|
||||
{ accessorKey: "companyName", header: t("LeadCompany"), enableSorting: false },
|
||||
|
||||
@@ -69,7 +69,7 @@ function HistoryTab() {
|
||||
const columns = useMemo<ColumnDef<AuditRow, unknown>[]>(
|
||||
() => [
|
||||
{ id: "datetime", header: t("LsTime"), enableSorting: false, cell: ({ row }) => formatDate(row.original.datetime) },
|
||||
...(showCompanyColumn ? [{ id: "company", accessorFn: (r: AuditRow) => codeOf(r.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AuditRow, unknown>] : []),
|
||||
...(showCompanyColumn ? [{ id: "company", cell: ({ row }) => codeOf(row.original.companyId ?? 0), header: t("Company"), enableSorting: false } as ColumnDef<AuditRow, unknown>] : []),
|
||||
{ accessorKey: "username", header: t("Username"), enableSorting: false },
|
||||
{ id: "event", header: t("LsEvent"), enableSorting: false, cell: ({ row }) => t(LOGIN_EVENT_KEYS[row.original.event] ?? row.original.event) },
|
||||
{ accessorKey: "ipAddress", header: t("LsIp"), enableSorting: false },
|
||||
|
||||
Reference in new issue
Block a user