forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

data-table.test.tsx226 lines, 9.0 KiBHistoryRawDownload
1import { fireEvent, render, screen, within } from "@solidjs/testing-library";
2import type { ColumnDef } from "@tanstack/solid-table";
3import { DataTable } from "@/components/ui/data-table";
4import { DataSection } from "@/components/ui/data-section";
5import { PageHeader } from "@/components/layout/page-header";
6import { PreferencesProvider } from "@/stores/preferences-context";
7
8type Row = { name: string; role?: string };
9
10const columns: ColumnDef<Row>[] = [
11 { accessorKey: "name", header: "Name" },
12 { id: "update", header: "Role", cell: () => "Role" },
13 {
14 id: "actions",
15 header: "Actions",
16 meta: { headerClass: "w-14", cellClass: "w-40 text-right" },
17 cell: () => "…",
18 },
19];
20
21beforeEach(() => {
22 localStorage.clear();
23 vi.spyOn(window, "scrollTo").mockImplementation(() => undefined);
24});
25afterEach(() => vi.restoreAllMocks());
26
27test("keeps only the rightmost action column sticky and fixed at 110x45", () => {
28 render(() => (
29 <PreferencesProvider>
30 <DataTable columns={columns} data={[{ name: "Ada" }]} enableColumnVisibility={false} />
31 </PreferencesProvider>
32 ));
33
34 const actionsHeader = screen.getByRole("columnheader", { name: "Actions" });
35 expect(actionsHeader.classList).toContain("table-sticky-head-right");
36 expect(actionsHeader.classList).toContain("table-action-cell");
37 expect(actionsHeader.classList).toContain("h-[45px]");
38 expect(actionsHeader.classList).toContain("w-[110px]");
39 expect(actionsHeader.classList).not.toContain("w-14");
40 expect(actionsHeader.style.width).toBe("110px");
41 expect(actionsHeader.classList).toContain("z-30");
42 const actionsCell = screen.getByRole("cell", { name: "…" });
43 expect(actionsCell.classList).toContain("table-action-cell");
44 expect(actionsCell.classList).toContain("h-[45px]");
45 expect(actionsCell.classList).toContain("w-[110px]");
46 expect(actionsCell.classList).not.toContain("w-40");
47 expect(actionsCell.classList).toContain("text-center");
48 expect(actionsCell.classList).not.toContain("text-right");
49 expect(actionsCell.style.width).toBe("110px");
50 const columnTracks = document.querySelectorAll("col");
51 expect(columnTracks[0]?.style.width).toBe("calc(50% - 55px)");
52 expect(columnTracks[1]?.style.width).toBe("calc(50% - 55px)");
53 expect(columnTracks[2]?.style.width).toBe("110px");
54 const roleHeader = screen.getAllByRole("columnheader").find((header) => header.textContent?.startsWith("Role"));
55 expect(roleHeader?.classList).not.toContain("table-sticky-head-right");
56});
57
58test("freezes the meta.stickyLeft column and keeps the table centered", () => {
59 const alignedColumns: ColumnDef<Row>[] = [
60 { accessorKey: "name", header: "Name", meta: { stickyLeft: true } },
61 { accessorKey: "role", header: "Role", meta: { align: "right" } },
62 ];
63 render(() => (
64 <PreferencesProvider>
65 <DataTable columns={alignedColumns} data={[{ name: "Ada", role: "Admin" }]} enableColumnVisibility={false} enableSorting={false} />
66 </PreferencesProvider>
67 ));
68
69 const headers = screen.getAllByRole("columnheader");
70 const nameHeader = headers.find((header) => header.textContent?.startsWith("Name"));
71 expect(nameHeader?.classList).toContain("table-sticky-head-left");
72 expect(nameHeader?.classList).toContain("left-0");
73
74 const roleHeader = headers.find((header) => header.textContent?.startsWith("Role"));
75 const roleCell = screen.getByRole("cell", { name: "Admin" });
76 // List tables use one consistent centered alignment, including numeric columns.
77 expect(roleHeader?.classList).toContain("text-center");
78 expect(roleCell.classList).toContain("text-center");
79});
80
81test("paginates tables by default", () => {
82 const rows = Array.from({ length: 11 }, (_, index) => ({ name: `Person ${index + 1}` }));
83 render(() => (
84 <PreferencesProvider>
85 <DataTable columns={[columns[0]]} data={rows} enableColumnVisibility={false} />
86 </PreferencesProvider>
87 ));
88
89 expect(screen.getByText("Person 10")).toBeTruthy();
90 expect(screen.queryByText("Person 11")).toBeNull();
91 fireEvent.click(screen.getByRole("button", { name: "Next" }));
92 expect(screen.getByText("Person 11")).toBeTruthy();
93});
94
95test("shows the pagination footer on a single page", () => {
96 render(() => (
97 <PreferencesProvider>
98 <DataTable columns={[columns[0]]} data={[{ name: "Ada" }]} enableColumnVisibility={false} />
99 </PreferencesProvider>
100 ));
101
102 expect(screen.getByText("1 / 1")).toBeTruthy();
103 expect((screen.getByRole("button", { name: "Previous" }) as HTMLButtonElement).disabled).toBe(true);
104 expect((screen.getByRole("button", { name: "Next" }) as HTMLButtonElement).disabled).toBe(true);
105});
106
107test("a display column renders its own cell instead of the empty dash", () => {
108 // A column with no accessorFn has no value to be empty, so the empty-cell
109 // fallback must not swallow its render function.
110 render(() => (
111 <PreferencesProvider>
112 <DataTable
113 columns={[
114 { accessorKey: "name", header: "Name" },
115 { id: "label", header: "Label", cell: (c) => `#${c.row.original.name}` },
116 ] as ColumnDef<Row>[]}
117 data={[{ name: "ada" }]}
118 />
119 </PreferencesProvider>
120 ));
121
122 expect(screen.getByText("#ada")).toBeTruthy();
123});
124
125test("the header action toolbar of every shared host can shrink below its max-content width", () => {
126 // `shrink-0` plus the flex default `min-width: auto` pinned the toolbar to
127 // its max-content width, so a wide action row pushed the whole document
128 // past a 390px viewport instead of wrapping inside the constrained parent.
129 render(() => (
130 <>
131 <PreferencesProvider>
132 <DataTable
133 columns={[columns[0]]}
134 data={[{ name: "Ada" }]}
135 enableColumnVisibility={false}
136 actions={<button type="button">Export</button>}
137 />
138 </PreferencesProvider>
139 <DataSection title="Card" actions={<button type="button">Export</button>} />
140 <PageHeader title="Page" actions={<button type="button">Export</button>} />
141 </>
142 ));
143
144 const toolbars = screen.getAllByText("Export").map((action) => action.parentElement as HTMLElement);
145 expect(toolbars).toHaveLength(3);
146 for (const toolbar of toolbars) {
147 expect(toolbar.classList).toContain("flex-wrap");
148 expect(toolbar.classList).toContain("min-w-0");
149 expect(toolbar.classList).not.toContain("shrink-0");
150 }
151});
152
153test("a search that matches nothing says so and clears back to the list", () => {
154 render(() => (
155 <PreferencesProvider>
156 <DataTable
157 columns={columns}
158 data={[{ name: "Ada" }]}
159 empty="No people yet."
160 enableColumnVisibility={false}
161 searchPredicate={(row, query) => row.name.toLowerCase().includes(query.toLowerCase())}
162 />
163 </PreferencesProvider>
164 ));
165
166 fireEvent.input(screen.getByRole("textbox"), { target: { value: "zzz" } });
167 expect(screen.queryByText("No people yet.")).toBeNull();
168 expect(screen.getByText(/Nothing matches|eşleşen kayıt yok/)).toBeTruthy();
169
170 fireEvent.click(within(screen.getByRole("table")).getByRole("button", { name: /Clear search|Aramayı temizle/ }));
171 expect(screen.getByText("Ada")).toBeTruthy();
172});
173
174test("a server-paged table offers no header sort, since it could only sort one page", () => {
175 const sortable: ColumnDef<Row>[] = [{ accessorKey: "name", header: "Name" }];
176 const { unmount } = render(() => (
177 <PreferencesProvider>
178 <DataTable columns={sortable} data={[{ name: "Ada" }]} enableColumnVisibility={false} />
179 </PreferencesProvider>
180 ));
181 expect(screen.getByRole("button", { name: /Name/ })).toBeTruthy();
182 unmount();
183
184 render(() => (
185 <PreferencesProvider>
186 <DataTable
187 columns={sortable}
188 data={[{ name: "Ada" }]}
189 enableColumnVisibility={false}
190 manualPagination={{ pageIndex: 0, pageSize: 10, total: 40, onPageChange: () => {} }}
191 />
192 </PreferencesProvider>
193 ));
194 expect(screen.queryByRole("button", { name: /^Name/ })).toBeNull();
195});
196
197test("on a phone-width screen rows render as labelled cards instead of a sideways table", () => {
198 vi.stubGlobal("matchMedia", (query: string) => ({
199 matches: query.includes("max-width"),
200 addEventListener: () => {},
201 removeEventListener: () => {},
202 }));
203 const cardColumns: ColumnDef<Row>[] = [
204 { accessorKey: "name", header: "Name" },
205 { accessorKey: "role", header: "Role" },
206 { id: "actions", header: "Actions", cell: () => <button type="button">Menu</button> },
207 ];
208 const onRowClick = vi.fn();
209 render(() => (
210 <PreferencesProvider>
211 <DataTable columns={cardColumns} data={[{ name: "Ada", role: "teacher" }]} enableColumnVisibility={false} onRowClick={onRowClick} />
212 </PreferencesProvider>
213 ));
214
215 expect(screen.queryByRole("table")).toBeNull();
216 const card = screen.getByRole("listitem");
217 expect(within(card).getByText("Ada")).toBeTruthy();
218 expect(within(card).getByText("Role")).toBeTruthy();
219 expect(within(card).getByText("teacher")).toBeTruthy();
220
221 fireEvent.click(within(card).getByRole("button", { name: "Menu" }));
222 expect(onRowClick).not.toHaveBeenCalled();
223 fireEvent.click(within(card).getByText("teacher"));
224 expect(onRowClick).toHaveBeenCalledOnce();
225 vi.unstubAllGlobals();
226});