@@ -9,9 +9,11 @@ test('renders correctly', () => {
99 // Idiom: no need to capture render output, as we will use `screen` for queries.
1010 render ( < App /> ) ;
1111
12- // Idiom: `getByXxx` is a predicate by itself , but we will use it with `expect().toBeTruthy()`
12+ // Idiom: `getBy*` queries are predicates by themselves , but we will use it with `expect().toBeTruthy()`
1313 // to clarify our intent.
14- expect ( screen . getByText ( 'Sign in to Example App' ) ) . toBeTruthy ( ) ;
14+ expect (
15+ screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
16+ ) . toBeTruthy ( ) ;
1517} ) ;
1618
1719/**
@@ -23,30 +25,34 @@ test('User can sign in successully with correct credentials', async () => {
2325 // Idiom: no need to capture render output, as we will use `screen` for queries.
2426 render ( < App /> ) ;
2527
26- // Idiom: `getByXxx` is a predicate by itself , but we will use it with `expect().toBeTruthy()` to
27- // clarify our intent.
28+ // Idiom: `getBy*` queries are predicates by themselves , but we will use it with `expect().toBeTruthy()`
29+ // to clarify our intent.
2830 // Note: `.toBeTruthy()` is the preferred matcher for checking that elements are present.
29- expect ( screen . getByText ( 'Sign in to Example App' ) ) . toBeTruthy ( ) ;
30- expect ( screen . getByText ( 'Username' ) ) . toBeTruthy ( ) ;
31- expect ( screen . getByText ( 'Password' ) ) . toBeTruthy ( ) ;
31+ expect (
32+ screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
33+ ) . toBeTruthy ( ) ;
3234
33- // Hint: we can use `getByLabelText` to find our text inputs in accessibility-friendly way .
35+ // Hint: we can use `getByLabelText` to find our text inputs using their labels .
3436 fireEvent . changeText ( screen . getByLabelText ( 'Username' ) , 'admin' ) ;
3537 fireEvent . changeText ( screen . getByLabelText ( 'Password' ) , 'admin1' ) ;
3638
37- // Hint: we can use `getByText ` to find our button by its text.
38- fireEvent . press ( screen . getByText ( ' Sign In') ) ;
39+ // Hint: we can use `getByRole ` to find our button with given text.
40+ fireEvent . press ( screen . getByRole ( 'button' , { name : ' Sign In' } ) ) ;
3941
40- // Idiom: since pressing button triggers async operation we need to use `findBy` query to wait
42+ // Idiom: since pressing button triggers async operation we need to use `findBy* ` query to wait
4143 // for the action to complete.
42- // Hint: subsequent queries do not need to use `findBy`, because they are used after the async action
44+ // Hint: subsequent queries do not need to use `findBy* `, because they are used after the async action
4345 // already finished
44- expect ( await screen . findByText ( 'Welcome admin!' ) ) . toBeTruthy ( ) ;
45-
46- // Idiom: use `queryByXxx` with `expect().toBeFalsy()` to assess that element is not present.
47- expect ( screen . queryByText ( 'Sign in to Example App' ) ) . toBeFalsy ( ) ;
48- expect ( screen . queryByText ( 'Username' ) ) . toBeFalsy ( ) ;
49- expect ( screen . queryByText ( 'Password' ) ) . toBeFalsy ( ) ;
46+ expect (
47+ await screen . findByRole ( 'header' , { name : 'Welcome admin!' } )
48+ ) . toBeTruthy ( ) ;
49+
50+ // Idiom: use `queryBy*` with `expect().toBeFalsy()` to assess that element is not present.
51+ expect (
52+ screen . queryByRole ( 'header' , { name : 'Sign in to Example App' } )
53+ ) . toBeFalsy ( ) ;
54+ expect ( screen . queryByLabelText ( 'Username' ) ) . toBeFalsy ( ) ;
55+ expect ( screen . queryByLabelText ( 'Password' ) ) . toBeFalsy ( ) ;
5056} ) ;
5157
5258/**
@@ -56,30 +62,32 @@ test('User can sign in successully with correct credentials', async () => {
5662 * that is not directly reflected in the UI.
5763 *
5864 * For this reason prefer quries that correspond to things directly observable by the user like:
59- * `getByText`, `getByLabelText`, `getByPlaceholderText, `getByDisplayValue`, `getByRole `, etc.
65+ * `getByRole`, ` getByText`, `getByLabelText`, `getByPlaceholderText, `getByDisplayValue`, etc.
6066 * over `getByTestId` which is not directly observable by the user.
6167 *
6268 * Note: that some times you will have to resort to `getByTestId`, but treat it as a last resort.
6369 */
6470test ( 'User will see errors for incorrect credentials' , async ( ) => {
6571 render ( < App /> ) ;
6672
67- expect ( screen . getByText ( 'Sign in to Example App' ) ) . toBeTruthy ( ) ;
68- expect ( screen . getByText ( 'Username' ) ) . toBeTruthy ( ) ;
69- expect ( screen . getByText ( 'Password' ) ) . toBeTruthy ( ) ;
73+ expect (
74+ screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
75+ ) . toBeTruthy ( ) ;
7076
7177 fireEvent . changeText ( screen . getByLabelText ( 'Username' ) , 'admin' ) ;
7278 fireEvent . changeText ( screen . getByLabelText ( 'Password' ) , 'qwerty123' ) ;
73- fireEvent . press ( screen . getByText ( ' Sign In') ) ;
79+ fireEvent . press ( screen . getByRole ( 'button' , { name : ' Sign In' } ) ) ;
7480
7581 // Hint: you can use custom Jest Native matcher to check text content.
76- expect ( await screen . findByLabelText ( 'Error ') ) . toHaveTextContent (
82+ expect ( await screen . findByRole ( 'alert ') ) . toHaveTextContent (
7783 'Incorrect username or password'
7884 ) ;
7985
80- expect ( screen . getByText ( 'Sign in to Example App' ) ) . toBeTruthy ( ) ;
81- expect ( screen . getByText ( 'Username' ) ) . toBeTruthy ( ) ;
82- expect ( screen . getByText ( 'Password' ) ) . toBeTruthy ( ) ;
86+ expect (
87+ screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
88+ ) . toBeTruthy ( ) ;
89+ expect ( screen . getByLabelText ( 'Username' ) ) . toBeTruthy ( ) ;
90+ expect ( screen . getByLabelText ( 'Password' ) ) . toBeTruthy ( ) ;
8391} ) ;
8492
8593/**
@@ -88,23 +96,25 @@ test('User will see errors for incorrect credentials', async () => {
8896test ( 'User can sign in after incorrect attempt' , async ( ) => {
8997 render ( < App /> ) ;
9098
91- expect ( screen . getByText ( 'Sign in to Example App' ) ) . toBeTruthy ( ) ;
92- expect ( screen . getByText ( 'Username' ) ) . toBeTruthy ( ) ;
93- expect ( screen . getByText ( 'Password' ) ) . toBeTruthy ( ) ;
99+ expect (
100+ screen . getByRole ( 'header' , { name : 'Sign in to Example App' } )
101+ ) . toBeTruthy ( ) ;
94102
95103 fireEvent . changeText ( screen . getByLabelText ( 'Username' ) , 'admin' ) ;
96104 fireEvent . changeText ( screen . getByLabelText ( 'Password' ) , 'qwerty123' ) ;
97- fireEvent . press ( screen . getByText ( ' Sign In') ) ;
105+ fireEvent . press ( screen . getByRole ( 'button' , { name : ' Sign In' } ) ) ;
98106
99- expect ( await screen . findByLabelText ( 'Error ') ) . toHaveTextContent (
107+ expect ( await screen . findByRole ( 'alert ') ) . toHaveTextContent (
100108 'Incorrect username or password'
101109 ) ;
102110
103111 fireEvent . changeText ( screen . getByLabelText ( 'Password' ) , 'admin1' ) ;
104- fireEvent . press ( screen . getByText ( ' Sign In') ) ;
112+ fireEvent . press ( screen . getByRole ( 'button' , { name : ' Sign In' } ) ) ;
105113
106114 expect ( await screen . findByText ( 'Welcome admin!' ) ) . toBeTruthy ( ) ;
107- expect ( screen . queryByText ( 'Sign in to Example App' ) ) . toBeFalsy ( ) ;
108- expect ( screen . queryByText ( 'Username' ) ) . toBeFalsy ( ) ;
109- expect ( screen . queryByText ( 'Password' ) ) . toBeFalsy ( ) ;
115+ expect (
116+ screen . queryByRole ( 'header' , { name : 'Sign in to Example App' } )
117+ ) . toBeFalsy ( ) ;
118+ expect ( screen . queryByLabelText ( 'Username' ) ) . toBeFalsy ( ) ;
119+ expect ( screen . queryByLabelText ( 'Password' ) ) . toBeFalsy ( ) ;
110120} ) ;
0 commit comments