Skip to content

Commit 7899412

Browse files
docs: updates to docs (#1547)
* docs: update TestingEnv doc * docs: update Contributing.md * docs: update UserEvent.md * docs: remove tocs * docs: remove no-longer working FlatList examples for `fireEvent.scroll()`
1 parent 97ebb99 commit 7899412

File tree

4 files changed

+82
-137
lines changed

4 files changed

+82
-137
lines changed

CONTRIBUTING.md

+1-8
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,8 @@ When you're sending a pull request:
4949

5050
### Publishing a release
5151

52-
If you have publish access to the NPM package, perform following steps:
53-
54-
1. Update package version to `X.Y.Z` in `package.json` file.
55-
2. Commit changed file as `vX.Y.Z`.
56-
3. Tag branch with `vX.Y.Z` tag.
57-
4. Run the following from the main branch to publish a new release:
58-
5952
```sh
60-
yarn npm publish --access public
53+
yarn publish
6154
```
6255

6356
5. Push changes & tag to `origin`.

website/docs/API.md

+15-55
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
id: api
33
title: API
44
---
5+
56
import TOCInline from '@theme/TOCInline';
67

78
<TOCInline toc={toc} />
@@ -249,7 +250,7 @@ function fireEvent(
249250
```
250251

251252
:::note
252-
For common events like `press` or `type` it's recommended to use [User Event API](UserEvent.md) as it offers
253+
For common events like `press` or `type` it's recommended to use [User Event API](UserEvent.md) as it offers
253254
more realistic event simulation by emitting a sequence of events with proper event objects that mimic React Native runtime behavior.
254255

255256
Use Fire Event for cases not supported by User Event and for triggering event handlers on composite components.
@@ -395,48 +396,6 @@ render(
395396
fireEvent.scroll(screen.getByText('scroll-view'), eventData);
396397
```
397398

398-
#### On a `FlatList`
399-
400-
```jsx
401-
import { FlatList, View } from 'react-native';
402-
import { render, screen, fireEvent } from '@testing-library/react-native';
403-
404-
const onEndReached = jest.fn();
405-
render(
406-
<FlatList
407-
data={Array.from({ length: 10 }, (_, key) => ({ key: `${key}` }))}
408-
renderItem={() => <View style={{ height: 500, width: 100 }} />}
409-
onEndReached={onEndReached}
410-
onEndReachedThreshold={0.2}
411-
testID="flat-list"
412-
/>
413-
);
414-
const eventData = {
415-
nativeEvent: {
416-
contentOffset: {
417-
y: 500,
418-
},
419-
contentSize: {
420-
// Dimensions of the scrollable content
421-
height: 500,
422-
width: 100,
423-
},
424-
layoutMeasurement: {
425-
// Dimensions of the device
426-
height: 100,
427-
width: 100,
428-
},
429-
},
430-
};
431-
432-
fireEvent.scroll(screen.getByTestId('flat-list'), eventData);
433-
expect(onEndReached).toHaveBeenCalled();
434-
```
435-
436-
:::note
437-
If you're noticing that components are not being found on a list, even after mocking a scroll event, try changing the [`initialNumToRender`](https://reactnative.dev/docs/flatlist#initialnumtorender) that you have set. If you aren't comfortable changing the code to accept this prop from the unit test, try using an e2e test that might better suit what use case you're attempting to replicate.
438-
:::
439-
440399
## Helper functions
441400

442401
### `waitFor`
@@ -469,7 +428,7 @@ await waitFor(() => false);
469428

470429
```jsx
471430
// ❌ missing `await`: `waitFor` will just return Promise that will be rejected when the timeout is reached
472-
waitFor(() => expect(1).toBe(2))
431+
waitFor(() => expect(1).toBe(2));
473432
```
474433

475434
:::note
@@ -481,9 +440,9 @@ Since `waitFor` is likely to run `expectation` callback multiple times, it is hi
481440
```jsx
482441
await waitFor(() => {
483442
// ❌ button will be pressed on each waitFor iteration
484-
fireEvent.press(screen.getByText('press me'))
485-
expect(mockOnPress).toHaveBeenCalled()
486-
})
443+
fireEvent.press(screen.getByText('press me'));
444+
expect(mockOnPress).toHaveBeenCalled();
445+
});
487446
```
488447

489448
:::note
@@ -495,7 +454,7 @@ It is also recommended to have a [single assertion per each `waitFor`](https://k
495454
#### Using a React Native version < 0.71 with Jest fake timers
496455

497456
:::caution
498-
When using a version of React Native < 0.71 and modern fake timers (the default for `Jest` >= 27), `waitFor` won't work (it will always timeout even if `expectation()` doesn't throw) unless you use the custom [@testing-library/react-native preset](https://github.com/callstack/react-native-testing-library#custom-jest-preset).
457+
When using a version of React Native < 0.71 and modern fake timers (the default for `Jest` >= 27), `waitFor` won't work (it will always timeout even if `expectation()` doesn't throw) unless you use the custom [@testing-library/react-native preset](https://github.com/callstack/react-native-testing-library#custom-jest-preset).
499458
:::
500459

501460
`waitFor` checks whether Jest fake timers are enabled and adapts its behavior in such case. The following snippet is a simplified version of how it behaves when fake timers are enabled:
@@ -504,7 +463,7 @@ When using a version of React Native < 0.71 and modern fake timers (the default
504463
let fakeTimeRemaining = timeout;
505464
let lastError;
506465

507-
while(fakeTimeRemaining > 0) {
466+
while (fakeTimeRemaining > 0) {
508467
fakeTimeRemaining = fakeTimeRemaining - interval;
509468
jest.advanceTimersByTime(interval);
510469
try {
@@ -516,23 +475,23 @@ while(fakeTimeRemaining > 0) {
516475
}
517476

518477
// reject
519-
throw lastError
478+
throw lastError;
520479
```
521480

522-
In the following example we test that a function is called after 10 seconds using fake timers. Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we don't have to advance fake timers through Jest fake timers API because `waitFor` already does this for us.
481+
In the following example we test that a function is called after 10 seconds using fake timers. Since we're using fake timers, the test won't depend on real time passing and thus be much faster and more reliable. Also we don't have to advance fake timers through Jest fake timers API because `waitFor` already does this for us.
523482

524483
```tsx
525484
// in component
526485
setTimeout(() => {
527486
someFunction();
528-
}, 10000)
487+
}, 10000);
529488

530489
// in test
531490
jest.useFakeTimers();
532491

533492
await waitFor(() => {
534493
expect(someFunction).toHaveBeenCalledWith();
535-
}, 10000)
494+
}, 10000);
536495
```
537496

538497
:::info
@@ -607,15 +566,16 @@ const detailsScreen = within(screen.getByA11yHint('Details Screen'));
607566
expect(detailsScreen.getByText('Some Text')).toBeOnTheScreen();
608567
expect(detailsScreen.getByDisplayValue('Some Value')).toBeOnTheScreen();
609568
expect(detailsScreen.queryByLabelText('Some Label')).toBeOnTheScreen();
610-
await expect(detailsScreen.findByA11yHint('Some Label')).resolves.toBeOnTheScreen();
569+
await expect(
570+
detailsScreen.findByA11yHint('Some Label')
571+
).resolves.toBeOnTheScreen();
611572
```
612573

613574
Use cases for scoped queries include:
614575

615576
- queries scoped to a single item inside a FlatList containing many items
616577
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
617578

618-
619579
### `act`
620580

621581
Useful function to help testing components that use hooks API. By default any `render`, `update`, `fireEvent`, and `waitFor` calls are wrapped by this function, so there is no need to wrap it manually. This method is re-exported from [`react-test-renderer`](https://github.com/facebook/react/blob/main/packages/react-test-renderer/src/ReactTestRenderer.js#L567]).

0 commit comments

Comments
 (0)