Skip to content

Commit bd32184

Browse files
authored
Minor test improvements (#3991)
1 parent ba8fb29 commit bd32184

11 files changed

Lines changed: 99 additions & 112 deletions

test/browser/column/grouping.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import type { ColumnOrColumnGroup } from '../../../src';
4-
import { safeTab, setup, tabIntoGrid, testCount, validateCellPosition } from '../utils';
4+
import { safeTab, setup, testCount, validateCellPosition } from '../utils';
55

66
const grid = page.getGrid();
77
const headerRows = grid.getHeaderRow();
@@ -252,12 +252,12 @@ test('grouping', async () => {
252252
});
253253

254254
test('keyboard navigation', async () => {
255-
await setup({ columns, rows: [{}] }, true);
255+
await setup({ columns, rows: [{}] });
256256

257257
// no initial active position
258258
await expect.element(grid.getActiveCell()).not.toBeInTheDocument();
259259

260-
await tabIntoGrid();
260+
await safeTab();
261261
await validateCellPosition(0, 3);
262262

263263
// arrow navigation

test/browser/direction.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import type { Column } from '../../src';
4-
import { setup, tabIntoGrid } from './utils';
4+
import { safeTab, setup } from './utils';
55

66
const grid = page.getGrid();
77
const activeCell = grid.getActiveCell();
@@ -25,27 +25,27 @@ const columns: readonly Column<Row>[] = [
2525
const rows: readonly Row[] = [];
2626

2727
test('should use left to right direction by default', async () => {
28-
await setup({ rows, columns }, true);
28+
await setup({ rows, columns });
2929
await expect.element(grid).toHaveAttribute('dir', 'ltr');
30-
await tabIntoGrid();
30+
await safeTab();
3131
await expect.element(activeCell).toHaveTextContent('ID');
3232
await userEvent.keyboard('{ArrowRight}');
3333
await expect.element(activeCell).toHaveTextContent('Name');
3434
});
3535

3636
test('should use left to right direction if direction prop is set to ltr', async () => {
37-
await setup({ rows, columns, direction: 'ltr' }, true);
37+
await setup({ rows, columns, direction: 'ltr' });
3838
await expect.element(grid).toHaveAttribute('dir', 'ltr');
39-
await tabIntoGrid();
39+
await safeTab();
4040
await expect.element(activeCell).toHaveTextContent('ID');
4141
await userEvent.keyboard('{ArrowRight}');
4242
await expect.element(activeCell).toHaveTextContent('Name');
4343
});
4444

4545
test('should use right to left direction if direction prop is set to rtl', async () => {
46-
await setup({ rows, columns, direction: 'rtl' }, true);
46+
await setup({ rows, columns, direction: 'rtl' });
4747
await expect.element(grid).toHaveAttribute('dir', 'rtl');
48-
await tabIntoGrid();
48+
await safeTab();
4949
await expect.element(activeCell).toHaveTextContent('ID');
5050
await userEvent.keyboard('{ArrowLeft}');
5151
await expect.element(activeCell).toHaveTextContent('Name');

test/browser/dragFill.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,14 @@ test('should allow drag up using mouse', async () => {
8888

8989
test('should focus the cell when drag handle is clicked', async () => {
9090
await setup();
91-
await userEvent.click(getCellsAtRowIndex(0).nth(0));
92-
await userEvent.click(document.body);
93-
await expect.element(document.body).toHaveFocus();
91+
const cell = getCellsAtRowIndex(0).nth(0);
92+
93+
await userEvent.click(cell);
94+
await expect.element(cell).toHaveFocus();
95+
96+
cell.element().blur();
97+
await expect.element(cell).not.toHaveFocus();
98+
9499
await userEvent.click(dragHandle);
95-
await expect.element(getCellsAtRowIndex(0).nth(0)).toHaveFocus();
100+
await expect.element(cell).toHaveFocus();
96101
});

test/browser/events.test.tsx

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import { DataGrid } from '../../src';
4-
import type { Column, DataGridProps } from '../../src';
4+
import type { Column } from '../../src';
55
import { safeTab } from './utils';
66

77
interface Row {
@@ -55,7 +55,9 @@ const rows: readonly Row[] = [
5555
describe('Events', () => {
5656
it('should not select cell if onCellMouseDown prevents grid default', async () => {
5757
await page.render(
58-
<EventTest
58+
<DataGrid
59+
columns={columns}
60+
rows={rows}
5961
onCellMouseDown={(args, event) => {
6062
if (args.column.key === 'col1') {
6163
event.preventGridDefault();
@@ -71,7 +73,9 @@ describe('Events', () => {
7173

7274
it('should be able to open editor editor on single click using onCellClick', async () => {
7375
await page.render(
74-
<EventTest
76+
<DataGrid
77+
columns={columns}
78+
rows={rows}
7579
onCellClick={(args, event) => {
7680
if (args.column.key === 'col2') {
7781
event.preventGridDefault();
@@ -88,7 +92,9 @@ describe('Events', () => {
8892

8993
it('should not open editor editor on double click if onCellDoubleClick prevents default', async () => {
9094
await page.render(
91-
<EventTest
95+
<DataGrid
96+
columns={columns}
97+
rows={rows}
9298
onCellDoubleClick={(args, event) => {
9399
if (args.column.key === 'col1') {
94100
event.preventGridDefault();
@@ -104,7 +110,9 @@ describe('Events', () => {
104110

105111
it('should call onCellContextMenu when cell is right clicked', async () => {
106112
const onCellContextMenu = vi.fn();
107-
await page.render(<EventTest onCellContextMenu={onCellContextMenu} />);
113+
await page.render(
114+
<DataGrid columns={columns} rows={rows} onCellContextMenu={onCellContextMenu} />
115+
);
108116
expect(onCellContextMenu).not.toHaveBeenCalled();
109117
await userEvent.click(page.getCell({ name: '1' }), { button: 'right' });
110118
expect(onCellContextMenu).toHaveBeenCalledExactlyOnceWith(
@@ -122,7 +130,9 @@ describe('Events', () => {
122130
it('should call onActivePositionChange when cell selection is changed', async () => {
123131
const onActivePositionChange = vi.fn();
124132

125-
await page.render(<EventTest onActivePositionChange={onActivePositionChange} />);
133+
await page.render(
134+
<DataGrid columns={columns} rows={rows} onActivePositionChange={onActivePositionChange} />
135+
);
126136

127137
expect(onActivePositionChange).not.toHaveBeenCalled();
128138

@@ -181,16 +191,3 @@ describe('Events', () => {
181191
expect(onActivePositionChange).toHaveBeenCalledTimes(6);
182192
});
183193
});
184-
185-
type EventProps = Pick<
186-
DataGridProps<Row>,
187-
| 'onCellMouseDown'
188-
| 'onCellClick'
189-
| 'onCellDoubleClick'
190-
| 'onCellContextMenu'
191-
| 'onActivePositionChange'
192-
>;
193-
194-
function EventTest(props: EventProps) {
195-
return <DataGrid columns={columns} rows={rows} {...props} />;
196-
}

test/browser/keyboardNavigation.test.tsx

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import {
77
safeTab,
88
scrollGrid,
99
setup,
10-
tabIntoGrid,
1110
testCount,
1211
validateCellPosition
1312
} from './utils';
1413

1514
const activeCell = page.getActiveCell();
15+
const activeSelectAllCheckbox = activeCell.getSelectAllCheckbox();
16+
const activeSelectCheckbox = activeCell.getByRole('checkbox', { name: 'Select', exact: true });
1617

1718
type Row = undefined;
1819

@@ -31,13 +32,13 @@ const columns: readonly Column<Row, Row>[] = [
3132
];
3233

3334
test('keyboard navigation', async () => {
34-
await setup({ columns, rows, topSummaryRows, bottomSummaryRows }, true);
35+
await setup({ columns, rows, topSummaryRows, bottomSummaryRows });
3536

3637
// no initial active position
3738
await expect.element(activeCell).not.toBeInTheDocument();
3839

3940
// tab into the grid
40-
await tabIntoGrid();
41+
await safeTab();
4142
await validateCellPosition(0, 0);
4243

4344
// tab to the next cell
@@ -107,10 +108,10 @@ test('keyboard navigation', async () => {
107108
});
108109

109110
test('arrow and tab navigation', async () => {
110-
await setup({ columns, rows, bottomSummaryRows }, true);
111+
await setup({ columns, rows, bottomSummaryRows });
111112

112113
// pressing arrowleft on the leftmost cell does nothing
113-
await tabIntoGrid();
114+
await safeTab();
114115
await userEvent.keyboard('{arrowdown}');
115116
await validateCellPosition(0, 1);
116117
await userEvent.keyboard('{arrowleft}');
@@ -132,7 +133,17 @@ test('arrow and tab navigation', async () => {
132133
});
133134

134135
test('grid enter/exit', async () => {
135-
await setup<Row, Row>({ columns, rows: Array.from({ length: 5 }), bottomSummaryRows }, true);
136+
await page.render(
137+
<>
138+
<button type="button">Before</button>
139+
<DataGrid
140+
columns={columns}
141+
rows={Array.from<Row>({ length: 5 })}
142+
bottomSummaryRows={bottomSummaryRows}
143+
/>
144+
<button type="button">After</button>
145+
</>
146+
);
136147

137148
const beforeButton = page.getByRole('button', { name: 'Before' });
138149
const afterButton = page.getByRole('button', { name: 'After' });
@@ -141,31 +152,37 @@ test('grid enter/exit', async () => {
141152
await expect.element(activeCell).not.toBeInTheDocument();
142153

143154
// tab into the grid
144-
await tabIntoGrid();
155+
await safeTab();
156+
await safeTab();
145157
await validateCellPosition(0, 0);
158+
await expect.element(activeSelectAllCheckbox).toHaveFocus();
146159

147160
// shift+tab tabs out of the grid if we are at the first cell
148161
await safeTab(true);
149162
await expect.element(beforeButton).toHaveFocus();
150163

151164
await safeTab();
152165
await validateCellPosition(0, 0);
166+
await expect.element(activeSelectAllCheckbox).toHaveFocus();
153167

154168
await userEvent.keyboard('{arrowdown}{arrowdown}');
155169
await validateCellPosition(0, 2);
170+
await expect.element(activeSelectCheckbox).toHaveFocus();
156171

157172
// tab should focus the last active cell
158173
// click outside the grid
159174
await userEvent.click(beforeButton);
160175
await safeTab();
161176
await userEvent.keyboard('{arrowdown}');
162177
await validateCellPosition(0, 3);
178+
await expect.element(activeSelectCheckbox).toHaveFocus();
163179

164180
// shift+tab should focus the last active cell
181+
// click outside the grid
165182
await userEvent.click(afterButton);
166183
await safeTab(true);
167184
await validateCellPosition(0, 3);
168-
await expect.element(activeCell.getByRole('checkbox')).toHaveFocus();
185+
await expect.element(activeSelectCheckbox).toHaveFocus();
169186

170187
// tab tabs out of the grid if we are at the last cell
171188
await userEvent.keyboard('{Control>}{end}{/Control}');
@@ -174,16 +191,15 @@ test('grid enter/exit', async () => {
174191
});
175192

176193
test('navigation with focusable cell renderer', async () => {
177-
await setup<Row, Row>({ columns, rows: Array.from({ length: 1 }), bottomSummaryRows }, true);
178-
await tabIntoGrid();
194+
await setup({ columns, rows: Array.from<Row>({ length: 1 }), bottomSummaryRows });
195+
await safeTab();
179196
await userEvent.keyboard('{arrowdown}');
180197
await validateCellPosition(0, 1);
181198

182199
// cell should not set tabIndex to 0 if it contains a focusable cell renderer
183200
await expect.element(activeCell).toHaveAttribute('tabIndex', '-1');
184-
const checkbox = activeCell.getByRole('checkbox');
185-
await expect.element(checkbox).toHaveFocus();
186-
await expect.element(checkbox).toHaveAttribute('tabIndex', '0');
201+
await expect.element(activeSelectCheckbox).toHaveFocus();
202+
await expect.element(activeSelectCheckbox).toHaveAttribute('tabIndex', '0');
187203

188204
await safeTab();
189205
await validateCellPosition(1, 1);
@@ -215,11 +231,8 @@ test('navigation when header and summary rows have focusable elements', async ()
215231
}
216232
];
217233

218-
await setup<Row, number>(
219-
{ columns, rows: Array.from({ length: 2 }), bottomSummaryRows: [1, 2] },
220-
true
221-
);
222-
await tabIntoGrid();
234+
await setup({ columns, rows: Array.from<Row>({ length: 2 }), bottomSummaryRows: [1, 2] });
235+
await safeTab();
223236

224237
// should set focus on the header filter
225238
await expect.element(page.getByTestId('header-filter1')).toHaveFocus();
@@ -259,8 +272,8 @@ test('navigation when active cell not in the viewport', async () => {
259272
for (let i = 0; i < 99; i++) {
260273
columns.push({ key: `col${i}`, name: `col${i}`, frozen: i < 5 });
261274
}
262-
await setup({ columns, rows, bottomSummaryRows }, true);
263-
await tabIntoGrid();
275+
await setup({ columns, rows, bottomSummaryRows });
276+
await safeTab();
264277
await validateCellPosition(0, 0);
265278

266279
await userEvent.keyboard('{Control>}{end}{/Control}{arrowup}{arrowup}');
@@ -330,8 +343,8 @@ test('reset active cell when row is removed', async () => {
330343
});
331344

332345
test('should not change the left and right arrow behavior for right to left languages', async () => {
333-
await setup<Row, Row>({ columns, rows, direction: 'rtl' }, true);
334-
await tabIntoGrid();
346+
await setup<Row, Row>({ columns, rows, direction: 'rtl' });
347+
await safeTab();
335348
await validateCellPosition(0, 0);
336349
await safeTab();
337350
await validateCellPosition(1, 0);

test/browser/rowHeight.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page, userEvent } from 'vitest/browser';
22

33
import type { Column, DataGridProps } from '../../src';
4-
import { setup, tabIntoGrid, testRowCount } from './utils';
4+
import { safeTab, setup, testRowCount } from './utils';
55

66
const grid = page.getGrid();
77

@@ -19,7 +19,7 @@ function setupGrid(rowHeight: DataGridProps<Row>['rowHeight']) {
1919
width: 80
2020
});
2121
}
22-
return setup({ columns, rows, rowHeight }, true);
22+
return setup({ columns, rows, rowHeight });
2323
}
2424

2525
async function expectGridRows(rowHeightFn: (row: number) => number, expected: string) {
@@ -36,7 +36,7 @@ test('rowHeight is number', async () => {
3636
'40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px 40px'
3737
});
3838
await testRowCount(30);
39-
await tabIntoGrid();
39+
await safeTab();
4040
await expect.element(grid).toHaveProperty('scrollTop', 0);
4141
await userEvent.keyboard('{Control>}{end}');
4242
const gridEl = grid.element();
@@ -52,7 +52,7 @@ test('rowHeight is function', async () => {
5252
});
5353
await testRowCount(22);
5454

55-
await tabIntoGrid();
55+
await safeTab();
5656
await expect.element(grid).toHaveProperty('scrollTop', 0);
5757
await userEvent.keyboard('{Control>}{end}');
5858
const gridEl = grid.element();

test/browser/styles.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.rdg {
2+
block-size: 1080px;
3+
scrollbar-width: none;
4+
}

0 commit comments

Comments
 (0)