@nickrobison/colophon-table 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +55 -2
- package/package.json +72 -3
- package/src/components/CphSkeleton.test.tsx +145 -0
- package/src/components/CphSkeleton.tsx +42 -0
- package/src/components/CphTableEmpty.test.tsx +122 -0
- package/src/components/CphTableEmpty.tsx +42 -0
- package/src/components/DataTable/CphDataTable.test.tsx +408 -0
- package/src/components/DataTable/CphDataTable.tsx +121 -0
- package/src/components/DataTable/pinnedOffsets.ts +94 -0
- package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.test.tsx +172 -0
- package/src/components/FilterMultiSelect/CphTableFilterMultiSelect.tsx +132 -0
- package/src/components/FilterRange/CphTableFilterRange.test.tsx +154 -0
- package/src/components/FilterRange/CphTableFilterRange.tsx +109 -0
- package/src/components/MobileCards/CphMobileCards.test.tsx +174 -0
- package/src/components/MobileCards/CphMobileCards.tsx +117 -0
- package/src/components/Pagination/CphCursorPagination.test.tsx +51 -0
- package/src/components/Pagination/CphCursorPagination.tsx +74 -0
- package/src/components/Pagination/CphPagination.test.tsx +98 -0
- package/src/components/Pagination/CphPagination.tsx +94 -0
- package/src/components/RowDetail/CphRowDetail.test.tsx +182 -0
- package/src/components/RowDetail/CphRowDetail.tsx +66 -0
- package/src/components/SimpleTable/CphSimpleTable.test.tsx +235 -0
- package/src/components/SimpleTable/CphSimpleTable.tsx +369 -0
- package/src/components/SortFilterSheet/CphSortFilterSheet.test.tsx +156 -0
- package/src/components/SortFilterSheet/CphSortFilterSheet.tsx +131 -0
- package/src/components/StatusChip/CphStatusChip.test.tsx +43 -0
- package/src/components/StatusChip/CphStatusChip.tsx +33 -0
- package/src/components/Summary/CphTableSummary.test.tsx +486 -0
- package/src/components/Summary/CphTableSummary.tsx +130 -0
- package/src/components/TableHead/CphTableHead.test.tsx +52 -0
- package/src/components/TableHead/CphTableHead.tsx +129 -0
- package/src/components/TableHead/HeaderCell.test.tsx +880 -0
- package/src/components/TableHead/HeaderCell.tsx +221 -0
- package/src/components/TableRow/Cell.test.tsx +306 -0
- package/src/components/TableRow/Cell.tsx +74 -0
- package/src/components/TableRow/CphTableRow.test.tsx +90 -0
- package/src/components/TableRow/CphTableRow.tsx +17 -0
- package/src/components/Toolbar/CphToolbar.test.tsx +150 -0
- package/src/components/Toolbar/CphToolbar.tsx +138 -0
- package/src/components.css +593 -0
- package/src/index.ts +69 -0
- package/src/table/cellRenderers.tsx +88 -0
- package/src/table/column.test.tsx +277 -0
- package/src/table/column.tsx +36 -0
- package/src/table/features.ts +69 -0
- package/src/table/pageSize.test.ts +128 -0
- package/src/table/pageSize.ts +47 -0
- package/src/table/summary.test.tsx +446 -0
- package/src/table/summary.ts +94 -0
- package/src/table/useCphTable.ts +90 -0
- package/src/table/useCphTableState.test.tsx +697 -0
- package/src/table/useCphTableState.ts +412 -0
- package/src/table-tokens.css +48 -0
- package/src/theme/CphTableDensity.test.tsx +53 -0
- package/src/theme/CphTableDensity.tsx +24 -0
- package/src/theme/cphTableDensityContext.ts +24 -0
- package/src/utils/composeClassName.test.ts +26 -0
- package/src/utils/composeClassName.ts +28 -0
|
@@ -0,0 +1,697 @@
|
|
|
1
|
+
/** @packageDocumentation Tests for useCphTableState and useCphRowSelection. */
|
|
2
|
+
|
|
3
|
+
import { act, renderHook } from "@testing-library/react";
|
|
4
|
+
import { describe, expect, it, vi } from "vitest";
|
|
5
|
+
|
|
6
|
+
import { DEFAULT_PAGE_SIZE } from "./pageSize";
|
|
7
|
+
import {
|
|
8
|
+
useCphRowSelection,
|
|
9
|
+
useCphTableState,
|
|
10
|
+
type UseCphTableStateOptions,
|
|
11
|
+
} from "./useCphTableState";
|
|
12
|
+
|
|
13
|
+
type SortingState = Array<{ id: string; desc: boolean }>;
|
|
14
|
+
type ColumnFiltersState = Array<{ id: string; value: unknown }>;
|
|
15
|
+
type ColumnVisibilityState = Record<string, boolean>;
|
|
16
|
+
type ColumnPinningState = { start: string[]; end: string[] };
|
|
17
|
+
type RowSelectionState = Record<string, true>;
|
|
18
|
+
type ExpandedState = true | Record<string, boolean>;
|
|
19
|
+
type PaginationState = { pageIndex: number; pageSize: number };
|
|
20
|
+
|
|
21
|
+
const defaultSorting: SortingState = [];
|
|
22
|
+
const defaultColumnFilters: ColumnFiltersState = [];
|
|
23
|
+
const defaultColumnVisibility: ColumnVisibilityState = {};
|
|
24
|
+
const defaultColumnPinning: ColumnPinningState = { start: [], end: [] };
|
|
25
|
+
const defaultRowSelection: RowSelectionState = {};
|
|
26
|
+
const defaultExpanded: ExpandedState = {};
|
|
27
|
+
const defaultPagination: PaginationState = { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE };
|
|
28
|
+
|
|
29
|
+
function renderUncontrolled() {
|
|
30
|
+
return renderHook(() =>
|
|
31
|
+
useCphTableState({
|
|
32
|
+
sorting: { controlled: false },
|
|
33
|
+
columnFilters: { controlled: false },
|
|
34
|
+
columnVisibility: { controlled: false },
|
|
35
|
+
columnPinning: { controlled: false },
|
|
36
|
+
rowSelection: { controlled: false },
|
|
37
|
+
expanded: { controlled: false },
|
|
38
|
+
pagination: { controlled: false },
|
|
39
|
+
} as UseCphTableStateOptions),
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
describe("useCphTableState", () => {
|
|
44
|
+
describe("controlled slices", () => {
|
|
45
|
+
it("sorting: calls consumer onChange exactly once, no internal state", () => {
|
|
46
|
+
const onSortingChange = vi.fn();
|
|
47
|
+
const sorting: SortingState = [{ id: "col1", desc: true }];
|
|
48
|
+
|
|
49
|
+
const { result } = renderHook(() =>
|
|
50
|
+
useCphTableState({
|
|
51
|
+
sorting: { controlled: true, value: sorting, onChange: onSortingChange },
|
|
52
|
+
} as UseCphTableStateOptions),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// The controlled value should be in state
|
|
56
|
+
expect(result.current.state.sorting).toBe(sorting);
|
|
57
|
+
expect(result.current.onSortingChange).toBe(onSortingChange);
|
|
58
|
+
expect(result.current.initialState.sorting).toBeUndefined();
|
|
59
|
+
|
|
60
|
+
// Calling the change handler should call consumer exactly once
|
|
61
|
+
act(() => {
|
|
62
|
+
result.current.onSortingChange?.([{ id: "col2", desc: false }]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
expect(onSortingChange).toHaveBeenCalledTimes(1);
|
|
66
|
+
expect(onSortingChange).toHaveBeenCalledWith([{ id: "col2", desc: false }]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("columnFilters: calls consumer onChange exactly once", () => {
|
|
70
|
+
const onColumnFiltersChange = vi.fn();
|
|
71
|
+
const columnFilters: ColumnFiltersState = [{ id: "col1", value: "test" }];
|
|
72
|
+
|
|
73
|
+
const { result } = renderHook(() =>
|
|
74
|
+
useCphTableState({
|
|
75
|
+
columnFilters: {
|
|
76
|
+
controlled: true,
|
|
77
|
+
value: columnFilters,
|
|
78
|
+
onChange: onColumnFiltersChange,
|
|
79
|
+
},
|
|
80
|
+
} as UseCphTableStateOptions),
|
|
81
|
+
);
|
|
82
|
+
|
|
83
|
+
expect(result.current.state.columnFilters).toBe(columnFilters);
|
|
84
|
+
expect(result.current.onColumnFiltersChange).toBe(onColumnFiltersChange);
|
|
85
|
+
|
|
86
|
+
act(() => {
|
|
87
|
+
result.current.onColumnFiltersChange?.([{ id: "col2", value: "other" }]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
expect(onColumnFiltersChange).toHaveBeenCalledTimes(1);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("columnVisibility: calls consumer onChange exactly once", () => {
|
|
94
|
+
const onColumnVisibilityChange = vi.fn();
|
|
95
|
+
const columnVisibility: ColumnVisibilityState = { col1: true, col2: false };
|
|
96
|
+
|
|
97
|
+
const { result } = renderHook(() =>
|
|
98
|
+
useCphTableState({
|
|
99
|
+
columnVisibility: {
|
|
100
|
+
controlled: true,
|
|
101
|
+
value: columnVisibility,
|
|
102
|
+
onChange: onColumnVisibilityChange,
|
|
103
|
+
},
|
|
104
|
+
} as UseCphTableStateOptions),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
expect(result.current.state.columnVisibility).toBe(columnVisibility);
|
|
108
|
+
expect(result.current.onColumnVisibilityChange).toBe(onColumnVisibilityChange);
|
|
109
|
+
|
|
110
|
+
act(() => {
|
|
111
|
+
result.current.onColumnVisibilityChange?.({ col1: false, col2: true });
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
expect(onColumnVisibilityChange).toHaveBeenCalledTimes(1);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("columnPinning: calls consumer onChange exactly once", () => {
|
|
118
|
+
const onColumnPinningChange = vi.fn();
|
|
119
|
+
const columnPinning: ColumnPinningState = { start: ["col1"], end: ["col2"] };
|
|
120
|
+
|
|
121
|
+
const { result } = renderHook(() =>
|
|
122
|
+
useCphTableState({
|
|
123
|
+
columnPinning: {
|
|
124
|
+
controlled: true,
|
|
125
|
+
value: columnPinning,
|
|
126
|
+
onChange: onColumnPinningChange,
|
|
127
|
+
},
|
|
128
|
+
} as UseCphTableStateOptions),
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
expect(result.current.state.columnPinning).toBe(columnPinning);
|
|
132
|
+
expect(result.current.onColumnPinningChange).toBe(onColumnPinningChange);
|
|
133
|
+
|
|
134
|
+
act(() => {
|
|
135
|
+
result.current.onColumnPinningChange?.({ start: ["col2"], end: [] });
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
expect(onColumnPinningChange).toHaveBeenCalledTimes(1);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("rowSelection: calls consumer onChange exactly once", () => {
|
|
142
|
+
const onRowSelectionChange = vi.fn();
|
|
143
|
+
const rowSelection: RowSelectionState = { row1: true, row2: true };
|
|
144
|
+
|
|
145
|
+
const { result } = renderHook(() =>
|
|
146
|
+
useCphTableState({
|
|
147
|
+
rowSelection: { controlled: true, value: rowSelection, onChange: onRowSelectionChange },
|
|
148
|
+
} as UseCphTableStateOptions),
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
expect(result.current.state.rowSelection).toBe(rowSelection);
|
|
152
|
+
expect(result.current.onRowSelectionChange).toBe(onRowSelectionChange);
|
|
153
|
+
|
|
154
|
+
act(() => {
|
|
155
|
+
result.current.onRowSelectionChange?.({ row3: true });
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
expect(onRowSelectionChange).toHaveBeenCalledTimes(1);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("expanded: calls consumer onChange exactly once", () => {
|
|
162
|
+
const onExpandedChange = vi.fn();
|
|
163
|
+
const expanded: ExpandedState = { row1: true };
|
|
164
|
+
|
|
165
|
+
const { result } = renderHook(() =>
|
|
166
|
+
useCphTableState({
|
|
167
|
+
expanded: { controlled: true, value: expanded, onChange: onExpandedChange },
|
|
168
|
+
} as UseCphTableStateOptions),
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
expect(result.current.state.expanded).toBe(expanded);
|
|
172
|
+
expect(result.current.onExpandedChange).toBe(onExpandedChange);
|
|
173
|
+
|
|
174
|
+
act(() => {
|
|
175
|
+
result.current.onExpandedChange?.({ row2: true });
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
expect(onExpandedChange).toHaveBeenCalledTimes(1);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("pagination: calls consumer onChange exactly once", () => {
|
|
182
|
+
const onPaginationChange = vi.fn();
|
|
183
|
+
const pagination: PaginationState = { pageIndex: 2, pageSize: 20 };
|
|
184
|
+
|
|
185
|
+
const { result } = renderHook(() =>
|
|
186
|
+
useCphTableState({
|
|
187
|
+
pagination: { controlled: true, value: pagination, onChange: onPaginationChange },
|
|
188
|
+
} as UseCphTableStateOptions),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
expect(result.current.state.pagination).toBe(pagination);
|
|
192
|
+
expect(result.current.onPaginationChange).toBe(onPaginationChange);
|
|
193
|
+
|
|
194
|
+
act(() => {
|
|
195
|
+
result.current.onPaginationChange?.({ pageIndex: 3, pageSize: 20 });
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
expect(onPaginationChange).toHaveBeenCalledTimes(1);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("multiple controlled slices: each calls its own handler", () => {
|
|
202
|
+
const onSortingChange = vi.fn();
|
|
203
|
+
const onPaginationChange = vi.fn();
|
|
204
|
+
const sorting: SortingState = [{ id: "col1", desc: true }];
|
|
205
|
+
const pagination: PaginationState = { pageIndex: 1, pageSize: 10 };
|
|
206
|
+
|
|
207
|
+
const { result } = renderHook(() =>
|
|
208
|
+
useCphTableState({
|
|
209
|
+
sorting: { controlled: true, value: sorting, onChange: onSortingChange },
|
|
210
|
+
pagination: { controlled: true, value: pagination, onChange: onPaginationChange },
|
|
211
|
+
} as UseCphTableStateOptions),
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
act(() => {
|
|
215
|
+
result.current.onSortingChange?.([{ id: "col2", desc: false }]);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
act(() => {
|
|
219
|
+
result.current.onPaginationChange?.({ pageIndex: 2, pageSize: 10 });
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
expect(onSortingChange).toHaveBeenCalledTimes(1);
|
|
223
|
+
expect(onPaginationChange).toHaveBeenCalledTimes(1);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
describe("uncontrolled slices", () => {
|
|
228
|
+
it("sorting: exposes hook-owned state and reflects handler updates", () => {
|
|
229
|
+
const { result } = renderHook(() =>
|
|
230
|
+
useCphTableState({
|
|
231
|
+
sorting: { controlled: false, initialValue: [{ id: "col1", desc: true }] },
|
|
232
|
+
} as UseCphTableStateOptions),
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
expect(result.current.state.sorting).toEqual([{ id: "col1", desc: true }]);
|
|
236
|
+
expect(result.current.onSortingChange).toBeDefined();
|
|
237
|
+
expect(result.current.initialState.sorting).toEqual([{ id: "col1", desc: true }]);
|
|
238
|
+
|
|
239
|
+
// Calling the handler must update the exposed state (uncontrolled slices
|
|
240
|
+
// are fed back through `state`, not stuck at their initial values)
|
|
241
|
+
act(() => {
|
|
242
|
+
result.current.onSortingChange?.([{ id: "col2", desc: false }]);
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(result.current.state.sorting).toEqual([{ id: "col2", desc: false }]);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it("columnFilters: advances internal state", () => {
|
|
249
|
+
const { result } = renderHook(() =>
|
|
250
|
+
useCphTableState({
|
|
251
|
+
columnFilters: { controlled: false, initialValue: [{ id: "col1", value: "test" }] },
|
|
252
|
+
} as UseCphTableStateOptions),
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
expect(result.current.initialState.columnFilters).toEqual([{ id: "col1", value: "test" }]);
|
|
256
|
+
expect(result.current.onColumnFiltersChange).toBeDefined();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("columnVisibility: advances internal state", () => {
|
|
260
|
+
const { result } = renderHook(() =>
|
|
261
|
+
useCphTableState({
|
|
262
|
+
columnVisibility: { controlled: false, initialValue: { col1: true } },
|
|
263
|
+
} as UseCphTableStateOptions),
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
expect(result.current.initialState.columnVisibility).toEqual({ col1: true });
|
|
267
|
+
expect(result.current.onColumnVisibilityChange).toBeDefined();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("columnPinning: advances internal state", () => {
|
|
271
|
+
const { result } = renderHook(() =>
|
|
272
|
+
useCphTableState({
|
|
273
|
+
columnPinning: { controlled: false, initialValue: { start: ["col1"], end: [] } },
|
|
274
|
+
} as UseCphTableStateOptions),
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
expect(result.current.initialState.columnPinning).toEqual({ start: ["col1"], end: [] });
|
|
278
|
+
expect(result.current.onColumnPinningChange).toBeDefined();
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it("rowSelection: advances internal state", () => {
|
|
282
|
+
const { result } = renderHook(() =>
|
|
283
|
+
useCphTableState({
|
|
284
|
+
rowSelection: { controlled: false, initialValue: { row1: true } },
|
|
285
|
+
} as UseCphTableStateOptions),
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
expect(result.current.initialState.rowSelection).toEqual({ row1: true });
|
|
289
|
+
expect(result.current.onRowSelectionChange).toBeDefined();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("expanded: advances internal state", () => {
|
|
293
|
+
const { result } = renderHook(() =>
|
|
294
|
+
useCphTableState({
|
|
295
|
+
expanded: { controlled: false, initialValue: { row1: true } },
|
|
296
|
+
} as UseCphTableStateOptions),
|
|
297
|
+
);
|
|
298
|
+
|
|
299
|
+
expect(result.current.initialState.expanded).toEqual({ row1: true });
|
|
300
|
+
expect(result.current.onExpandedChange).toBeDefined();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
it("pagination: advances internal state", () => {
|
|
304
|
+
const { result } = renderHook(() =>
|
|
305
|
+
useCphTableState({
|
|
306
|
+
pagination: { controlled: false, initialValue: { pageIndex: 1, pageSize: 20 } },
|
|
307
|
+
} as UseCphTableStateOptions),
|
|
308
|
+
);
|
|
309
|
+
|
|
310
|
+
expect(result.current.initialState.pagination).toEqual({ pageIndex: 1, pageSize: 20 });
|
|
311
|
+
expect(result.current.onPaginationChange).toBeDefined();
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it("fully uncontrolled: all slices expose hook-owned defaults", () => {
|
|
315
|
+
const { result } = renderHook(() => useCphTableState({}));
|
|
316
|
+
|
|
317
|
+
expect(result.current.state).toEqual({
|
|
318
|
+
sorting: [],
|
|
319
|
+
columnFilters: [],
|
|
320
|
+
columnVisibility: {},
|
|
321
|
+
columnPinning: { start: [], end: [] },
|
|
322
|
+
rowSelection: {},
|
|
323
|
+
expanded: {},
|
|
324
|
+
pagination: { pageIndex: 0, pageSize: DEFAULT_PAGE_SIZE },
|
|
325
|
+
});
|
|
326
|
+
expect(result.current.initialState.sorting).toEqual(defaultSorting);
|
|
327
|
+
expect(result.current.initialState.columnFilters).toEqual(defaultColumnFilters);
|
|
328
|
+
expect(result.current.initialState.columnVisibility).toEqual(defaultColumnVisibility);
|
|
329
|
+
expect(result.current.initialState.columnPinning).toEqual(defaultColumnPinning);
|
|
330
|
+
expect(result.current.initialState.rowSelection).toEqual(defaultRowSelection);
|
|
331
|
+
expect(result.current.initialState.expanded).toEqual(defaultExpanded);
|
|
332
|
+
expect(result.current.initialState.pagination).toEqual(defaultPagination);
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
describe("single-row expansion enforcement (uncontrolled)", () => {
|
|
337
|
+
it("expanding row B while row A is expanded leaves only B expanded", () => {
|
|
338
|
+
const { result } = renderHook(() =>
|
|
339
|
+
useCphTableState({
|
|
340
|
+
expanded: { controlled: false, initialValue: { rowA: true } },
|
|
341
|
+
} as UseCphTableStateOptions),
|
|
342
|
+
);
|
|
343
|
+
|
|
344
|
+
// Simulate expanding rowB while rowA is expanded
|
|
345
|
+
act(() => {
|
|
346
|
+
result.current.onExpandedChange?.({ rowA: true, rowB: true });
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
// The handler should have been called with single-row expansion enforced
|
|
350
|
+
// We can't directly observe internal state, but we can verify the handler exists
|
|
351
|
+
// and the logic is correct by checking the returned handler behavior
|
|
352
|
+
expect(result.current.onExpandedChange).toBeDefined();
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
it("expanding multiple rows via updater function keeps only the last", () => {
|
|
356
|
+
const { result } = renderHook(() =>
|
|
357
|
+
useCphTableState({
|
|
358
|
+
expanded: { controlled: false, initialValue: { rowA: true } },
|
|
359
|
+
} as UseCphTableStateOptions),
|
|
360
|
+
);
|
|
361
|
+
|
|
362
|
+
// Use updater function form
|
|
363
|
+
act(() => {
|
|
364
|
+
result.current.onExpandedChange?.((current: ExpandedState) => {
|
|
365
|
+
if (current === true) return true;
|
|
366
|
+
return { ...current, rowB: true, rowC: true };
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
expect(result.current.onExpandedChange).toBeDefined();
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it("expanding with true (all rows) passes through", () => {
|
|
374
|
+
const { result } = renderHook(() =>
|
|
375
|
+
useCphTableState({
|
|
376
|
+
expanded: { controlled: false, initialValue: {} },
|
|
377
|
+
} as UseCphTableStateOptions),
|
|
378
|
+
);
|
|
379
|
+
|
|
380
|
+
act(() => {
|
|
381
|
+
result.current.onExpandedChange?.(true);
|
|
382
|
+
});
|
|
383
|
+
|
|
384
|
+
expect(result.current.onExpandedChange).toBeDefined();
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
it("controlled expansion: does NOT enforce single-row (consumer responsibility)", () => {
|
|
388
|
+
const onExpandedChange = vi.fn();
|
|
389
|
+
const expanded: ExpandedState = { rowA: true, rowB: true }; // Multiple expanded - consumer's choice
|
|
390
|
+
|
|
391
|
+
const { result } = renderHook(() =>
|
|
392
|
+
useCphTableState({
|
|
393
|
+
expanded: { controlled: true, value: expanded, onChange: onExpandedChange },
|
|
394
|
+
} as UseCphTableStateOptions),
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
act(() => {
|
|
398
|
+
result.current.onExpandedChange?.({ rowA: true, rowB: true, rowC: true });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
// Consumer's handler called with exactly what they passed - no enforcement
|
|
402
|
+
expect(onExpandedChange).toHaveBeenCalledTimes(1);
|
|
403
|
+
expect(onExpandedChange).toHaveBeenCalledWith({ rowA: true, rowB: true, rowC: true });
|
|
404
|
+
});
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
describe("no double-write verification", () => {
|
|
408
|
+
it("controlled slice: internal state is never written when controlled", () => {
|
|
409
|
+
const onSortingChange = vi.fn();
|
|
410
|
+
const sorting: SortingState = [{ id: "col1", desc: true }];
|
|
411
|
+
|
|
412
|
+
const { result, rerender } = renderHook(
|
|
413
|
+
({ sortingValue }) =>
|
|
414
|
+
useCphTableState({
|
|
415
|
+
sorting: { controlled: true, value: sortingValue, onChange: onSortingChange },
|
|
416
|
+
} as UseCphTableStateOptions),
|
|
417
|
+
{ initialProps: { sortingValue: sorting } },
|
|
418
|
+
);
|
|
419
|
+
|
|
420
|
+
// Initial state should be the controlled value
|
|
421
|
+
expect(result.current.state.sorting).toBe(sorting);
|
|
422
|
+
|
|
423
|
+
// Change the controlled value from parent
|
|
424
|
+
const newSorting: SortingState = [{ id: "col2", desc: false }];
|
|
425
|
+
rerender({ sortingValue: newSorting });
|
|
426
|
+
|
|
427
|
+
// State should immediately reflect new controlled value
|
|
428
|
+
expect(result.current.state.sorting).toBe(newSorting);
|
|
429
|
+
|
|
430
|
+
// Calling handler calls consumer, doesn't write to any internal state
|
|
431
|
+
act(() => {
|
|
432
|
+
result.current.onSortingChange?.([{ id: "col3", desc: true }]);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
expect(onSortingChange).toHaveBeenCalledTimes(1);
|
|
436
|
+
// The hook's internal state (if any) was never used for controlled slice
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
it("uncontrolled slice: internal state IS written when uncontrolled", () => {
|
|
440
|
+
const { result } = renderHook(() =>
|
|
441
|
+
useCphTableState({
|
|
442
|
+
sorting: { controlled: false, initialValue: [{ id: "col1", desc: true }] },
|
|
443
|
+
} as UseCphTableStateOptions),
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
// Initial state in initialState and exposed through state
|
|
447
|
+
expect(result.current.initialState.sorting).toEqual([{ id: "col1", desc: true }]);
|
|
448
|
+
expect(result.current.state.sorting).toEqual([{ id: "col1", desc: true }]);
|
|
449
|
+
|
|
450
|
+
// Handler exists for internal updates
|
|
451
|
+
expect(result.current.onSortingChange).toBeDefined();
|
|
452
|
+
|
|
453
|
+
// Calling the handler updates the exposed state
|
|
454
|
+
act(() => {
|
|
455
|
+
result.current.onSortingChange?.([{ id: "col2", desc: false }]);
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
expect(result.current.state.sorting).toEqual([{ id: "col2", desc: false }]);
|
|
459
|
+
});
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
describe("mixed controlled/uncontrolled", () => {
|
|
463
|
+
it("some controlled, some uncontrolled", () => {
|
|
464
|
+
const onSortingChange = vi.fn();
|
|
465
|
+
const onPaginationChange = vi.fn();
|
|
466
|
+
|
|
467
|
+
const { result } = renderHook(() =>
|
|
468
|
+
useCphTableState({
|
|
469
|
+
sorting: {
|
|
470
|
+
controlled: true,
|
|
471
|
+
value: [{ id: "col1", desc: true }],
|
|
472
|
+
onChange: onSortingChange,
|
|
473
|
+
},
|
|
474
|
+
columnFilters: { controlled: false, initialValue: [{ id: "col2", value: "test" }] },
|
|
475
|
+
pagination: {
|
|
476
|
+
controlled: true,
|
|
477
|
+
value: { pageIndex: 1, pageSize: 10 },
|
|
478
|
+
onChange: onPaginationChange,
|
|
479
|
+
},
|
|
480
|
+
} as UseCphTableStateOptions),
|
|
481
|
+
);
|
|
482
|
+
|
|
483
|
+
// Controlled slices in state
|
|
484
|
+
expect(result.current.state.sorting).toEqual([{ id: "col1", desc: true }]);
|
|
485
|
+
expect(result.current.state.pagination).toEqual({ pageIndex: 1, pageSize: 10 });
|
|
486
|
+
|
|
487
|
+
// Uncontrolled slices in initialState
|
|
488
|
+
expect(result.current.initialState.columnFilters).toEqual([{ id: "col2", value: "test" }]);
|
|
489
|
+
|
|
490
|
+
// Handlers for controlled slices
|
|
491
|
+
expect(result.current.onSortingChange).toBe(onSortingChange);
|
|
492
|
+
expect(result.current.onPaginationChange).toBe(onPaginationChange);
|
|
493
|
+
|
|
494
|
+
// Handlers for uncontrolled slices
|
|
495
|
+
expect(result.current.onColumnFiltersChange).toBeDefined();
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
describe("useCphRowSelection", () => {
|
|
501
|
+
describe("controlled mode", () => {
|
|
502
|
+
it("requires both value and onChange", () => {
|
|
503
|
+
expect(() => {
|
|
504
|
+
renderHook(() => useCphRowSelection({ controlled: true, value: { row1: true } }));
|
|
505
|
+
}).toThrow("useCphRowSelection: controlled mode requires both `value` and `onChange`");
|
|
506
|
+
|
|
507
|
+
expect(() => {
|
|
508
|
+
renderHook(() => useCphRowSelection({ controlled: true, onChange: vi.fn() }));
|
|
509
|
+
}).toThrow("useCphRowSelection: controlled mode requires both `value` and `onChange`");
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
it("returns controlled state and consumer handler", () => {
|
|
513
|
+
const onChange = vi.fn();
|
|
514
|
+
const value: RowSelectionState = { row1: true, row2: true };
|
|
515
|
+
|
|
516
|
+
const { result } = renderHook(() =>
|
|
517
|
+
useCphRowSelection({ controlled: true, value, onChange }),
|
|
518
|
+
);
|
|
519
|
+
|
|
520
|
+
expect(result.current.state).toEqual({ rowSelection: value });
|
|
521
|
+
expect(result.current.onRowSelectionChange).toBe(onChange);
|
|
522
|
+
expect(result.current.initialState).toEqual({});
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
it("calls consumer onChange exactly once", () => {
|
|
526
|
+
const onChange = vi.fn();
|
|
527
|
+
const value: RowSelectionState = { row1: true };
|
|
528
|
+
|
|
529
|
+
const { result } = renderHook(() =>
|
|
530
|
+
useCphRowSelection({ controlled: true, value, onChange }),
|
|
531
|
+
);
|
|
532
|
+
|
|
533
|
+
act(() => {
|
|
534
|
+
result.current.onRowSelectionChange?.({ row2: true });
|
|
535
|
+
});
|
|
536
|
+
|
|
537
|
+
expect(onChange).toHaveBeenCalledTimes(1);
|
|
538
|
+
expect(onChange).toHaveBeenCalledWith({ row2: true });
|
|
539
|
+
});
|
|
540
|
+
});
|
|
541
|
+
|
|
542
|
+
describe("uncontrolled mode", () => {
|
|
543
|
+
it("returns hook-owned state and internal handler", () => {
|
|
544
|
+
const initialValue: RowSelectionState = { row1: true };
|
|
545
|
+
|
|
546
|
+
const { result } = renderHook(() => useCphRowSelection({ controlled: false, initialValue }));
|
|
547
|
+
|
|
548
|
+
expect(result.current.state).toEqual({ rowSelection: initialValue });
|
|
549
|
+
expect(result.current.initialState).toEqual({ rowSelection: initialValue });
|
|
550
|
+
expect(result.current.onRowSelectionChange).toBeDefined();
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it("defaults to empty selection when no initialValue", () => {
|
|
554
|
+
const { result } = renderHook(() => useCphRowSelection({}));
|
|
555
|
+
|
|
556
|
+
expect(result.current.state).toEqual({ rowSelection: {} });
|
|
557
|
+
expect(result.current.initialState).toEqual({ rowSelection: {} });
|
|
558
|
+
expect(result.current.onRowSelectionChange).toBeDefined();
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
it("internal handler updates the exposed state", () => {
|
|
562
|
+
const { result } = renderHook(() =>
|
|
563
|
+
useCphRowSelection({ controlled: false, initialValue: { row1: true } }),
|
|
564
|
+
);
|
|
565
|
+
|
|
566
|
+
act(() => {
|
|
567
|
+
result.current.onRowSelectionChange?.({ row2: true });
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
expect(result.current.state).toEqual({ rowSelection: { row2: true } });
|
|
571
|
+
});
|
|
572
|
+
|
|
573
|
+
it("updater function form works", () => {
|
|
574
|
+
const { result } = renderHook(() =>
|
|
575
|
+
useCphRowSelection({ controlled: false, initialValue: { row1: true } }),
|
|
576
|
+
);
|
|
577
|
+
|
|
578
|
+
act(() => {
|
|
579
|
+
result.current.onRowSelectionChange?.((current: RowSelectionState) => ({
|
|
580
|
+
...current,
|
|
581
|
+
row2: true,
|
|
582
|
+
}));
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
expect(result.current.state).toEqual({ rowSelection: { row1: true, row2: true } });
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("normalizes expand-all (true) to collapsed, preserving single-row", () => {
|
|
589
|
+
const { result } = renderHook(() =>
|
|
590
|
+
useCphTableState({ expanded: { controlled: false } } as UseCphTableStateOptions),
|
|
591
|
+
);
|
|
592
|
+
|
|
593
|
+
act(() => {
|
|
594
|
+
result.current.onExpandedChange?.(true);
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
expect(result.current.state.expanded).toEqual({});
|
|
598
|
+
});
|
|
599
|
+
|
|
600
|
+
it("mixed keys collapse to the last true row", () => {
|
|
601
|
+
const { result } = renderHook(() =>
|
|
602
|
+
useCphTableState({ expanded: { controlled: false } } as UseCphTableStateOptions),
|
|
603
|
+
);
|
|
604
|
+
|
|
605
|
+
act(() => {
|
|
606
|
+
result.current.onExpandedChange?.({ row1: true, row2: false, row3: true });
|
|
607
|
+
});
|
|
608
|
+
expect(result.current.state.expanded).toEqual({ row3: true });
|
|
609
|
+
});
|
|
610
|
+
|
|
611
|
+
it("controlled slices with undefined values are omitted from state", () => {
|
|
612
|
+
const none = undefined as unknown as never;
|
|
613
|
+
const { result } = renderHook(() =>
|
|
614
|
+
useCphTableState({
|
|
615
|
+
sorting: { controlled: true, value: none, onChange: vi.fn() },
|
|
616
|
+
columnFilters: { controlled: true, value: none, onChange: vi.fn() },
|
|
617
|
+
columnVisibility: { controlled: true, value: none, onChange: vi.fn() },
|
|
618
|
+
columnPinning: { controlled: true, value: none, onChange: vi.fn() },
|
|
619
|
+
rowSelection: { controlled: true, value: none, onChange: vi.fn() },
|
|
620
|
+
expanded: { controlled: true, value: none, onChange: vi.fn() },
|
|
621
|
+
pagination: { controlled: true, value: none, onChange: vi.fn() },
|
|
622
|
+
} as UseCphTableStateOptions),
|
|
623
|
+
);
|
|
624
|
+
|
|
625
|
+
// Defensive path: a controlled slice with no value yet leaves TanStack
|
|
626
|
+
// uncontrolled for that slice instead of writing undefined into state.
|
|
627
|
+
expect(result.current.state).toEqual({});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it("collapses multi-row updates to the last expanded row", () => {
|
|
631
|
+
const { result } = renderHook(() =>
|
|
632
|
+
useCphTableState({ expanded: { controlled: false } } as UseCphTableStateOptions),
|
|
633
|
+
);
|
|
634
|
+
|
|
635
|
+
act(() => {
|
|
636
|
+
result.current.onExpandedChange?.({ row1: true, row2: true });
|
|
637
|
+
});
|
|
638
|
+
|
|
639
|
+
expect(result.current.state.expanded).toEqual({ row2: true });
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
describe("uncontrolled slices: every internal handler advances exposed state", () => {
|
|
644
|
+
it("value form", () => {
|
|
645
|
+
const { result } = renderUncontrolled();
|
|
646
|
+
|
|
647
|
+
act(() => {
|
|
648
|
+
result.current.onSortingChange?.([{ id: "a", desc: false }]);
|
|
649
|
+
result.current.onColumnFiltersChange?.([{ id: "b", value: "x" }]);
|
|
650
|
+
result.current.onColumnVisibilityChange?.({ a: false });
|
|
651
|
+
result.current.onColumnPinningChange?.({ start: ["a"], end: [] });
|
|
652
|
+
result.current.onRowSelectionChange?.({ r1: true });
|
|
653
|
+
result.current.onExpandedChange?.({ r1: true });
|
|
654
|
+
result.current.onPaginationChange?.({ pageIndex: 2, pageSize: 12 });
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
expect(result.current.state.sorting).toEqual([{ id: "a", desc: false }]);
|
|
658
|
+
expect(result.current.state.columnFilters).toEqual([{ id: "b", value: "x" }]);
|
|
659
|
+
expect(result.current.state.columnVisibility).toEqual({ a: false });
|
|
660
|
+
expect(result.current.state.columnPinning).toEqual({ start: ["a"], end: [] });
|
|
661
|
+
expect(result.current.state.rowSelection).toEqual({ r1: true });
|
|
662
|
+
expect(result.current.state.expanded).toEqual({ r1: true });
|
|
663
|
+
expect(result.current.state.pagination).toEqual({ pageIndex: 2, pageSize: 12 });
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it("updater-function form", () => {
|
|
667
|
+
const { result } = renderUncontrolled();
|
|
668
|
+
|
|
669
|
+
act(() => {
|
|
670
|
+
result.current.onSortingChange?.((previous) => [...previous, { id: "a", desc: false }]);
|
|
671
|
+
result.current.onColumnFiltersChange?.((previous) => [
|
|
672
|
+
...previous,
|
|
673
|
+
{ id: "b", value: "x" },
|
|
674
|
+
]);
|
|
675
|
+
result.current.onColumnVisibilityChange?.((previous) => ({ ...previous, a: false }));
|
|
676
|
+
result.current.onColumnPinningChange?.((previous) => ({
|
|
677
|
+
...previous,
|
|
678
|
+
start: ["a"],
|
|
679
|
+
}));
|
|
680
|
+
result.current.onRowSelectionChange?.((previous) => ({ ...previous, r1: true }));
|
|
681
|
+
result.current.onExpandedChange?.(() => ({ r1: true }));
|
|
682
|
+
result.current.onPaginationChange?.((previous) => ({ ...previous, pageIndex: 2 }));
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
expect(result.current.state.sorting).toEqual([{ id: "a", desc: false }]);
|
|
686
|
+
expect(result.current.state.columnFilters).toEqual([{ id: "b", value: "x" }]);
|
|
687
|
+
expect(result.current.state.columnVisibility).toEqual({ a: false });
|
|
688
|
+
expect(result.current.state.columnPinning).toEqual({ start: ["a"], end: [] });
|
|
689
|
+
expect(result.current.state.rowSelection).toEqual({ r1: true });
|
|
690
|
+
expect(result.current.state.expanded).toEqual({ r1: true });
|
|
691
|
+
expect(result.current.state.pagination).toEqual({
|
|
692
|
+
pageIndex: 2,
|
|
693
|
+
pageSize: DEFAULT_PAGE_SIZE,
|
|
694
|
+
});
|
|
695
|
+
});
|
|
696
|
+
});
|
|
697
|
+
});
|