@@ -5,9 +5,9 @@ sidebar_label: Example
5
5
---
6
6
7
7
``` jsx
8
- import React from ' react'
8
+ import * as React from ' react'
9
9
import {Button , Text , TextInput , View } from ' react-native'
10
- import {fireEvent , render , waitFor } from ' @testing-library/react-native'
10
+ import {render , screen , fireEvent } from ' @testing-library/react-native'
11
11
12
12
function Example () {
13
13
const [name , setUser ] = React .useState (' ' )
@@ -22,7 +22,7 @@ function Example() {
22
22
// let's pretend this is making a server request, so it's async
23
23
// (you'd want to mock this imaginary request in your unit tests)...
24
24
setTimeout (() => {
25
- setShow (! show )
25
+ setShow (true )
26
26
}, Math .floor (Math .random () * 200 ))
27
27
}}
28
28
/ >
@@ -32,20 +32,19 @@ function Example() {
32
32
}
33
33
34
34
test (' examples of some things' , async () => {
35
- const {getByTestId , getByText , queryByTestId , toJSON } = render (< Example / > )
36
- const famousProgrammerInHistory = ' Ada Lovelace'
37
-
38
- const input = getByTestId (' input' )
39
- fireEvent .changeText (input, famousProgrammerInHistory)
40
-
41
- const button = getByText (' Print Username' )
42
- fireEvent .press (button)
43
-
44
- await waitFor (() => expect (queryByTestId (' printed-username' )).toBeTruthy ())
45
-
46
- expect (getByTestId (' printed-username' ).props .children ).toBe (
47
- famousProgrammerInHistory,
48
- )
49
- expect (toJSON ()).toMatchSnapshot ()
35
+ const expectedUsername = ' Ada Lovelace'
36
+
37
+ render (< Example / > )
38
+
39
+ fireEvent .changeText (screen .getByTestId (' input' ), expectedUsername)
40
+ fireEvent .press (screen .getByText (' Print Username' ))
41
+
42
+ // Using `findBy` query to wait for asynchronous operation to finish
43
+ const usernameOutput = await screen .findByTestId (' printed-username' )
44
+
45
+ // Using `toHaveTextContent` matcher from `@testing-library/jest-native` package.
46
+ expect (usernameOutput).toHaveTextContent (expectedUsername);
47
+
48
+ expect (screen .toJSON ()).toMatchSnapshot ()
50
49
})
51
50
```
0 commit comments