Skip to content

Commit b70c07d

Browse files
chore: update basic example with User Event (#1438)
* chore: update basic example with User Event * chore: add fake timers
1 parent dace5cb commit b70c07d

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

examples/basic/__tests__/App.test.tsx

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import * as React from 'react';
2-
import { render, screen, fireEvent } from '@testing-library/react-native';
2+
import {
3+
render,
4+
screen,
5+
fireEvent,
6+
userEvent,
7+
} from '@testing-library/react-native';
38
import App from '../App';
49

10+
jest.useFakeTimers();
11+
512
/**
613
* A good place to start is having a tests that your component renders correctly.
714
*/
@@ -22,6 +29,9 @@ test('renders correctly', () => {
2229
* his point of view.
2330
*/
2431
test('User can sign in successully with correct credentials', async () => {
32+
// Setup User Event instance for realistic simulation of user interaction.
33+
const user = userEvent.setup();
34+
2535
// Idiom: no need to capture render output, as we will use `screen` for queries.
2636
render(<App />);
2737

@@ -32,11 +42,11 @@ test('User can sign in successully with correct credentials', async () => {
3242
).toBeOnTheScreen();
3343

3444
// Hint: we can use `getByLabelText` to find our text inputs using their labels.
35-
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
36-
fireEvent.changeText(screen.getByLabelText('Password'), 'admin1');
45+
await user.type(screen.getByLabelText('Username'), 'admin');
46+
await user.type(screen.getByLabelText('Password'), 'admin1');
3747

3848
// Hint: we can use `getByRole` to find our button with given text.
39-
fireEvent.press(screen.getByRole('button', { name: 'Sign In' }));
49+
await user.press(screen.getByRole('button', { name: 'Sign In' }));
4050

4151
// Idiom: since pressing button triggers async operation we need to use `findBy*` query to wait
4252
// for the action to complete.
@@ -67,15 +77,16 @@ test('User can sign in successully with correct credentials', async () => {
6777
* Note: that some times you will have to resort to `getByTestId`, but treat it as a last resort.
6878
*/
6979
test('User will see errors for incorrect credentials', async () => {
80+
const user = userEvent.setup();
7081
render(<App />);
7182

7283
expect(
7384
screen.getByRole('header', { name: 'Sign in to Example App' })
7485
).toBeOnTheScreen();
7586

76-
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
77-
fireEvent.changeText(screen.getByLabelText('Password'), 'qwerty123');
78-
fireEvent.press(screen.getByRole('button', { name: 'Sign In' }));
87+
await user.type(screen.getByLabelText('Username'), 'admin');
88+
await user.type(screen.getByLabelText('Password'), 'qwerty123');
89+
await user.press(screen.getByRole('button', { name: 'Sign In' }));
7990

8091
// Hint: you can use custom Jest Native matcher to check text content.
8192
expect(await screen.findByRole('alert')).toHaveTextContent(
@@ -93,22 +104,29 @@ test('User will see errors for incorrect credentials', async () => {
93104
* Do not be afraid to write longer test scenarios, with repeating act and assert statements.
94105
*/
95106
test('User can sign in after incorrect attempt', async () => {
107+
const user = userEvent.setup();
96108
render(<App />);
97109

98110
expect(
99111
screen.getByRole('header', { name: 'Sign in to Example App' })
100112
).toBeOnTheScreen();
101113

102-
fireEvent.changeText(screen.getByLabelText('Username'), 'admin');
103-
fireEvent.changeText(screen.getByLabelText('Password'), 'qwerty123');
104-
fireEvent.press(screen.getByRole('button', { name: 'Sign In' }));
114+
const usernameInput = screen.getByLabelText('Username');
115+
const passwordInput = screen.getByLabelText('Password');
116+
117+
await user.type(usernameInput, 'admin');
118+
await user.type(passwordInput, 'qwerty123');
119+
await user.press(screen.getByRole('button', { name: 'Sign In' }));
105120

106121
expect(await screen.findByRole('alert')).toHaveTextContent(
107122
'Incorrect username or password'
108123
);
109124

110-
fireEvent.changeText(screen.getByLabelText('Password'), 'admin1');
111-
fireEvent.press(screen.getByRole('button', { name: 'Sign In' }));
125+
// Workaround for clearing TextInput, clear() function will be added soon.
126+
fireEvent.changeText(passwordInput, '');
127+
128+
await user.type(passwordInput, 'admin1');
129+
await user.press(screen.getByRole('button', { name: 'Sign In' }));
112130

113131
expect(await screen.findByText('Welcome admin!')).toBeOnTheScreen();
114132
expect(

examples/basic/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"devDependencies": {
2121
"@babel/core": "^7.20.0",
2222
"@testing-library/jest-native": "^5.4.2",
23-
"@testing-library/react-native": "^12.1.3",
23+
"@testing-library/react-native": "^12.2.0",
2424
"@types/react": "~18.2.14",
2525
"jest": "^29.3.0",
2626
"react-test-renderer": "18.2.0",

0 commit comments

Comments
 (0)