Skip to content

Commit 1876ca4

Browse files
committed
Try out vitest-browser-react
1 parent dd76e81 commit 1876ca4

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useState } from 'react';
2-
import { fireEvent, render, screen } from '@testing-library/react';
3-
import userEvent from '@testing-library/user-event';
2+
import { userEvent } from '@vitest/browser/context';
3+
import { render } from 'vitest-browser-react';
44

55
import DataGrid, { textEditor } from '../../src';
66
import type { Column } from '../../src';
@@ -10,7 +10,16 @@ interface Row {
1010
readonly name: string;
1111
}
1212

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

1625
function Test() {
@@ -20,10 +29,10 @@ function Test() {
2029
}
2130

2231
test('TextEditor', async () => {
23-
render(<Test />);
32+
const screen = render(<Test />);
2433

2534
await userEvent.dblClick(getCells()[0]);
26-
let input: HTMLInputElement | null = screen.getByRole<HTMLInputElement>('textbox');
35+
let input = screen.getByRole('textbox').element() as HTMLInputElement;
2736
expect(input).toHaveClass('rdg-text-editor');
2837
// input value is row[column.key]
2938
expect(input).toHaveValue(initialRows[0].name);
@@ -40,9 +49,9 @@ test('TextEditor', async () => {
4049

4150
// blurring the input closes and commits the editor
4251
await userEvent.dblClick(getCells()[0]);
43-
input = screen.getByRole<HTMLInputElement>('textbox');
52+
input = screen.getByRole('textbox').element() as HTMLInputElement;
4453
await userEvent.keyboard('Jim Milton');
45-
fireEvent.blur(input);
46-
expect(input).not.toBeInTheDocument();
54+
await userEvent.tab();
55+
await expect.element(input).not.toBeInTheDocument();
4756
expect(getCells()[0]).toHaveTextContent(/^Jim Milton$/);
4857
});

test/setup.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { configure } from '@testing-library/react';
2+
import { configure as vitestReactConfigure } from 'vitest-browser-react/pure';
23

34
configure({
45
reactStrictMode: true,
56
throwSuggestions: true
67
});
8+
9+
vitestReactConfigure({
10+
reactStrictMode: true
11+
});

0 commit comments

Comments
 (0)