The Companies card clipped its Edit button at 360 px: the page's own button group did not wrap inside the shared actions cell. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
88 lines
3.7 KiB
TypeScript
88 lines
3.7 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; company: string; tag: string };
|
|
const columns: ColumnDef<Row, unknown>[] = [
|
|
{ accessorKey: "name", header: "Name" },
|
|
{ accessorKey: "company", header: "Company" },
|
|
{ accessorKey: "tag", header: () => <span>Tag</span>, enableSorting: false },
|
|
{ id: "actions", header: "Actions", enableSorting: false, cell: () => <button>Act</button> },
|
|
];
|
|
const data: Row[] = [{ id: "1", name: "Alpha", company: "INS", tag: "x" }];
|
|
|
|
function renderTable(extra: Partial<React.ComponentProps<typeof DataTable<Row>>> = {}) {
|
|
return render(
|
|
<DataTable<Row> label="t" columns={columns} data={data} total={1} page={1} pageSize={20}
|
|
onPageChange={() => {}} rowKey={r => r.id} {...extra} />,
|
|
);
|
|
}
|
|
const cells = () => Array.from(screen.getByText("Alpha").closest("tr")!.querySelectorAll("td"));
|
|
|
|
describe("DataTable phone cards", () => {
|
|
it("labels non-first cells with the header, leaves the title cell unlabelled", () => {
|
|
renderTable();
|
|
const [title, company] = cells();
|
|
expect(title).not.toHaveAttribute("data-label");
|
|
expect(company).toHaveAttribute("data-label", "Company");
|
|
});
|
|
|
|
it("gives a non-string header no label", () => {
|
|
renderTable();
|
|
expect(cells()[2]).not.toHaveAttribute("data-label");
|
|
});
|
|
|
|
it("puts the actions cell last, unlabelled", () => {
|
|
renderTable();
|
|
const actions = cells()[3];
|
|
expect(actions).not.toHaveAttribute("data-label");
|
|
expect(actions.className).toContain("max-md:order-last");
|
|
});
|
|
|
|
// jsdom applies no CSS, so these guard the classes that keep a card from overflowing at 360 px.
|
|
it("lets the card grow past the table row height", () => {
|
|
renderTable();
|
|
expect(screen.getByText("Alpha").closest("tr")!.className).toContain("max-md:h-auto");
|
|
});
|
|
|
|
it("wraps long title and values instead of scrolling sideways", () => {
|
|
renderTable();
|
|
const [title, company] = cells();
|
|
expect(title.className).toContain("max-md:whitespace-normal");
|
|
expect(title.className).toContain("max-md:[overflow-wrap:anywhere]");
|
|
expect(company.className).toContain("max-md:[overflow-wrap:anywhere]");
|
|
});
|
|
|
|
// Pages wrap their buttons in one `flex gap-2` div; that div must wrap too, or the row clips at 360 px.
|
|
it("wraps the page's own button group inside the actions cell", () => {
|
|
renderTable();
|
|
const actions = cells()[3];
|
|
expect(actions.className).toContain("max-md:[&>div]:flex-wrap");
|
|
expect(actions.className).toContain("max-md:[&>div]:justify-end");
|
|
});
|
|
});
|
|
|
|
describe("DataTable phone sort select", () => {
|
|
it("is absent without onSortingChange", () => {
|
|
renderTable();
|
|
expect(screen.queryByRole("combobox", { name: "Sort by" })).toBeNull();
|
|
});
|
|
|
|
it("lists sortable columns only, both directions", () => {
|
|
renderTable({ onSortingChange: () => {} });
|
|
const select = screen.getByRole("combobox", { name: "Sort by" });
|
|
const texts = Array.from(select.querySelectorAll("option")).map(o => o.textContent);
|
|
expect(texts).toEqual(["Sort by", "Name ↑", "Name ↓", "Company ↑", "Company ↓"]);
|
|
});
|
|
|
|
it("reflects the current sorting and reports changes", () => {
|
|
const onSortingChange = vi.fn();
|
|
renderTable({ onSortingChange, sorting: [{ id: "company", desc: true }] });
|
|
const select = screen.getByRole("combobox", { name: "Sort by" }) as HTMLSelectElement;
|
|
expect(select.value).toBe("company:desc");
|
|
fireEvent.change(select, { target: { value: "name:asc" } });
|
|
expect(onSortingChange).toHaveBeenCalledWith([{ id: "name", desc: false }]);
|
|
});
|
|
});
|