@@ -6,9 +6,12 @@ title: API
6
6
### Table of contents:
7
7
8
8
- [ ` render ` ] ( #render )
9
+ - [ ` ...queries ` ] ( #queries )
10
+ - [ Example] ( #example )
9
11
- [ ` update ` ] ( #update )
10
12
- [ ` unmount ` ] ( #unmount )
11
13
- [ ` debug ` ] ( #debug )
14
+ - [ ` debug.shallow ` ] ( #debugshallow )
12
15
- [ ` toJSON ` ] ( #tojson )
13
16
- [ ` container ` ] ( #container )
14
17
- [ ` cleanup ` ] ( #cleanup )
@@ -17,19 +20,26 @@ title: API
17
20
- [ ` fireEvent.press ` ] ( #fireeventpress )
18
21
- [ ` fireEvent.changeText ` ] ( #fireeventchangetext )
19
22
- [ ` fireEvent.scroll ` ] ( #fireeventscroll )
23
+ - [ On a ` ScrollView ` ] ( #on-a-scrollview )
24
+ - [ On a ` FlatList ` ] ( #on-a-flatlist )
20
25
- [ ` waitFor ` ] ( #waitfor )
21
26
- [ ` waitForElementToBeRemoved ` ] ( #waitforelementtoberemoved )
22
- - [ ` within, getQueriesForElement ` ] ( #within-getqueriesforelement )
27
+ - [ ` within ` , ` getQueriesForElement ` ] ( #within-getqueriesforelement )
23
28
- [ ` query ` APIs] ( #query-apis )
24
29
- [ ` queryAll ` APIs] ( #queryall-apis )
25
30
- [ ` act ` ] ( #act )
26
31
- [ ` renderHook ` ] ( #renderhook )
27
32
- [ ` callback ` ] ( #callback )
28
- - [ ` options ` ] ( #options-optional )
33
+ - [ ` options ` (Optional)] ( #options-optional )
34
+ - [ ` initialProps ` ] ( #initialprops )
35
+ - [ ` wrapper ` ] ( #wrapper )
29
36
- [ ` RenderHookResult ` object] ( #renderhookresult-object )
30
37
- [ ` result ` ] ( #result )
31
38
- [ ` rerender ` ] ( #rerender )
32
39
- [ ` unmount ` ] ( #unmount-1 )
40
+ - [ Examples] ( #examples )
41
+ - [ With ` initialProps ` ] ( #with-initialprops )
42
+ - [ With ` wrapper ` ] ( #with-wrapper )
33
43
34
44
This page gathers public API of React Native Testing Library along with usage examples.
35
45
@@ -58,8 +68,8 @@ import { render } from '@testing-library/react-native';
58
68
import { QuestionsBoard } from ' ../QuestionsBoard' ;
59
69
60
70
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' );
63
73
64
74
expect (allQuestions).toHaveLength (2 );
65
75
});
@@ -117,9 +127,9 @@ debug(message?: string): void
117
127
Pretty prints deeply rendered component passed to ` render ` with optional message on top.
118
128
119
129
``` jsx
120
- const { debug } = render (< Component / > );
130
+ render (< Component / > );
121
131
122
- debug (' optional message' );
132
+ screen . debug (' optional message' );
123
133
```
124
134
125
135
logs optional message and colored JSX:
@@ -154,6 +164,12 @@ container: ReactTestInstance;
154
164
155
165
A reference to the rendered root element.
156
166
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
+
157
173
## ` cleanup `
158
174
159
175
``` ts
@@ -208,17 +224,17 @@ Fires native-like event with data.
208
224
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree.
209
225
210
226
``` jsx
211
- import { render , fireEvent } from ' @testing-library/react-native' ;
227
+ import { render , screen , fireEvent } from ' @testing-library/react-native' ;
212
228
213
229
test (' fire changeText event' , () => {
214
230
const onEventMock = jest .fn ();
215
- const { getByPlaceholderText } = render (
231
+ render (
216
232
// MyComponent renders TextInput which has a placeholder 'Enter details'
217
233
// and with `onChangeText` bound to handleChangeText
218
234
< MyComponent handleChangeText= {onEventMock} / >
219
235
);
220
236
221
- fireEvent (getByPlaceholderText (' change' ), ' onChangeText' , ' ab' );
237
+ fireEvent (screen . getByPlaceholderText (' change' ), ' onChangeText' , ' ab' );
222
238
expect (onEventMock).toHaveBeenCalledWith (' ab' );
223
239
});
224
240
```
@@ -235,14 +251,14 @@ import { fireEvent, render } from '@testing-library/react-native';
235
251
236
252
const onBlurMock = jest .fn ();
237
253
238
- const { getByPlaceholderText } = render (
254
+ render (
239
255
< View>
240
256
< TextInput placeholder= " my placeholder" onBlur= {onBlurMock} / >
241
257
< / View>
242
258
);
243
259
244
260
// you can omit the `on` prefix
245
- fireEvent (getByPlaceholderText (' my placeholder' ), ' blur' );
261
+ fireEvent (screen . getByPlaceholderText (' my placeholder' ), ' blur' );
246
262
```
247
263
248
264
## ` fireEvent[eventName] `
@@ -263,7 +279,7 @@ Invokes `press` event handler on the element or parent element in the tree.
263
279
264
280
``` jsx
265
281
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' ;
267
283
268
284
const onPressMock = jest .fn ();
269
285
const eventData = {
@@ -273,15 +289,15 @@ const eventData = {
273
289
},
274
290
};
275
291
276
- const { getByText } = render (
292
+ render (
277
293
< View>
278
294
< TouchableOpacity onPress= {onPressMock}>
279
295
< Text > Press me< / Text >
280
296
< / TouchableOpacity>
281
297
< / View>
282
298
);
283
299
284
- fireEvent .press (getByText (' Press me' ), eventData);
300
+ fireEvent .press (screen . getByText (' Press me' ), eventData);
285
301
expect (onPressMock).toHaveBeenCalledWith (eventData);
286
302
```
287
303
@@ -295,18 +311,18 @@ Invokes `changeText` event handler on the element or parent element in the tree.
295
311
296
312
``` jsx
297
313
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' ;
299
315
300
316
const onChangeTextMock = jest .fn ();
301
317
const CHANGE_TEXT = ' content' ;
302
318
303
- const { getByPlaceholderText } = render (
319
+ render (
304
320
< View>
305
321
< TextInput placeholder= " Enter data" onChangeText= {onChangeTextMock} / >
306
322
< / View>
307
323
);
308
324
309
- fireEvent .changeText (getByPlaceholderText (' Enter data' ), CHANGE_TEXT );
325
+ fireEvent .changeText (screen . getByPlaceholderText (' Enter data' ), CHANGE_TEXT );
310
326
```
311
327
312
328
### ` fireEvent.scroll `
@@ -321,7 +337,7 @@ Invokes `scroll` event handler on the element or parent element in the tree.
321
337
322
338
``` jsx
323
339
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' ;
325
341
326
342
const onScrollMock = jest .fn ();
327
343
const eventData = {
@@ -332,23 +348,23 @@ const eventData = {
332
348
},
333
349
};
334
350
335
- const { getByText } = render (
351
+ render (
336
352
< ScrollView onScroll= {onScrollMock}>
337
353
< Text > XD < / Text >
338
354
< / ScrollView>
339
355
);
340
356
341
- fireEvent .scroll (getByText (' scroll-view' ), eventData);
357
+ fireEvent .scroll (screen . getByText (' scroll-view' ), eventData);
342
358
```
343
359
344
360
#### On a ` FlatList `
345
361
346
362
``` jsx
347
363
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' ;
349
365
350
366
const onEndReached = jest .fn ();
351
- const { getByTestId } = render (
367
+ render (
352
368
< FlatList
353
369
data= {Array .from ({ length: 10 }, (_ , key ) => ({ key: ` ${ key} ` }))}
354
370
renderItem= {() => < View style= {{ height: 500 , width: 100 }} / > }
@@ -375,7 +391,7 @@ const eventData = {
375
391
},
376
392
};
377
393
378
- fireEvent .scroll (getByTestId (' flat-list' ), eventData);
394
+ fireEvent .scroll (screen . getByTestId (' flat-list' ), eventData);
379
395
expect (onEndReached).toHaveBeenCalled ();
380
396
```
381
397
@@ -399,12 +415,12 @@ function waitFor<T>(
399
415
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.
400
416
401
417
``` jsx
402
- import { render , waitFor } from ' @testing-library/react-native' ;
418
+ import { render , screen , waitFor } from ' @testing-library/react-native' ;
403
419
404
420
test (' waiting for an Banana to be ready' , async () => {
405
- const { getByText } = render (< Banana / > );
421
+ render (< Banana / > );
406
422
407
- await waitFor (() => getByText (' Banana ready' ));
423
+ await waitFor (() => screen . getByText (' Banana ready' ));
408
424
});
409
425
```
410
426
@@ -428,15 +444,12 @@ function waitForElementToBeRemoved<T>(
428
444
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.
429
445
430
446
``` jsx
431
- import {
432
- render ,
433
- waitForElementToBeRemoved ,
434
- } from ' @testing-library/react-native' ;
447
+ import { render , screen , waitForElementToBeRemoved } from ' @testing-library/react-native' ;
435
448
436
449
test (' waiting for an Banana to be removed' , async () => {
437
- const { getByText } = render (< Banana / > );
450
+ render (< Banana / > );
438
451
439
- await waitForElementToBeRemoved (() => getByText (' Banana ready' ));
452
+ await waitForElementToBeRemoved (() => screen . getByText (' Banana ready' ));
440
453
});
441
454
```
442
455
@@ -466,7 +479,7 @@ Please note that additional `render` specific operations like `update`, `unmount
466
479
:::
467
480
468
481
```jsx
469
- const detailsScreen = within(getByA11yHint('Details Screen'));
482
+ const detailsScreen = within(screen. getByA11yHint('Details Screen'));
470
483
expect(detailsScreen.getByText('Some Text')).toBeTruthy();
471
484
expect(detailsScreen.getByDisplayValue('Some Value')).toBeTruthy();
472
485
expect(detailsScreen.queryByLabelText('Some Label')).toBeTruthy();
@@ -483,10 +496,10 @@ Use cases for scoped queries include:
483
496
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:
484
497
485
498
```jsx
486
- import { render } from ' @testing-library/react-native' ;
499
+ import { render, screen } from ' @testing-library/react-native' ;
487
500
488
- const { queryByText } = render (< Form / > );
489
- const submitButton = queryByText (' submit' );
501
+ render (< Form / > );
502
+ const submitButton = screen . queryByText (' submit' );
490
503
expect (submitButton).toBeNull (); // it doesn't exist
491
504
```
492
505
@@ -497,8 +510,8 @@ Each of the query APIs have a corresponding queryAll version that always returns
497
510
``` jsx
498
511
import { render } from ' @testing-library/react-native' ;
499
512
500
- const { queryAllByText } = render (< Forms / > );
501
- const submitButtons = queryAllByText (' submit' );
513
+ render (< Forms / > );
514
+ const submitButtons = screen . queryAllByText (' submit' );
502
515
expect (submitButtons).toHaveLength (3 ); // expect 3 elements
503
516
```
504
517
0 commit comments