Skip to content

Commit 719975e

Browse files
mdjastrzebskithymikee
authored andcommitted
feature: update docs
1 parent 1cd0608 commit 719975e

File tree

6 files changed

+129
-118
lines changed

6 files changed

+129
-118
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,20 @@ flow-typed install react-test-renderer
102102
## Example
103103

104104
```jsx
105-
import { render, fireEvent } from '@testing-library/react-native';
105+
import { render, screen, fireEvent } from '@testing-library/react-native';
106106
import { QuestionsBoard } from '../QuestionsBoard';
107107

108108
test('form submits two answers', () => {
109109
const allQuestions = ['q1', 'q2'];
110110
const mockFn = jest.fn();
111111

112-
const { getAllByLabelText, getByText } = render(
113-
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
114-
);
112+
render(<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />);
115113

116-
const answerInputs = getAllByLabelText('answer input');
114+
const answerInputs = screen.getAllByLabelText('answer input');
117115

118116
fireEvent.changeText(answerInputs[0], 'a1');
119117
fireEvent.changeText(answerInputs[1], 'a2');
120-
fireEvent.press(getByText('Submit'));
118+
fireEvent.press(screen.getByText('Submit'));
121119

122120
expect(mockFn).toBeCalledWith({
123121
'1': { q: 'q1', a: 'a1' },

website/docs/API.md

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ title: API
66
### Table of contents:
77

88
- [`render`](#render)
9+
- [`...queries`](#queries)
10+
- [Example](#example)
911
- [`update`](#update)
1012
- [`unmount`](#unmount)
1113
- [`debug`](#debug)
14+
- [`debug.shallow`](#debugshallow)
1215
- [`toJSON`](#tojson)
1316
- [`container`](#container)
1417
- [`cleanup`](#cleanup)
@@ -17,19 +20,26 @@ title: API
1720
- [`fireEvent.press`](#fireeventpress)
1821
- [`fireEvent.changeText`](#fireeventchangetext)
1922
- [`fireEvent.scroll`](#fireeventscroll)
23+
- [On a `ScrollView`](#on-a-scrollview)
24+
- [On a `FlatList`](#on-a-flatlist)
2025
- [`waitFor`](#waitfor)
2126
- [`waitForElementToBeRemoved`](#waitforelementtoberemoved)
22-
- [`within, getQueriesForElement`](#within-getqueriesforelement)
27+
- [`within`, `getQueriesForElement`](#within-getqueriesforelement)
2328
- [`query` APIs](#query-apis)
2429
- [`queryAll` APIs](#queryall-apis)
2530
- [`act`](#act)
2631
- [`renderHook`](#renderhook)
2732
- [`callback`](#callback)
28-
- [`options`](#options-optional)
33+
- [`options` (Optional)](#options-optional)
34+
- [`initialProps`](#initialprops)
35+
- [`wrapper`](#wrapper)
2936
- [`RenderHookResult` object](#renderhookresult-object)
3037
- [`result`](#result)
3138
- [`rerender`](#rerender)
3239
- [`unmount`](#unmount-1)
40+
- [Examples](#examples)
41+
- [With `initialProps`](#with-initialprops)
42+
- [With `wrapper`](#with-wrapper)
3343

3444
This page gathers public API of React Native Testing Library along with usage examples.
3545

@@ -58,8 +68,8 @@ import { render } from '@testing-library/react-native';
5868
import { QuestionsBoard } from '../QuestionsBoard';
5969

6070
test('should verify two questions', () => {
61-
const { queryAllByRole } = render(<QuestionsBoard {...props} />);
62-
const allQuestions = queryAllByRole('header');
71+
render(<QuestionsBoard {...props} />);
72+
const allQuestions = screen.queryAllByRole('header');
6373

6474
expect(allQuestions).toHaveLength(2);
6575
});
@@ -117,9 +127,9 @@ debug(message?: string): void
117127
Pretty prints deeply rendered component passed to `render` with optional message on top.
118128

119129
```jsx
120-
const { debug } = render(<Component />);
130+
render(<Component />);
121131

122-
debug('optional message');
132+
screen.debug('optional message');
123133
```
124134

125135
logs optional message and colored JSX:
@@ -154,6 +164,12 @@ container: ReactTestInstance;
154164

155165
A reference to the rendered root element.
156166

167+
:::info
168+
Last `render` result is kept in `screen` variable that you can import from `@testing-library/react-native` package.
169+
170+
Using screen instead of destructuring `render` result is recommended apprach. See [this article](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen) from Kent C. Dodds for more details.
171+
:::
172+
157173
## `cleanup`
158174

159175
```ts
@@ -208,17 +224,17 @@ Fires native-like event with data.
208224
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree.
209225

