1
1
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' ;
3
8
import App from '../App' ;
4
9
10
+ jest . useFakeTimers ( ) ;
11
+
5
12
/**
6
13
* A good place to start is having a tests that your component renders correctly.
7
14
*/
@@ -22,6 +29,9 @@ test('renders correctly', () => {
22
29
* his point of view.
23
30
*/
24
31
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
+
25
35
// Idiom: no need to capture render output, as we will use `screen` for queries.
26
36
render ( < App /> ) ;
27
37
@@ -32,11 +42,11 @@ test('User can sign in successully with correct credentials', async () => {
32
42
) . toBeOnTheScreen ( ) ;
33
43
34
44
// 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' ) ;
37
47
38
48
// 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' } ) ) ;
40
50
41
51
// Idiom: since pressing button triggers async operation we need to use `findBy*` query to wait
42
52
// for the action to complete.
@@ -67,15 +77,16 @@ test('User can sign in successully with correct credentials', async () => {
67
77
* Note: that some times you will have to resort to `getByTestId`, but treat it as a last resort.
68
78
*/
69
79
test ( 'User will see errors for incorrect credentials' , async ( ) => {
80
+ const user = userEvent . setup ( ) ;
70
81
render ( < App /> ) ;
71
82
72
83
expect (
73
84
screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
74
85
) . toBeOnTheScreen ( ) ;
75
86
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' } ) ) ;
79
90
80
91
// Hint: you can use custom Jest Native matcher to check text content.
81
92
expect ( await screen . findByRole ( 'alert' ) ) . toHaveTextContent (
@@ -93,22 +104,29 @@ test('User will see errors for incorrect credentials', async () => {
93
104
* Do not be afraid to write longer test scenarios, with repeating act and assert statements.
94
105
*/
95
106
test ( 'User can sign in after incorrect attempt' , async ( ) => {
107
+ const user = userEvent . setup ( ) ;
96
108
render ( < App /> ) ;
97
109
98
110
expect (
99
111
screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
100
112
) . toBeOnTheScreen ( ) ;
101
113
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' } ) ) ;
105
120
106
121
expect ( await screen . findByRole ( 'alert' ) ) . toHaveTextContent (
107
122
'Incorrect username or password'
108
123
) ;
109
124
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' } ) ) ;
112
130
113
131
expect ( await screen . findByText ( 'Welcome admin!' ) ) . toBeOnTheScreen ( ) ;
114
132
expect (
0 commit comments