Skip to content

Commit b7abc1b

Browse files
docs: update React Native example to use recommended practices. (#1136)
1 parent 4246380 commit b7abc1b

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

docs/react-native-testing-library/example-intro.mdx

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ sidebar_label: Example
55
---
66

77
```jsx
8-
import React from 'react'
8+
import * as React from 'react'
99
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'
1111

1212
function Example() {
1313
const [name, setUser] = React.useState('')
@@ -22,7 +22,7 @@ function Example() {
2222
// let's pretend this is making a server request, so it's async
2323
// (you'd want to mock this imaginary request in your unit tests)...
2424
setTimeout(() => {
25-
setShow(!show)
25+
setShow(true)
2626
}, Math.floor(Math.random() * 200))
2727
}}
2828
/>
@@ -32,20 +32,19 @@ function Example() {
3232
}
3333

3434
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()
5049
})
5150
```

0 commit comments

Comments
 (0)