210226
```jsx
211-
import { render, fireEvent } from '@testing-library/react-native';
227+
import { render, screen, fireEvent } from '@testing-library/react-native';
212228

213229
test('fire changeText event', () => {
214230
const onEventMock = jest.fn();
215-
const { getByPlaceholderText } = render(
231+
render(
216232
// MyComponent renders TextInput which has a placeholder 'Enter details'
217233
// and with `onChangeText` bound to handleChangeText
218234
<MyComponent handleChangeText={onEventMock} />
219235
);
220236

221-
fireEvent(getByPlaceholderText('change'), 'onChangeText', 'ab');
237+
fireEvent(screen.getByPlaceholderText('change'), 'onChangeText', 'ab');
222238
expect(onEventMock).toHaveBeenCalledWith('ab');
223239
});
224240
```
@@ -235,14 +251,14 @@ import { fireEvent, render } from '@testing-library/react-native';
235251

236252
const onBlurMock = jest.fn();
237253

238-
const { getByPlaceholderText } = render(
254+
render(
239255
<View>
240256
<TextInput placeholder="my placeholder" onBlur={onBlurMock} />
241257
</View>
242258
);
243259

244260
// you can omit the `on` prefix
245-
fireEvent(getByPlaceholderText('my placeholder'), 'blur');
261+
fireEvent(screen.getByPlaceholderText('my placeholder'), 'blur');
246262
```
247263

248264
## `fireEvent[eventName]`
@@ -263,7 +279,7 @@ Invokes `press` event handler on the element or parent element in the tree.
263279

264280
```jsx
265281
import { View, Text, TouchableOpacity } from 'react-native';
266-
import { render, fireEvent } from '@testing-library/react-native';
282+
import { render, screen, fireEvent } from '@testing-library/react-native';
267283

268284
const onPressMock = jest.fn();
269285
const eventData = {
@@ -273,15 +289,15 @@ const eventData = {
273289
},
274290
};
275291

276-
const { getByText } = render(
292+
render(
277293
<View>
278294
<TouchableOpacity onPress={onPressMock}>
279295
<Text>Press me</Text>
280296
</TouchableOpacity>
281297
</View>
282298
);
283299

284-
fireEvent.press(getByText('Press me'), eventData);
300+
fireEvent.press(screen.getByText('Press me'), eventData);
285301
expect(onPressMock).toHaveBeenCalledWith(eventData);
286302
```
287303

@@ -295,18 +311,18 @@ Invokes `changeText` event handler on the element or parent element in the tree.
295311

296312
```jsx
297313
import { View, TextInput } from 'react-native';
298-
import { render, fireEvent } from '@testing-library/react-native';
314+
import { render, screen, fireEvent } from '@testing-library/react-native';
299315

300316
const onChangeTextMock = jest.fn();
301317
const CHANGE_TEXT = 'content';
302318

303-
const { getByPlaceholderText } = render(
319+
render(
304320
<View>
305321
<TextInput placeholder="Enter data" onChangeText={onChangeTextMock} />
306322
</View>
307323
);
308324

309-
fireEvent.changeText(getByPlaceholderText('Enter data'), CHANGE_TEXT);
325+
fireEvent.changeText(screen.getByPlaceholderText('Enter data'), CHANGE_TEXT);
310326
```
311327

312328
### `fireEvent.scroll`
@@ -321,7 +337,7 @@ Invokes `scroll` event handler on the element or parent element in the tree.
321337

322338
```jsx
323339
import { ScrollView, Text } from 'react-native';
324-
import { render, fireEvent } from '@testing-library/react-native';
340+
import { render, screen, fireEvent } from '@testing-library/react-native';
325341

326342
const onScrollMock = jest.fn();
327343
const eventData = {
@@ -332,23 +348,23 @@ const eventData = {
332348
},
333349
};
334350

335-
const { getByText } = render(
351+
render(
336352
<ScrollView onScroll={onScrollMock}>
337353
<Text>XD</Text>
338354
</ScrollView>
339355
);
340356

341-
fireEvent.scroll(getByText('scroll-view'), eventData);
357+
fireEvent.scroll(screen.getByText('scroll-view'), eventData);
342358
```
343359

344360
#### On a `FlatList`
345361

346362
```jsx
347363
import { FlatList, View } from 'react-native';
348-
import { render, fireEvent } from '@testing-library/react-native';
364+
import { render, screen, fireEvent } from '@testing-library/react-native';
349365

350366
const onEndReached = jest.fn();
351-
const { getByTestId } = render(
367+
render(
352368
<FlatList
353369
data={Array.from({ length: 10 }, (_, key) => ({ key: `${key}` }))}
354370
renderItem={() => <View style={{ height: 500, width: 100 }} />}
@@ -375,7 +391,7 @@ const eventData = {
375391
},
376392
};
377393

378-
fireEvent.scroll(getByTestId('flat-list'), eventData);
394+
fireEvent.scroll(screen.getByTestId('flat-list'), eventData);
379395
expect(onEndReached).toHaveBeenCalled();
380396
```
381397

@@ -399,12 +415,12 @@ function waitFor<T>(
399415
Waits for non-deterministic periods of time until your element appears or times out. `waitFor` periodically calls `expectation` every `interval` milliseconds to determine whether the element appeared or not.
400416

401417
```jsx
402-
import { render, waitFor } from '@testing-library/react-native';
418+
import { render, screen, waitFor } from '@testing-library/react-native';
403419

404420
test('waiting for an Banana to be ready', async () => {
405-
const { getByText } = render(<Banana />);
421+
render(<Banana />);
406422

407-
await waitFor(() => getByText('Banana ready'));
423+
await waitFor(() => screen.getByText('Banana ready'));
408424
});
409425
```
410426

@@ -428,15 +444,12 @@ function waitForElementToBeRemoved<T>(
428444
Waits for non-deterministic periods of time until queried element is removed or times out. `waitForElementToBeRemoved` periodically calls `expectation` every `interval` milliseconds to determine whether the element has been removed or not.
429445

430446
```jsx
431-
import {
432-
render,
433-
waitForElementToBeRemoved,
434-
} from '@testing-library/react-native';
447+
import { render, screen, waitForElementToBeRemoved } from '@testing-library/react-native';
435448

436449
test('waiting for an Banana to be removed', async () => {
437-
const { getByText } = render(<Banana />);
450+
render(<Banana />);
438451

439-
await waitForElementToBeRemoved(() => getByText('Banana ready'));
452+
await waitForElementToBeRemoved(() => screen.getByText('Banana ready'));
440453
});
441454
```
442455

@@ -466,7 +479,7 @@ Please note that additional `render` specific operations like `update`, `unmount
466479
:::
467480

468481
```jsx
469-
const detailsScreen = within(getByA11yHint('Details Screen'));
482+
const detailsScreen = within(screen.getByA11yHint('Details Screen'));
470483
expect(detailsScreen.getByText('Some Text')).toBeTruthy();
471484
expect(detailsScreen.getByDisplayValue('Some Value')).toBeTruthy();
472485
expect(detailsScreen.queryByLabelText('Some Label')).toBeTruthy();
@@ -483,10 +496,10 @@ Use cases for scoped queries include:
483496
Each of the get APIs listed in the render section above have a complimentary query API. The get APIs will throw errors if a proper node cannot be found. This is normally the desired effect. However, if you want to make an assertion that an element is not present in the hierarchy, then you can use the query API instead:
484497

485498
```jsx
486-
import { render } from '@testing-library/react-native';
499+
import { render, screen } from '@testing-library/react-native';
487500

488-
const { queryByText } = render(<Form />);
489-
const submitButton = queryByText('submit');
501+
render(<Form />);
502+
const submitButton = screen.queryByText('submit');
490503
expect(submitButton).toBeNull(); // it doesn't exist
491504
```
492505

@@ -497,8 +510,8 @@ Each of the query APIs have a corresponding queryAll version that always returns
497510
```jsx
498511
import { render } from '@testing-library/react-native';
499512

500-
const { queryAllByText } = render(<Forms />);
501-
const submitButtons = queryAllByText('submit');
513+
render(<Forms />);
514+
const submitButtons = screen.queryAllByText('submit');
502515
expect(submitButtons).toHaveLength(3); // expect 3 elements
503516
```
504517

website/docs/GettingStarted.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,22 +75,20 @@ flow-typed install react-test-renderer
7575
## Example
7676

7777
```jsx
78-
import { render, fireEvent } from '@testing-library/react-native';
78+
import { render, screen, fireEvent } from '@testing-library/react-native';
7979
import { QuestionsBoard } from '../QuestionsBoard';
8080

8181
test('form submits two answers', () => {
8282
const allQuestions = ['q1', 'q2'];
8383
const mockFn = jest.fn();
8484

85-
const { getAllByLabelText, getByText } = render(
86-
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
87-
);
85+
render(<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />);
8886

89-
const answerInputs = getAllByLabelText('answer input');
87+
const answerInputs = screen.getAllByLabelText('answer input');
9088

9189
fireEvent.changeText(answerInputs[0], 'a1');
9290
fireEvent.changeText(answerInputs[1], 'a2');
93-
fireEvent.press(getByText('Submit'));
91+
fireEvent.press(screen.getByText('Submit'));
9492

9593
expect(mockFn).toBeCalledWith({
9694
'1': { q: 'q1', a: 'a1' },

0 commit comments

Comments
 (0)