-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathTreeDataGrid.test.tsx
More file actions
452 lines (393 loc) · 14.9 KB
/
TreeDataGrid.test.tsx
File metadata and controls
452 lines (393 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import { useState } from 'react';
import { page, userEvent } from 'vitest/browser';
import type { Column } from '../../src';
import { SelectColumn, textEditor, TreeDataGrid } from '../../src';
import { focusSinkClassname } from '../../src/style/core';
import { rowSelected } from '../../src/style/row';
import {
getCell,
getCellsAtRowIndex,
getRowByCell,
getRowByCellName,
getRows,
getSelectAllCheckbox,
getSelectedCell,
getTreeGrid,
testCount,
testRowCount
} from './utils';
const rowSelectedClassname = 'rdg-row-selected';
interface Row {
id: number;
country: string;
year: number;
}
type SummaryRow = undefined;
const topSummaryRows: readonly SummaryRow[] = [undefined];
const bottomSummaryRows: readonly SummaryRow[] = [undefined];
const columns: readonly Column<Row, SummaryRow>[] = [
SelectColumn,
{
key: 'sport',
name: 'Sport'
},
{
key: 'country',
name: 'Country',
renderEditCell: textEditor
},
{
key: 'year',
name: 'Year'
},
{
key: 'id',
name: 'Id',
renderCell(props) {
function onClick() {
props.onRowChange({ ...props.row, id: props.row.id + 10 });
}
return (
<button type="button" onClick={onClick}>
value: {props.row.id}
</button>
);
},
renderGroupCell({ childRows }) {
return Math.min(...childRows.map((c) => c.id));
}
}
];
const initialRows: readonly Row[] = [
{
id: 1,
country: 'USA',
year: 2020
},
{
id: 2,
country: 'USA',
year: 2021
},
{
id: 3,
country: 'Canada',
year: 2021
},
{
id: 4,
country: 'Canada',
year: 2022
}
];
const onCellCopySpy = vi.fn();
const onCellPasteSpy = vi.fn(({ row }: { row: Row }) => row);
function rowKeyGetter(row: Row) {
return row.id;
}
function TestGrid({
groupBy,
groupIdGetter
}: {
groupBy: string[];
groupIdGetter: ((groupKey: string, parentId?: string) => string) | undefined;
}) {
const [rows, setRows] = useState(initialRows);
const [selectedRows, setSelectedRows] = useState((): ReadonlySet<number> => new Set());
const [expandedGroupIds, setExpandedGroupIds] = useState(
(): ReadonlySet<unknown> => new Set<unknown>([])
);
return (
<TreeDataGrid
columns={columns}
rows={rows}
topSummaryRows={topSummaryRows}
bottomSummaryRows={bottomSummaryRows}
rowKeyGetter={rowKeyGetter}
groupBy={groupBy}
rowGrouper={rowGrouper}
selectedRows={selectedRows}
onSelectedRowsChange={setSelectedRows}
expandedGroupIds={expandedGroupIds}
onExpandedGroupIdsChange={setExpandedGroupIds}
onRowsChange={setRows}
onCellCopy={onCellCopySpy}
onCellPaste={onCellPasteSpy}
groupIdGetter={groupIdGetter}
/>
);
}
function rowGrouper(rows: readonly Row[], columnKey: string) {
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return Object.groupBy(rows, (r) => r[columnKey]) as Record<string, readonly R[]>;
}
function setup(groupBy: string[], groupIdGetter?: (groupKey: string, parentId?: string) => string) {
return page.render(<TestGrid groupBy={groupBy} groupIdGetter={groupIdGetter} />);
}
async function testHeaderCellsContent(expected: readonly string[]) {
const headerCells = page.getByRole('columnheader');
await testCount(headerCells, expected.length);
const content = headerCells.elements().map((cell) => cell.textContent);
expect(content).toStrictEqual(expected);
}
test('should not group if groupBy is empty', async () => {
await setup([]);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '7');
await testHeaderCellsContent(['', 'Sport', 'Country', 'Year', 'Id']);
await testRowCount(7);
});
test('should not group if column does not exist', async () => {
await setup(['abc']);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '7');
await testRowCount(7);
});
test('should group by single column', async () => {
await setup(['country']);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '9');
await testHeaderCellsContent(['', 'Country', 'Sport', 'Year', 'Id']);
await testRowCount(5);
});
test('should group by multiple columns', async () => {
await setup(['country', 'year']);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '13');
await testHeaderCellsContent(['', 'Country', 'Year', 'Sport', 'Id']);
await testRowCount(5);
});
test('should use groupIdGetter when provided', async () => {
const groupIdGetter = vi.fn((groupKey: string, parentId?: string) =>
parentId !== undefined ? `${groupKey}#${parentId}` : groupKey
);
await setup(['country', 'year'], groupIdGetter);
expect(groupIdGetter).toHaveBeenCalled();
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '13');
await testHeaderCellsContent(['', 'Country', 'Year', 'Sport', 'Id']);
await testRowCount(5);
groupIdGetter.mockClear();
await userEvent.click(getCell('USA'));
await testRowCount(7);
expect(groupIdGetter).toHaveBeenCalled();
await userEvent.click(getCell('Canada'));
await testRowCount(9);
await userEvent.click(getCell('2020'));
await testRowCount(10);
});
test('should ignore duplicate groupBy columns', async () => {
await setup(['year', 'year', 'year']);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '10');
await testRowCount(6);
});
test('should use groupBy order while grouping', async () => {
await setup(['year', 'country']);
await expect.element(getTreeGrid()).toHaveAttribute('aria-rowcount', '14');
await testHeaderCellsContent(['', 'Year', 'Country', 'Sport', 'Id']);
await testRowCount(6);
});
test('should toggle group when group cell is clicked', async () => {
await setup(['year']);
await testRowCount(6);
const groupCell = getCell('2021');
await userEvent.click(groupCell);
await testRowCount(8);
await userEvent.click(groupCell);
await testRowCount(6);
});
test('should toggle group using keyboard', async () => {
await setup(['year']);
await testRowCount(6);
const groupCell = getCell('2021');
await userEvent.click(groupCell);
await testRowCount(8);
// clicking on the group cell selects the row
await expect.element(getSelectedCell()).not.toBeInTheDocument();
await expect.element(getRowByCellName('2021')).toHaveClass(rowSelectedClassname);
await userEvent.keyboard('{arrowright}{arrowright}{enter}');
await testRowCount(6);
await userEvent.keyboard('{enter}');
await testRowCount(8);
});
test('should set aria-attributes', async () => {
await setup(['year', 'country']);
const groupRow1 = getRowByCellName('2020');
await expect.element(groupRow1).toHaveAttribute('aria-level', '1');
await expect.element(groupRow1).toHaveAttribute('aria-setsize', '3');
await expect.element(groupRow1).toHaveAttribute('aria-posinset', '1');
await expect.element(groupRow1).toHaveAttribute('aria-rowindex', '3');
await expect.element(groupRow1).toHaveAttribute('aria-expanded', 'false');
const groupCell2 = getCell('2021');
const groupRow2 = getRowByCell(groupCell2);
await expect.element(groupRow2).toHaveAttribute('aria-level', '1');
await expect.element(groupRow2).toHaveAttribute('aria-setsize', '3');
await expect.element(groupRow2).toHaveAttribute('aria-posinset', '2');
await expect.element(groupRow2).toHaveAttribute('aria-rowindex', '6');
await expect.element(groupRow1).toHaveAttribute('aria-expanded', 'false');
await userEvent.click(groupCell2);
await expect.element(groupRow2).toHaveAttribute('aria-expanded', 'true');
const groupCell3 = getCell('Canada');
const groupRow3 = getRowByCell(groupCell3);
await expect.element(groupRow3).toHaveAttribute('aria-level', '2');
await expect.element(groupRow3).toHaveAttribute('aria-setsize', '2');
await expect.element(groupRow3).toHaveAttribute('aria-posinset', '2');
await expect.element(groupRow3).toHaveAttribute('aria-rowindex', '9');
await expect.element(groupRow1).toHaveAttribute('aria-expanded', 'false');
await userEvent.click(groupCell3);
await expect.element(groupRow3).toHaveAttribute('aria-expanded', 'true');
});
test('should select rows in a group', async () => {
await setup(['year', 'country']);
const headerCheckbox = getSelectAllCheckbox();
await expect.element(headerCheckbox).not.toBeChecked();
// expand group
const groupCell1 = getCell('2021');
await userEvent.click(groupCell1);
const groupCell2 = getCell('Canada');
await userEvent.click(groupCell2);
const selectedRows = page.getByRole('row', { selected: true });
await testCount(selectedRows, 0);
// select parent row
await userEvent.click(getRowByCell(groupCell1).getByRole('checkbox', { name: 'Select Group' }));
await testCount(selectedRows, 4);
await expect.element(selectedRows.nth(0)).toHaveAttribute('aria-rowindex', '6');
await expect.element(selectedRows.nth(1)).toHaveAttribute('aria-rowindex', '7');
await expect.element(selectedRows.nth(2)).toHaveAttribute('aria-rowindex', '9');
await expect.element(selectedRows.nth(3)).toHaveAttribute('aria-rowindex', '10');
// unselecting child should unselect the parent row
await userEvent.click(selectedRows.nth(3).getByRole('checkbox', { name: 'Select' }));
await testCount(selectedRows, 1);
await expect.element(selectedRows.nth(0)).toHaveAttribute('aria-rowindex', '7');
// select child group
const checkbox = getRowByCell(groupCell2).getByRole('checkbox', {
name: 'Select Group'
});
await userEvent.click(checkbox);
await testCount(selectedRows, 4);
// unselect child group
await userEvent.click(checkbox);
await testCount(selectedRows, 1);
await userEvent.click(getCell('2020'));
await userEvent.click(getCell('2022'));
await userEvent.click(headerCheckbox);
await testCount(selectedRows, 0);
await userEvent.click(headerCheckbox);
await testCount(selectedRows, 8);
await userEvent.click(headerCheckbox);
await testCount(selectedRows, 0);
});
test('cell navigation in a treegrid', async () => {
await setup(['country', 'year']);
await testRowCount(5);
const focusSink = page.getBySelector(`.${focusSinkClassname}`);
// expand group
const groupCell1 = getCell('USA');
expect(document.body).toHaveFocus();
await expect.element(focusSink).toHaveAttribute('tabIndex', '-1');
await userEvent.click(groupCell1);
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveAttribute('tabIndex', '0');
await expect.element(focusSink).toHaveStyle('grid-row-start:3');
await expect.element(focusSink).toHaveClass(rowSelected);
await userEvent.keyboard('{arrowup}');
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveStyle('grid-row-start:2');
await expect.element(focusSink).toHaveClass(rowSelected);
await userEvent.keyboard('{arrowup}');
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveStyle('grid-row-start:1');
await expect.element(focusSink).toHaveClass(rowSelected);
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveStyle('grid-row-start:1');
await expect.element(focusSink).toHaveClass(rowSelected);
await userEvent.keyboard('{arrowdown}');
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveStyle('grid-row-start:2');
await expect.element(focusSink).toHaveClass(rowSelected);
const groupCell2 = getCell('2021');
await userEvent.click(groupCell2);
await expect.element(focusSink).toHaveFocus();
await expect.element(focusSink).toHaveAttribute('tabIndex', '0');
// select cell
await userEvent.click(getCellsAtRowIndex(5)[1]);
expect(getCellsAtRowIndex(5)[1]).toHaveAttribute('aria-selected', 'true');
await expect.element(focusSink).toHaveAttribute('tabIndex', '-1');
// select the previous cell
await userEvent.keyboard('{arrowleft}');
expect(getCellsAtRowIndex(5)[1]).toHaveAttribute('aria-selected', 'false');
expect(getCellsAtRowIndex(5)[0]).toHaveAttribute('aria-selected', 'true');
// if the first cell is selected then arrowleft should select the row
await userEvent.keyboard('{arrowleft}');
expect(getCellsAtRowIndex(5)[0]).toHaveAttribute('aria-selected', 'false');
await expect.element(getRows()[4]).toHaveClass(rowSelectedClassname);
await expect.element(focusSink).toHaveFocus();
// if the row is selected then arrowright should select the first cell on the same row
await userEvent.keyboard('{arrowright}');
expect(getCellsAtRowIndex(5)[0]).toHaveAttribute('aria-selected', 'true');
await userEvent.keyboard('{arrowleft}{arrowup}');
await testRowCount(8);
// left arrow should collapse the group
await userEvent.keyboard('{arrowleft}');
await testRowCount(7);
// right arrow should expand the group
await userEvent.keyboard('{arrowright}');
await testRowCount(8);
// left arrow on a collapsed group should select the parent group
await expect.element(getRows()[1]).not.toHaveClass(rowSelectedClassname);
await userEvent.keyboard('{arrowleft}{arrowleft}');
await expect.element(getRows()[1]).toHaveClass(rowSelectedClassname);
await userEvent.keyboard('{end}');
await expect.element(getRows()[5]).toHaveClass(rowSelectedClassname);
await userEvent.keyboard('{home}');
await expect.element(page.getByRole('row').all()[0]).toHaveClass(rowSelectedClassname);
// collpase parent group
await userEvent.keyboard('{arrowdown}{arrowdown}{arrowleft}');
await expect.element(getCell('2021')).not.toBeInTheDocument();
await testRowCount(5);
});
test('copy/paste when grouping is enabled', async () => {
await setup(['year']);
await userEvent.click(getCell('2021'));
await userEvent.copy();
expect(onCellCopySpy).not.toHaveBeenCalled();
await userEvent.paste();
expect(onCellPasteSpy).not.toHaveBeenCalled();
await userEvent.click(getCell('USA'));
await userEvent.copy();
expect(onCellCopySpy).toHaveBeenCalledExactlyOnceWith(
{
column: expect.objectContaining(columns[2]),
row: {
country: 'USA',
id: 2,
year: 2021
}
},
expect.anything()
);
await userEvent.paste();
expect(onCellPasteSpy).toHaveBeenCalledExactlyOnceWith(
{
column: expect.objectContaining(columns[2]),
row: {
country: 'USA',
id: 2,
year: 2021
}
},
expect.anything()
);
});
test('update row using cell renderer', async () => {
await setup(['year']);
await userEvent.click(getCell('2021'));
await userEvent.click(getCell('USA'));
await userEvent.keyboard('{arrowright}{arrowright}');
await expect.element(getSelectedCell()).toHaveTextContent('value: 2');
await userEvent.click(page.getByRole('button', { name: 'value: 2' }));
await expect.element(getSelectedCell()).toHaveTextContent('value: 12');
});
test('custom renderGroupCell', async () => {
await setup(['country']);
await expect.element(getRowByCellName('USA').getByRole('gridcell').nth(4)).toHaveTextContent('1');
await expect
.element(getRowByCellName('Canada').getByRole('gridcell').nth(4))
.toHaveTextContent('3');
});