Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use vitest-browser-react and browserUserEvent #3629

Merged
merged 19 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ export default [
'testing-library/prefer-presence-queries': 1,
'testing-library/prefer-query-by-disappearance': 1,
'testing-library/prefer-query-matchers': 0,
'testing-library/prefer-screen-queries': 1,
'testing-library/prefer-screen-queries': 0,
Copy link
Contributor Author

@amanmahajan7 amanmahajan7 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to disable it as this complains about page...

'testing-library/prefer-user-event': 1,
'testing-library/render-result-naming-convention': 0
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
"@wyw-in-js/vite": "^0.5.0",
"babel-plugin-optimize-clsx": "^2.6.2",
"browserslist": "^4.24.0",
"eslint": "^9.14.0",
"eslint": "9.14.0",
"eslint-plugin-jest-dom": "^5.0.1",
"eslint-plugin-react": "^7.37.2",
"eslint-plugin-react-compiler": "^19.0.0-beta-a7bf2bd-20241110",
Expand All @@ -108,7 +108,8 @@
"rollup-plugin-postcss": "^4.0.2",
"typescript": "~5.6.2",
"vite": "^5.4.5",
"vitest": "^2.1.1"
"vitest": "^2.1.1",
"vitest-browser-react": "^0.0.3"
},
"peerDependencies": {
"react": "^18.0 || ^19.0",
Expand Down
34 changes: 21 additions & 13 deletions test/browser/TextEditor.test.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { useState } from 'react';
import { fireEvent, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { page, userEvent } from '@vitest/browser/context';

import DataGrid, { textEditor } from '../../src';
import type { Column } from '../../src';
import { getCells } from './utils';
import { getCellsNew } from './utils';

interface Row {
readonly name: string;
}

const columns: readonly Column<Row>[] = [{ key: 'name', name: 'Name', renderEditCell: textEditor }];
const columns: readonly Column<Row>[] = [
{
key: 'name',
name: 'Name',
renderEditCell: textEditor,
editorOptions: {
commitOnOutsideClick: false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this so we can test the blur event

}
}
];
const initialRows: readonly Row[] = [{ name: 'Tacitus Kilgore' }];

function Test() {
Expand All @@ -20,10 +28,10 @@ function Test() {
}

test('TextEditor', async () => {
render(<Test />);
page.render(<Test />);

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

// blurring the input closes and commits the editor
await userEvent.dblClick(getCells()[0]);
input = screen.getByRole<HTMLInputElement>('textbox');
await userEvent.keyboard('Jim Milton');
fireEvent.blur(input);
await userEvent.dblClick(getCellsNew()[0]);
input = page.getByRole('textbox').element() as HTMLInputElement;
await userEvent.fill(input, 'Jim Milton');
await userEvent.tab();
expect(input).not.toBeInTheDocument();
expect(getCells()[0]).toHaveTextContent(/^Jim Milton$/);
await expect.element(getCellsNew()[0]).toHaveTextContent(/^Jim Milton$/);
});
Loading