forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

data-table.test.tsx103 lines, 4.4 KiBHistoryRawDownload
1import { fireEvent, render, screen } from "@solidjs/testing-library";
2import type { ColumnDef } from "@tanstack/solid-table";
3import { DataTable } from "@/components/ui/data-table";
4import { PreferencesProvider } from "@/stores/preferences-context";
5
6type Row = { name: string; role?: string };
7
8const columns: ColumnDef<Row>[] = [
9 { accessorKey: "name", header: "Name" },
10 { id: "update", header: "Role", cell: () => "Role" },
11 {
12 id: "actions",
13 header: "Actions",
14 meta: { headerClass: "w-14", cellClass: "w-40 text-right" },
15 cell: () => "…",
16 },
17];
18
19beforeEach(() => {
20 localStorage.clear();
21 vi.spyOn(window, "scrollTo").mockImplementation(() => undefined);
22});
23afterEach(() => vi.restoreAllMocks());
24
25test("keeps only the rightmost action column sticky and fixed at 110x45", () => {
26 render(() => (
27 <PreferencesProvider>
28 <DataTable columns={columns} data={[{ name: "Ada" }]} enableColumnVisibility={false} />
29 </PreferencesProvider>
30 ));
31
32 const actionsHeader = screen.getByRole("columnheader", { name: "Actions" });
33 expect(actionsHeader.classList).toContain("table-sticky-head-right");
34 expect(actionsHeader.classList).toContain("table-action-cell");
35 expect(actionsHeader.classList).toContain("h-[45px]");
36 expect(actionsHeader.classList).toContain("w-[110px]");
37 expect(actionsHeader.classList).not.toContain("w-14");
38 expect(actionsHeader.style.width).toBe("110px");
39 expect(actionsHeader.classList).toContain("z-30");
40 const actionsCell = screen.getByRole("cell", { name: "…" });
41 expect(actionsCell.classList).toContain("table-action-cell");
42 expect(actionsCell.classList).toContain("h-[45px]");
43 expect(actionsCell.classList).toContain("w-[110px]");
44 expect(actionsCell.classList).not.toContain("w-40");
45 expect(actionsCell.classList).toContain("text-center");
46 expect(actionsCell.classList).not.toContain("text-right");
47 expect(actionsCell.style.width).toBe("110px");
48 const columnTracks = document.querySelectorAll("col");
49 expect(columnTracks[0]?.style.width).toBe("calc(50% - 55px)");
50 expect(columnTracks[1]?.style.width).toBe("calc(50% - 55px)");
51 expect(columnTracks[2]?.style.width).toBe("110px");
52 const roleHeader = screen.getAllByRole("columnheader").find((header) => header.textContent?.startsWith("Role"));
53 expect(roleHeader?.classList).not.toContain("table-sticky-head-right");
54});
55
56test("freezes the meta.stickyLeft column and aligns head + cell together", () => {
57 const alignedColumns: ColumnDef<Row>[] = [
58 { accessorKey: "name", header: "Name", meta: { stickyLeft: true } },
59 { accessorKey: "role", header: "Role", meta: { align: "right" } },
60 ];
61 render(() => (
62 <PreferencesProvider>
63 <DataTable columns={alignedColumns} data={[{ name: "Ada", role: "Admin" }]} enableColumnVisibility={false} enableSorting={false} />
64 </PreferencesProvider>
65 ));
66
67 const headers = screen.getAllByRole("columnheader");
68 const nameHeader = headers.find((header) => header.textContent?.startsWith("Name"));
69 expect(nameHeader?.classList).toContain("table-sticky-head-left");
70 expect(nameHeader?.classList).toContain("left-0");
71
72 const roleHeader = headers.find((header) => header.textContent?.startsWith("Role"));
73 const roleCell = screen.getByRole("cell", { name: "Admin" });
74 // Same alignment class on head and cell so columns never drift apart.
75 expect(roleHeader?.classList).toContain("text-right");
76 expect(roleCell.classList).toContain("text-right");
77});
78
79test("paginates tables by default", () => {
80 const rows = Array.from({ length: 11 }, (_, index) => ({ name: `Person ${index + 1}` }));
81 render(() => (
82 <PreferencesProvider>
83 <DataTable columns={[columns[0]]} data={rows} enableColumnVisibility={false} />
84 </PreferencesProvider>
85 ));
86
87 expect(screen.getByText("Person 10")).toBeTruthy();
88 expect(screen.queryByText("Person 11")).toBeNull();
89 fireEvent.click(screen.getByRole("button", { name: "Next" }));
90 expect(screen.getByText("Person 11")).toBeTruthy();
91});
92
93test("shows the pagination footer on a single page", () => {
94 render(() => (
95 <PreferencesProvider>
96 <DataTable columns={[columns[0]]} data={[{ name: "Ada" }]} enableColumnVisibility={false} />
97 </PreferencesProvider>
98 ));
99
100 expect(screen.getByText("1 / 1")).toBeTruthy();
101 expect((screen.getByRole("button", { name: "Previous" }) as HTMLButtonElement).disabled).toBe(true);
102 expect((screen.getByRole("button", { name: "Next" }) as HTMLButtonElement).disabled).toBe(true);
103});