Files
co-lite/solvway-admin-portal/app/components/ui/data-table.test.tsx
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
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
2026-11-26 15:55:45 +08:00

132 lines
6.1 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { render, screen, fireEvent } from "@testing-library/react";
import type { ColumnDef } from "@tanstack/react-table";
import { DataTable } from "./data-table";
type Row = { id: string; name: string };
const columns: ColumnDef<Row, unknown>[] = [{ accessorKey: "name", header: "Name" }];
const data: Row[] = [{ id: "1", name: "Alpha" }, { id: "2", name: "Beta" }];
function renderTable(extra: Partial<React.ComponentProps<typeof DataTable<Row>>> = {}) {
return render(
<DataTable<Row> label="t" columns={columns} data={data} total={2} page={1} pageSize={20}
onPageChange={() => {}} rowKey={r => r.id} {...extra} />,
);
}
describe("DataTable row selection", () => {
it("calls onRowClick on click and on Enter, and marks the selected row", () => {
const onRowClick = vi.fn();
renderTable({ onRowClick, selectedKey: "2" });
const beta = screen.getByText("Beta").closest("tr")!;
expect(beta).toHaveAttribute("aria-selected", "true");
fireEvent.click(screen.getByText("Alpha"));
expect(onRowClick).toHaveBeenCalledWith(data[0]);
fireEvent.keyDown(beta, { key: "Enter" });
expect(onRowClick).toHaveBeenCalledWith(data[1]);
});
it("leaves rows non-interactive when onRowClick is absent", () => {
renderTable();
const alpha = screen.getByText("Alpha").closest("tr")!;
expect(alpha).not.toHaveAttribute("tabindex");
expect(alpha).not.toHaveAttribute("aria-selected");
});
});
// unify-admin-page-patterns: the empty state used to sit in a colSpan cell inside the horizontal
// scroller, so on a phone it was clipped with the columns. It now renders outside the scroller.
describe("DataTable empty state", () => {
it("renders the empty state outside the horizontal scroller", () => {
const { container } = renderTable({ data: [], total: 0, empty: <p>No members match</p> });
const message = screen.getByText("No members match");
const scroller = container.querySelector("[data-slot=table-container]")!;
expect(scroller.contains(message)).toBe(false);
});
it("still shows the default empty message when none is given", () => {
renderTable({ data: [], total: 0 });
expect(screen.getByText("No results.")).toBeInTheDocument();
});
});
describe("Table header style", () => {
it("uses the shared small uppercase header on every table", async () => {
const { Table, TableHeader, TableRow, TableHead } = await import("./table");
render(
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
</TableRow>
</TableHeader>
</Table>,
);
const th = screen.getByRole("columnheader", { name: "Name" });
expect(th.className).toContain("uppercase");
expect(th.className).toContain("text-xs");
});
});
describe("DataTable a11y and selection", () => {
it("gives the selected row readable text colour", () => {
renderTable({ onRowClick: vi.fn(), selectedKey: "1" });
const row = screen.getByText("Alpha").closest("tr")!;
expect(row.className).toContain("text-accent-foreground");
});
it("announces the empty block as a status region", () => {
renderTable({ data: [], total: 0 });
expect(screen.getByRole("status")).toHaveTextContent("No results.");
});
});
// Wallet ledger rows share a refNo (a withdrawal reversal writes WDI and WDAI under one ref). Duplicate
// React keys made a filtered list keep a stale row from the previous list (an Acorns row under Apples).
describe("DataTable duplicate row keys", () => {
it("renders exactly the new rows when the data changes, even if rowKey repeats", () => {
const first: Row[] = [{ id: "a", name: "Old one" }, { id: "dup", name: "Old two" }, { id: "dup", name: "Old three" }];
const next: Row[] = [{ id: "dup", name: "New one" }, { id: "dup", name: "New two" }];
const props = { label: "t", columns, total: 3, page: 1, pageSize: 20, onPageChange: () => {}, rowKey: (r: Row) => r.id };
const { rerender, container } = render(<DataTable<Row> {...props} data={first} />);
rerender(<DataTable<Row> {...props} data={next} />);
const names = [...container.querySelectorAll("tbody tr")].map(tr => tr.textContent);
expect(names).toEqual(["New one", "New two"]);
});
it("still marks the selected row by its rowKey", () => {
const rows: Row[] = [{ id: "dup", name: "One" }, { id: "dup", name: "Two" }, { id: "x", name: "Three" }];
render(<DataTable<Row> label="t" columns={columns} data={rows} total={3} page={1} pageSize={20}
onPageChange={() => {}} rowKey={r => r.id} onRowClick={() => {}} selectedKey="x" />);
expect(screen.getByText("Three").closest("tr")).toHaveAttribute("aria-selected", "true");
expect(screen.getByText("One").closest("tr")).toHaveAttribute("aria-selected", "false");
});
});
// unify-admin-listing-toolbar: a table wider than its container keeps its first column pinned, with the
// row's own background (hover, selected) so scrolled cells never show through it.
describe("DataTable pinned first column", () => {
const wide: ColumnDef<Row, unknown>[] = [
{ accessorKey: "id", header: "Id" },
{ accessorKey: "name", header: "Name" },
];
it("pins the first header and body cell to the left edge, and only those", () => {
renderTable({ columns: wide });
const [firstHead, secondHead] = screen.getAllByRole("columnheader");
// The header row is gold (--table-head); the pinned head cell matches it so it stays opaque.
expect(firstHead).toHaveClass("sticky", "left-0", "bg-table-head");
expect(secondHead).not.toHaveClass("sticky");
const cells = screen.getByText("Alpha").closest("tr")!.querySelectorAll("td");
expect(cells[0]).toHaveClass("sticky", "left-0", "bg-card");
expect(cells[1]).not.toHaveClass("sticky");
});
it("gives the pinned cell the row's hover and selected background", () => {
renderTable({ columns: wide, onRowClick: () => {}, selectedKey: "1" });
const row = screen.getByText("Alpha").closest("tr")!;
expect(row).toHaveClass("group");
expect(row.querySelector("td")).toHaveClass("group-hover:bg-muted", "group-aria-selected:bg-accent");
});
});