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[] = [{ accessorKey: "name", header: "Name" }]; const data: Row[] = [{ id: "1", name: "Alpha" }, { id: "2", name: "Beta" }]; function renderTable(extra: Partial>> = {}) { return render( 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:

No members match

}); 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( Name
, ); 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( {...props} data={first} />); rerender( {...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( 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[] = [ { 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"); }); });