Skip to content

Commit 19d5487

Browse files
Use vitest-browser-react and browserUserEvent (adazzle#3629)
* Try out vitest-browser-react * skip eslint * Disable * Revert * Use `.fill` * Fix node tests * Use page object * Use vitest browser api * eslint * increase timeout? * increase again * revert timout changes * Fix tests * tweak * increase delay * Address comments, pin eslint to fix build * Using `expect.element` * Update test/browser/column/renderEditCell.test.tsx Co-authored-by: Nicolas Stepien <[email protected]> --------- Co-authored-by: Nicolas Stepien <[email protected]>
1 parent 8685a70 commit 19d5487

9 files changed

+147
-112
lines changed

eslint.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ export default [
707707
'testing-library/prefer-presence-queries': 1,
708708
'testing-library/prefer-query-by-disappearance': 1,
709709
'testing-library/prefer-query-matchers': 0,
710-
'testing-library/prefer-screen-queries': 1,
710+
'testing-library/prefer-screen-queries': 0,
711711
'testing-library/prefer-user-event': 1,
712712
'testing-library/render-result-naming-convention': 0
713713
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
"@wyw-in-js/vite": "^0.5.0",
8888
"babel-plugin-optimize-clsx": "^2.6.2",
8989
"browserslist": "^4.24.0",
90-
"eslint": "^9.14.0",
90+
"eslint": "9.14.0",
9191
"eslint-plugin-jest-dom": "^5.0.1",
9292
"eslint-plugin-react": "^7.37.2",
9393
"eslint-plugin-react-compiler": "^19.0.0-beta-a7bf2bd-20241110",
@@ -108,7 +108,8 @@
108108
"rollup-plugin-postcss": "^4.0.2",
109109
"typescript": "~5.6.2",
110110
"vite": "^5.4.5",
111-
"vitest": "^2.1.1"
111+
"vitest": "^2.1.1",
112+
"vitest-browser-react": "^0.0.3"
112113
},
113114
"peerDependencies": {
114115
"react": "^18.0 || ^19.0",

test/browser/TextEditor.test.tsx

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
import { useState } from 'react';
2-
import { fireEvent, render, screen } from '@testing-library/react';
3-
import userEvent from '@testing-library/user-event';
2+
import { page, userEvent } from '@vitest/browser/context';
43

54
import DataGrid, { textEditor } from '../../src';
65
import type { Column } from '../../src';
7-
import { getCells } from './utils';
6+
import { getCellsNew } from './utils';
87

98
interface Row {
109
readonly name: string;
1110
}
1211

13-
const columns: readonly Column<Row>[] = [{ key: 'name', name: 'Name', renderEditCell: textEditor }];
12+
const columns: readonly Column<Row>[] = [
13+
{
14+
key: 'name',
15+
name: 'Name',
16+
renderEditCell: textEditor,
17+
editorOptions: {
18+
commitOnOutsideClick: false
19+
}
20+
}
21+
];
1422
const initialRows: readonly Row[] = [{ name: 'Tacitus Kilgore' }];
1523

1624
function Test() {
@@ -20,10 +28,10 @@ function Test() {
2028
}
2129

2230
test('TextEditor', async () => {
23-
render(<Test />);
31+
page.render(<Test />);
2432

25-
await userEvent.dblClick(getCells()[0]);
26-
let input: HTMLInputElement | null = screen.getByRole<HTMLInputElement>('textbox');
33+
await userEvent.dblClick(getCellsNew()[0]);
34+
let input = page.getByRole('textbox').element() as HTMLInputElement;
2735
expect(input).toHaveClass('rdg-text-editor');
2836
// input value is row[column.key]
2937
expect(input).toHaveValue(initialRows[0].name);
@@ -36,13 +44,13 @@ test('TextEditor', async () => {
3644
// pressing escape closes the editor without committing
3745
await userEvent.keyboard('Test{escape}');
3846
expect(input).not.toBeInTheDocument();
39-
expect(getCells()[0]).toHaveTextContent(/^Tacitus Kilgore$/);
47+
await expect.element(getCellsNew()[0]).toHaveTextContent(/^Tacitus Kilgore$/);
4048

4149
// blurring the input closes and commits the editor
42-
await userEvent.dblClick(getCells()[0]);
43-
input = screen.getByRole<HTMLInputElement>('textbox');
44-
await userEvent.keyboard('Jim Milton');
45-
fireEvent.blur(input);
50+
await userEvent.dblClick(getCellsNew()[0]);
51+
input = page.getByRole('textbox').element() as HTMLInputElement;
52+
await userEvent.fill(input, 'Jim Milton');
53+
await userEvent.tab();
4654
expect(input).not.toBeInTheDocument();
47-
expect(getCells()[0]).toHaveTextContent(/^Jim Milton$/);
55+
await expect.element(getCellsNew()[0]).toHaveTextContent(/^Jim Milton$/);
4856
});

0 commit comments

Comments
 (0)