Skip to content

Commit 3ff0afb

Browse files
docs: setup automatic table of contents (#1482)
* docs: automate table of contents for all pages * docs: custom anchors * docs: tweaks
1 parent 5cbe69a commit 3ff0afb

15 files changed

+157
-229
lines changed

website/docs/API.md

Lines changed: 55 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,11 @@
22
id: api
33
title: API
44
---
5+
import TOCInline from '@theme/TOCInline';
56

6-
### Table of contents:
7-
8-
- [`render`](#render)
9-
- [`render` options](#render-options)
10-
- [`...queries`](#queries)
11-
- [`update`](#update)
12-
- [`unmount`](#unmount)
13-
- [`debug`](#debug)
14-
- [`toJSON`](#tojson)
15-
- [`root`](#root)
16-
- [`UNSAFE_root`](#unsaferoot)
17-
- [`screen`](#screen)
18-
- [`cleanup`](#cleanup)
19-
- [`fireEvent`](#fireevent)
20-
- [`fireEvent[eventName]`](#fireeventeventname)
21-
- [`fireEvent.press`](#fireeventpress)
22-
- [`fireEvent.changeText`](#fireeventchangetext)
23-
- [`fireEvent.scroll`](#fireeventscroll)
24-
- [`waitFor`](#waitfor)
25-
- [Using a React Native version \< 0.71 with Jest fake timers](#using-a-react-native-version--071-with-jest-fake-timers)
26-
- [`waitForElementToBeRemoved`](#waitforelementtoberemoved)
27-
- [`within`, `getQueriesForElement`](#within-getqueriesforelement)
28-
- [`queryBy*` APIs](#queryby-apis)
29-
- [`queryAll*` APIs](#queryall-apis)
30-
- [`act`](#act)
31-
- [`renderHook`](#renderhook)
32-
- [`callback`](#callback)
33-
- [`options` (Optional)](#options-optional)
34-
- [`RenderHookResult` object](#renderhookresult-object)
35-
- [Examples](#examples)
36-
- [Configuration](#configuration)
37-
- [`configure`](#configure)
38-
- [`resetToDefaults()`](#resettodefaults)
39-
- [Environment variables](#environment-variables)
40-
- [Accessibility](#accessibility)
41-
- [`isHiddenFromAccessibility`](#ishiddenfromaccessibility)
42-
43-
This page gathers public API of React Native Testing Library along with usage examples.
44-
45-
## `render`
7+
<TOCInline toc={toc} />
8+
9+
## `render` API
4610

4711
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/render.test.tsx)
4812

@@ -79,7 +43,7 @@ Latest `render` result is kept in [`screen`](#screen) variable that can be impor
7943
Using `screen` instead of destructuring `render` result is recommended approach. See [this article](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-screen) from Kent C. Dodds for more details.
8044
:::
8145

82-
### `render` options
46+
### Options {#render-options}
8347

8448
The behavior of `render` method can be customized by passing various options as a second argument of `RenderOptions` type:
8549

@@ -257,7 +221,7 @@ This API typically will return a composite view which goes against recommended t
257221
This API has been previously named `container` for compatibility with [React Testing Library](https://testing-library.com/docs/react-testing-library/api#container-1). However, despite the same name, the actual behavior has been signficantly different, hence the name change to `UNSAFE_root`.
258222
:::
259223

260-
## `screen`
224+
## `screen` API
261225

262226
```ts
263227
let screen: RenderResult;
@@ -274,48 +238,7 @@ This can also be used to build test utils that would normally require to be in r
274238
const debugText = () => screen.debug({ mapProps: (props) => ({}) });
275239
```
276240

277-
## `cleanup`
278-
279-
```ts
280-
const cleanup: () => void;
281-
```
282-
283-
Unmounts React trees that were mounted with `render` and clears `screen` variable that holds latest `render` output.
284-
285-
:::info
286-
Please note that this is done automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
287-
:::
288-
289-
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
290-
291-
```jsx
292-
import { cleanup, render } from '@testing-library/react-native/pure';
293-
import { View } from 'react-native';
294-
295-
afterEach(cleanup);
296-
297-
it('renders a view', () => {
298-
render(<View />);
299-
// ...
300-
});
301-
```
302-
303-
The `afterEach(cleanup)` call also works in `describe` blocks:
304-
305-
```jsx
306-
describe('when logged in', () => {
307-
afterEach(cleanup);
308-
309-
it('renders the user', () => {
310-
render(<SiteHeader />);
311-
// ...
312-
});
313-
});
314-
```
315-
316-
Failing to call `cleanup` when you've called `render` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
317-
318-
## `fireEvent`
241+
## `fireEvent` API
319242

320243
```ts
321244
function fireEvent(
@@ -374,13 +297,7 @@ render(
374297
fireEvent(screen.getByPlaceholderText('my placeholder'), 'blur');
375298
```
376299

377-
## `fireEvent[eventName]`
378-
379-
```ts
380-
fireEvent[eventName](element: ReactTestInstance, ...data: Array<any>): void
381-
```
382-
383-
Convenience methods for common events like: `press`, `changeText`, `scroll`.
300+
FireEvent exposes convenience methods for common events like: `press`, `changeText`, `scroll`.
384301

385302
### `fireEvent.press`
386303

@@ -520,7 +437,9 @@ expect(onEndReached).toHaveBeenCalled();
520437
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.
521438
:::
522439

523-
## `waitFor`
440+
## Helper functions
441+
442+
### `waitFor`
524443

525444
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/waitFor.test.tsx)
526445

@@ -573,7 +492,7 @@ Avoiding side effects in `expectation` callback can be partially enforced with t
573492

574493
It is also recommended to have a [single assertion per each `waitFor`](https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#having-multiple-assertions-in-a-single-waitfor-callback) for more consistency and faster failing tests. If you want to make several assertions, then they should be in seperate `waitFor` calls. In many cases you won't actually need to wrap the second assertion in `waitFor` since the first one will do the waiting required for asynchronous change to happen.
575494

576-
### Using a React Native version < 0.71 with Jest fake timers
495+
#### Using a React Native version < 0.71 with Jest fake timers
577496

578497
:::caution
579498
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).
@@ -624,7 +543,7 @@ In order to properly use `waitFor` you need at least React >=16.9.0 (featuring a
624543
If you receive warnings related to `act()` function consult our [Undestanding Act](./UnderstandingAct.md) function document.
625544
:::
626545

627-
## `waitForElementToBeRemoved`
546+
### `waitForElementToBeRemoved`
628547

629548
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/waitForElementToBeRemoved.test.tsx)
630549

@@ -665,7 +584,7 @@ In order to properly use `waitForElementToBeRemoved` you need at least React >=1
665584
If you receive warnings related to `act()` function consult our [Undestanding Act](./UnderstandingAct.md) function document.
666585
:::
667586

668-
## `within`, `getQueriesForElement`
587+
### `within`, `getQueriesForElement`
669588

670589
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/main/src/__tests__/within.test.tsx)
671590

@@ -696,37 +615,55 @@ Use cases for scoped queries include:
696615
- queries scoped to a single item inside a FlatList containing many items
697616
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
698617

699-
## `queryBy*` APIs
700618

701-
Each of the `getBy*` APIs listed in the render section above have a complimentary `queryBy*` API. The `getBy*` 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 `queryBy*` API instead:
619+
### `act`
702620

703-
```jsx
704-
import { render, screen } from '@testing-library/react-native';
621+
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]).
705622

706-
render(<Form />);
707-
const submitButton = screen.queryByText('submit');
708-
expect(submitButton).not.toBeOnTheScreen(); // it doesn't exist
623+
Consult our [Undestanding Act function](./UnderstandingAct.md) document for more understanding of its intricacies.
624+
625+
### `cleanup`
626+
627+
```ts
628+
const cleanup: () => void;
709629
```
710630

711-
## `queryAll*` APIs
631+
Unmounts React trees that were mounted with `render` and clears `screen` variable that holds latest `render` output.
632+
633+
:::info
634+
Please note that this is done automatically if the testing framework you're using supports the `afterEach` global (like mocha, Jest, and Jasmine). If not, you will need to do manual cleanups after each test.
635+
:::
712636

713-
Each of the query APIs have a corresponding `queryAll*` version that always returns an array of matching nodes. `getAll*` is the same but throws when the array has a length of 0.
637+
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
714638

715639
```jsx
716-
import { render } from '@testing-library/react-native';
640+
import { cleanup, render } from '@testing-library/react-native/pure';
641+
import { View } from 'react-native';
642+
643+
afterEach(cleanup);
717644

718-
render(<Forms />);
719-
const submitButtons = screen.queryAllByText('submit');
720-
expect(submitButtons).toHaveLength(3); // expect 3 elements
645+
it('renders a view', () => {
646+
render(<View />);
647+
// ...
648+
});
721649
```
722650

723-
## `act`
651+
The `afterEach(cleanup)` call also works in `describe` blocks:
724652

725-
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]).
653+
```jsx
654+
describe('when logged in', () => {
655+
afterEach(cleanup);
726656

727-
Consult our [Undestanding Act function](./UnderstandingAct.md) document for more understanding of its intricacies.
657+
it('renders the user', () => {
658+
render(<SiteHeader />);
659+
// ...
660+
});
661+
});
662+
```
663+
664+
Failing to call `cleanup` when you've called `render` could result in a memory leak and tests which are not "idempotent" (which can lead to difficult to debug errors in your tests).
728665

729-
## `renderHook`
666+
## `renderHook` API
730667

731668
Defined as:
732669

@@ -767,13 +704,11 @@ export const useCount = () => {
767704

768705
The `renderHook` function accepts the following arguments:
769706

770-
### `callback`
771-
772-
The function that is called each `render` of the test component. This function should call one or more hooks for testing.
707+
Callback is a function that is called each `render` of the test component. This function should call one or more hooks for testing.
773708

774709
The `props` passed into the callback will be the `initialProps` provided in the `options` to `renderHook`, unless new props are provided by a subsequent `rerender` call.
775710

776-
### `options` (Optional)
711+
### `options`
777712

778713
A `RenderHookOptions<Props>` object to modify the execution of the `callback` function, containing the following properties:
779714

@@ -785,7 +720,7 @@ The initial values to pass as `props` to the `callback` function of `renderHook`
785720

786721
A React component to wrap the test component in when rendering. This is usually used to add context providers from `React.createContext` for the hook to access with `useContext`.
787722

788-
### `RenderHookResult` object
723+
### `RenderHookResult`
789724

790725
```ts
791726
interface RenderHookResult<Result, Props> {
@@ -797,15 +732,15 @@ interface RenderHookResult<Result, Props> {
797732

798733
The `renderHook` function returns an object that has the following properties:
799734

800-
#### `result`
735+
### `result`
801736

802737
The `current` value of the `result` will reflect the latest of whatever is returned from the `callback` passed to `renderHook`. The `Result` type is determined by the type passed to or inferred by the `renderHook` call.
803738

804-
#### `rerender`
739+
### `rerender`
805740

806741
A function to rerender the test component, causing any hooks to be recalculated. If `newProps` are passed, they will replace the `callback` function's `initialProps` for subsequent rerenders. The `Props` type is determined by the type passed to or inferred by the `renderHook` call.
807742

808-
#### `unmount`
743+
### `unmount`
809744

810745
A function to unmount the test component. This is commonly used to trigger cleanup effects for `useEffect` hooks.
811746

website/docs/GettingStarted.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
id: getting-started
33
title: Getting Started
44
---
5-
65
## The problem
76

87
You want to write maintainable tests for your React Native components. As a part of this goal, you want your tests to avoid including implementation details of your components and rather focus on making your tests give you the confidence for which they are intended. As part of this, you want your testbase to be maintainable in the long run so refactors of your components (changes to implementation but not functionality) don't break your tests and slow you and your team down.

website/docs/MigrationV11.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
id: migration-v11
33
title: Migration to 11.0
44
---
5+
import TOCInline from '@theme/TOCInline';
56

67
Migration to React Native Testing Library version 11 from version 9.x or 10.x should be a relatively easy task due small amount of breaking changes.
78

9+
<TOCInline toc={toc} />
10+
811
# Breaking changes
912

10-
## Update to Jest 28 if you use fake timers
13+
### Update to Jest 28 if you use fake timers
1114

1215
If you use fake timers in any of your tests you should update your Jest dependencies to version 28. This is due to the fact that [`jest.useFakeTimers()` config structure](https://jestjs.io/docs/jest-object#jestusefaketimersfaketimersconfig) has changed.
1316

14-
## Refactor legacy `waitForOptions` position
17+
### Refactor legacy `waitForOptions` position
1518

1619
In version 9 we introducted query `options` parameters for each query type. This affected all `findBy` and `findAllBy` queries because their signatures changed e.g. from:
1720

@@ -41,13 +44,13 @@ should become
4144
findByText(/Text/, {}, { timeout: 1000 })
4245
```
4346

44-
## Triggering non-touch events on targets with `pointerEvents="box-none"` prop
47+
### Triggering non-touch events on targets with `pointerEvents="box-none"` prop
4548

4649
Up to version 10, RNTL disables all events for a target with `pointerEvents="box-none"`. This behavior is counter to how React Native itself functions.
4750

4851
From version 11, RNTL continues to disable `press` event for these targets but allows triggering other events, e.g. `layout`.
4952

50-
# All changes
53+
## All changes
5154

5255
* chore(breaking): update Jest to 28 by @mdjastrzebski in https://github.com/callstack/react-native-testing-library/pull/1008
5356
* refactor(breaking): remove legacy wait for options support by @mdjastrzebski in https://github.com/callstack/react-native-testing-library/pull/1018
@@ -59,6 +62,6 @@ From version 11, RNTL continues to disable `press` event for these targets but a
5962
* chore: Organise a11y queries by predicate by @MattAgn in https://github.com/callstack/react-native-testing-library/pull/977
6063
* chore: reenable skipped byText tests by @mdjastrzebski in https://github.com/callstack/react-native-testing-library/pull/1017
6164

62-
# Full Changelog
65+
## Full Changelog
6366
https://github.com/callstack/react-native-testing-library/compare/v10.1.1...v11.0.0
6467

0 commit comments

Comments
 (0)