import { createSignal } from "solid-js"; import { Portal } from "solid-js/web"; import { fireEvent, render, screen, within } from "@solidjs/testing-library"; import type { ColumnDef } from "@tanstack/solid-table"; import { DataTable } from "@/components/ui/data-table"; import { DataSection } from "@/components/ui/data-section"; import { PageHeader } from "@/components/layout/page-header"; import { PreferencesProvider } from "@/stores/preferences-context"; type Row = { name: string; role?: string }; const columns: ColumnDef[] = [ { accessorKey: "name", header: "Name" }, { id: "update", header: "Role", cell: () => "Role" }, { id: "actions", header: "Actions", meta: { headerClass: "w-14", cellClass: "w-40 text-right" }, cell: () => "…", }, ]; beforeEach(() => { localStorage.clear(); vi.spyOn(window, "scrollTo").mockImplementation(() => undefined); }); afterEach(() => vi.restoreAllMocks()); test("keeps only the rightmost action column sticky and fixed at 110x45", () => { render(() => ( )); const actionsHeader = screen.getByRole("columnheader", { name: "Actions" }); expect(actionsHeader.classList).toContain("table-sticky-head-right"); expect(actionsHeader.classList).toContain("table-action-cell"); expect(actionsHeader.classList).toContain("h-[45px]"); expect(actionsHeader.classList).toContain("w-[110px]"); expect(actionsHeader.classList).not.toContain("w-14"); expect(actionsHeader.style.width).toBe("110px"); expect(actionsHeader.classList).toContain("z-30"); const actionsCell = screen.getByRole("cell", { name: "…" }); expect(actionsCell.classList).toContain("table-action-cell"); expect(actionsCell.classList).toContain("h-[45px]"); expect(actionsCell.classList).toContain("w-[110px]"); expect(actionsCell.classList).not.toContain("w-40"); expect(actionsCell.classList).toContain("text-center"); expect(actionsCell.classList).not.toContain("text-right"); expect(actionsCell.style.width).toBe("110px"); const columnTracks = document.querySelectorAll("col"); // Plain percentages: Chrome reads a %-and-px calc() on a table column as // auto and splits the table evenly, whatever each column's size says. expect(columnTracks[0]?.style.width).toBe("50%"); expect(columnTracks[1]?.style.width).toBe("50%"); expect(columnTracks[2]?.style.width).toBe("110px"); const roleHeader = screen.getAllByRole("columnheader").find((header) => header.textContent?.startsWith("Role")); expect(roleHeader?.classList).not.toContain("table-sticky-head-right"); }); test("freezes the meta.stickyLeft column and centres headers and left-aligns cells", () => { const alignedColumns: ColumnDef[] = [ { accessorKey: "name", header: "Name", meta: { stickyLeft: true } }, { accessorKey: "role", header: "Role", meta: { align: "right" } }, ]; render(() => ( )); const headers = screen.getAllByRole("columnheader"); const nameHeader = headers.find((header) => header.textContent?.startsWith("Name")); expect(nameHeader?.classList).toContain("table-sticky-head-left"); expect(nameHeader?.classList).toContain("left-0"); const roleHeader = headers.find((header) => header.textContent?.startsWith("Role")); const roleCell = screen.getByRole("cell", { name: "Admin" }); // List tables use one consistent centered alignment, including numeric columns. expect(roleHeader?.classList).toContain("text-center"); expect(roleCell.classList).toContain("text-left"); }); /** Stand-in IntersectionObserver whose callback a test fires by hand. */ function stubIntersectionObserver() { const callbacks: IntersectionObserverCallback[] = []; class FakeObserver { constructor(callback: IntersectionObserverCallback) { callbacks.push(callback); } observe() {} disconnect() {} } vi.stubGlobal("IntersectionObserver", FakeObserver); return () => { for (const callback of callbacks) callback([{ isIntersecting: true } as IntersectionObserverEntry], {} as IntersectionObserver); }; } test("paginates client rows without observing the scroll position", () => { const reachEnd = stubIntersectionObserver(); const rows = Array.from({ length: 21 }, (_, index) => ({ name: `Person ${index + 1}` })); try { render(() => ( )); expect(screen.getByText("Person 10")).toBeTruthy(); expect(screen.queryByText("Person 11")).toBeNull(); expect(screen.getByText("1 / 3")).toBeTruthy(); reachEnd(); expect(screen.queryByText("Person 11")).toBeNull(); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); expect(screen.getByText("Person 11")).toBeTruthy(); } finally { vi.unstubAllGlobals(); } }); test("reports rendered client and manual page rows", () => { const rows = Array.from({ length: 12 }, (_, index) => ({ name: `Person ${index + 1}` })); const onPageRowsChange = vi.fn(); const client = render(() => ( )); expect(onPageRowsChange.mock.lastCall?.[0].map((row: Row) => row.name)).toEqual(rows.slice(0, 10).map((row) => row.name)); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); expect(onPageRowsChange.mock.lastCall?.[0].map((row: Row) => row.name)).toEqual(["Person 11", "Person 12"]); client.unmount(); onPageRowsChange.mockClear(); const [page, setPage] = createSignal(0); render(() => ( )); expect(onPageRowsChange.mock.lastCall?.[0].map((row: Row) => row.name)).toEqual(rows.slice(0, 5).map((row) => row.name)); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); expect(onPageRowsChange.mock.lastCall?.[0].map((row: Row) => row.name)).toEqual(rows.slice(5, 10).map((row) => row.name)); }); test("without IntersectionObserver client rows still paginate", () => { vi.stubGlobal("IntersectionObserver", undefined); const rows = Array.from({ length: 60 }, (_, index) => ({ name: `Person ${index + 1}` })); try { render(() => ( )); expect(screen.queryByText("Person 60")).toBeNull(); expect(screen.getByText("1 / 6")).toBeTruthy(); } finally { vi.unstubAllGlobals(); } }); test("uses a shorter client page on phones", () => { vi.stubGlobal("matchMedia", (query: string) => ({ matches: query.includes("max-width"), media: query, addEventListener: () => undefined, removeEventListener: () => undefined, })); try { render(() => ( ({ name: `Person ${index + 1}` }))} enableColumnVisibility={false} /> )); expect(screen.getByText("Person 5")).toBeTruthy(); expect(screen.queryByText("Person 6")).toBeNull(); expect(screen.getByText("1 / 3")).toBeTruthy(); } finally { vi.unstubAllGlobals(); } }); test("search resets a client page and shrinking data clamps its index", () => { const [rows, setRows] = createSignal(Array.from({ length: 22 }, (_, index) => ({ name: `Person ${index + 1}` }))); render(() => ( row.name.toLowerCase().includes(query.toLowerCase())} /> )); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); expect(screen.getByText("Person 22")).toBeTruthy(); setRows((current) => current.slice(0, 12)); expect(screen.getByText("Person 12")).toBeTruthy(); fireEvent.input(screen.getByRole("textbox"), { target: { value: "Person 12" } }); expect(screen.getByText("Person 12")).toBeTruthy(); }); test("a display column renders its own cell instead of the empty dash", () => { // A column with no accessorFn has no value to be empty, so the empty-cell // fallback must not swallow its render function. render(() => ( `#${c.row.original.name}` }, ] as ColumnDef[]} data={[{ name: "ada" }]} /> )); expect(screen.getByText("#ada")).toBeTruthy(); }); test("the header action toolbar of every shared host can shrink below its max-content width", () => { // `shrink-0` plus the flex default `min-width: auto` pinned the toolbar to // its max-content width, so a wide action row pushed the whole document // past a 390px viewport instead of wrapping inside the constrained parent. render(() => ( <> Export} /> Export} /> Export} /> )); const toolbars = screen.getAllByText("Export").map((action) => action.parentElement as HTMLElement); expect(toolbars).toHaveLength(3); for (const toolbar of toolbars) { expect(toolbar.classList).toContain("flex-wrap"); expect(toolbar.classList).toContain("min-w-0"); expect(toolbar.classList).not.toContain("shrink-0"); } }); test("a search that matches nothing says so and clears back to the list", () => { render(() => ( row.name.toLowerCase().includes(query.toLowerCase())} /> )); fireEvent.input(screen.getByRole("textbox"), { target: { value: "zzz" } }); expect(screen.queryByText("No people yet.")).toBeNull(); expect(screen.getByText(/No results for “zzz”|“zzz” için sonuç yok/)).toBeTruthy(); fireEvent.click(within(screen.getByRole("table")).getByRole("button", { name: /Clear search|Aramayı temizle/ })); expect(screen.getByText("Ada")).toBeTruthy(); }); test("a server-paged table offers no header sort, since it could only sort one page", () => { const sortable: ColumnDef[] = [{ accessorKey: "name", header: "Name" }]; const { unmount } = render(() => ( )); expect(screen.getByRole("button", { name: /Name/ })).toBeTruthy(); unmount(); render(() => ( {} }} /> )); expect(screen.queryByRole("button", { name: /^Name/ })).toBeNull(); }); test("on a phone-width screen rows render as labelled cards instead of a sideways table", () => { vi.stubGlobal("matchMedia", (query: string) => ({ matches: query.includes("max-width"), addEventListener: () => {}, removeEventListener: () => {}, })); const cardColumns: ColumnDef[] = [ { accessorKey: "name", header: "Name" }, { accessorKey: "role", header: "Role" }, { id: "actions", header: "Actions", cell: () => }, ]; const onRowClick = vi.fn(); render(() => ( )); expect(screen.queryByRole("table")).toBeNull(); const card = screen.getByRole("listitem"); expect(within(card).getByText("Ada")).toBeTruthy(); expect(within(card).getByText("Role")).toBeTruthy(); expect(within(card).getByText("teacher")).toBeTruthy(); fireEvent.click(within(card).getByRole("button", { name: "Menu" })); expect(onRowClick).not.toHaveBeenCalled(); fireEvent.click(within(card).getByText("teacher")); expect(onRowClick).toHaveBeenCalledOnce(); vi.unstubAllGlobals(); }); test("a pick in a row's portalled menu does not also open the row", () => { const portalColumns: ColumnDef[] = [ { accessorKey: "name", header: "Name" }, { id: "actions", header: "Actions", cell: () => (
Edit
), }, ]; const onRowClick = vi.fn(); render(() => ( )); fireEvent.click(screen.getAllByRole("menuitem", { name: "Edit" })[0]!); expect(onRowClick).not.toHaveBeenCalled(); fireEvent.click(screen.getAllByText("Ada")[0]!); expect(onRowClick).toHaveBeenCalledOnce(); }); test("sortable headers expose their order through aria-sort", () => { const sortable: ColumnDef[] = [ { accessorKey: "name", header: "Name" }, { accessorKey: "role", header: "Role", enableSorting: false }, ]; render(() => ( )); const [nameHeader, roleHeader] = screen.getAllByRole("columnheader"); expect(nameHeader?.getAttribute("aria-sort")).toBe("none"); expect(roleHeader?.hasAttribute("aria-sort")).toBe(false); fireEvent.click(within(nameHeader!).getByRole("button")); expect(nameHeader?.getAttribute("aria-sort")).toBe("ascending"); fireEvent.click(within(nameHeader!).getByRole("button")); expect(nameHeader?.getAttribute("aria-sort")).toBe("descending"); }); test("an empty list narrowed by the caller's filters offers to clear them", () => { const onClearFilters = vi.fn(); render(() => ( )); expect(screen.queryByText("No people yet.")).toBeNull(); fireEvent.click(screen.getByRole("button", { name: /Clear filters|Filtreleri temizle/ })); expect(onClearFilters).toHaveBeenCalledOnce(); }); test("the client page holds when the caller hands over a fresh data array", () => { const [tick, setTick] = createSignal(0); const people = Array.from({ length: 70 }, (_, i) => ({ name: `P${String(i).padStart(2, "0")}` })); render(() => ( ({ ...row })))} enableColumnVisibility={false} /> )); fireEvent.click(screen.getByRole("button", { name: /Next|Sonraki/ })); expect(screen.getByText("P10")).toBeTruthy(); setTick(1); expect(screen.getByText("P10")).toBeTruthy(); expect(screen.queryByText("P00")).toBeNull(); }); test("a toolbar holding only the column menu still gets its card", () => { const { container } = render(() => ( )); const toolbar = container.querySelector(".space-y-3 > div"); expect(toolbar?.className).toContain("rounded-xl"); }); test("page actions in the toolbar take the shared control height", () => { const { container } = render(() => ( Add} /> )); const add = [...container.querySelectorAll("button")].find((el) => el.textContent === "Add"); expect(add?.parentElement?.className).toContain("sm:[&_button]:h-8"); }); test("meta.headerInfo puts a labelled info icon beside the header that opens without sorting", async () => { const withInfo: ColumnDef[] = [ { accessorKey: "name", header: "Name" }, { accessorKey: "role", header: "Average", meta: { headerInfo: "Filled in for small schools only." } }, ]; render(() => ( )); const info = screen.getByRole("button", { name: /About Average|Average hakkında bilgi/ }); const header = info.closest("th"); expect(header?.getAttribute("aria-sort")).toBe("none"); // Beside the sort button, never inside it: a button in a button is invalid. expect(info.parentElement?.closest("button")).toBeNull(); expect(screen.getAllByRole("columnheader")).toHaveLength(2); expect(screen.queryByText("Filled in for small schools only.")).toBeNull(); fireEvent.click(info); expect(header?.getAttribute("aria-sort")).toBe("none"); expect(await screen.findByText("Filled in for small schools only.")).toBeTruthy(); // No always-visible copy of the note is left in the table. expect(document.querySelector("table")?.textContent).not.toContain("Filled in for small schools only."); }); test("an infinite table asks for the next page near its end and does not sort", async () => { const reachEnd = stubIntersectionObserver(); const onLoadMore = vi.fn(); const rows = Array.from({ length: 50 }, (_, index) => ({ name: `Person ${index + 1}` })); try { render(() => ( )); expect(screen.getByText("Person 50")).toBeTruthy(); expect(screen.getByText(/(Showing 50 of 120|120 kayıttan 50)/)).toBeTruthy(); expect(screen.getByRole("columnheader").getAttribute("aria-sort")).not.toBe("ascending"); reachEnd(); expect(onLoadMore).toHaveBeenCalledTimes(1); } finally { vi.unstubAllGlobals(); } });