@testing-library/react-native 14.0.0-beta.1 → 14.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -6
- package/build/helpers/accessibility.js +24 -7
- package/build/helpers/accessibility.js.map +1 -1
- package/docs/README.md +31 -0
- package/docs/agents/architecture.md +21 -0
- package/docs/agents/build-and-validation.md +27 -0
- package/docs/agents/code-style.md +12 -0
- package/docs/agents/example-apps.md +56 -0
- package/docs/agents/git-workflow.md +13 -0
- package/docs/agents/testing.md +20 -0
- package/docs/api/accessibility.md +26 -0
- package/docs/api/async-utilities.md +137 -0
- package/docs/api/configuration.md +61 -0
- package/docs/api/fire-event.md +165 -0
- package/docs/api/jest-matchers.md +198 -0
- package/docs/api/other-helpers.md +94 -0
- package/docs/api/overview.md +18 -0
- package/docs/api/queries.md +500 -0
- package/docs/api/render-hook.md +176 -0
- package/docs/api/render.md +49 -0
- package/docs/api/screen.md +188 -0
- package/docs/api/user-event.md +295 -0
- package/docs/cookbook/async-events.md +147 -0
- package/docs/cookbook/custom-render.md +83 -0
- package/docs/cookbook/network-requests.md +375 -0
- package/docs/guides/common-mistakes.md +587 -0
- package/docs/guides/how-to-query.md +125 -0
- package/docs/guides/llm-guidelines.md +228 -0
- package/docs/guides/migration-v14.md +553 -0
- package/docs/guides/quick-start.md +77 -0
- package/docs/guides/testing-environment.md +123 -0
- package/docs/guides/troubleshooting.md +61 -0
- package/docs/guides/understanding-act.md +207 -0
- package/package.json +9 -8
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Fire Event API
|
|
2
|
+
|
|
3
|
+
## `fireEvent`
|
|
4
|
+
|
|
5
|
+
> [!NOTE]
|
|
6
|
+
> For common events like `press` or `type`, use the [User Event API](./user-event.md). It simulates events more realistically by emitting a sequence of events with proper event objects that mimic React Native runtime behavior.
|
|
7
|
+
>
|
|
8
|
+
> Use Fire Event for cases not supported by User Event and for triggering event handlers on composite components.
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
function fireEvent(instance: TestInstance, eventName: string, ...data: unknown[]): Promise<unknown>;
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
The `fireEvent` API triggers event handlers on both host and composite components. It traverses the component tree bottom-up from the passed element to find an enabled event handler named `onXxx` where `xxx` is the event name.
|
|
15
|
+
|
|
16
|
+
Unlike User Event, this API does not automatically pass event object to event handler, this is responsibility of the user to construct such object.
|
|
17
|
+
|
|
18
|
+
The base `fireEvent(instance, eventName, ...data)` API can pass multiple custom arguments to the handler. Convenience helpers such as `fireEvent.press` and `fireEvent.scroll` are different: they create a default event object and accept one optional object to merge into it.
|
|
19
|
+
|
|
20
|
+
This function uses async `act` internally to execute all pending React updates during event handling.
|
|
21
|
+
|
|
22
|
+
```jsx
|
|
23
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
24
|
+
|
|
25
|
+
test('fire changeText event', async () => {
|
|
26
|
+
const onEventMock = jest.fn();
|
|
27
|
+
await render(
|
|
28
|
+
// MyComponent renders TextInput which has a placeholder 'Enter details'
|
|
29
|
+
// and with `onChangeText` bound to handleChangeText
|
|
30
|
+
<MyComponent handleChangeText={onEventMock} />,
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
await fireEvent(screen.getByPlaceholderText('change'), 'onChangeText', 'ab');
|
|
34
|
+
expect(onEventMock).toHaveBeenCalledWith('ab');
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
> [!NOTE]
|
|
39
|
+
> `fireEvent` performs checks that should prevent events firing on disabled elements.
|
|
40
|
+
|
|
41
|
+
An example using `fireEvent` with native events that aren't already aliased by the `fireEvent` api.
|
|
42
|
+
|
|
43
|
+
```jsx
|
|
44
|
+
import { TextInput, View } from 'react-native';
|
|
45
|
+
import { fireEvent, render, screen } from '@testing-library/react-native';
|
|
46
|
+
|
|
47
|
+
const onBlurMock = jest.fn();
|
|
48
|
+
|
|
49
|
+
await render(
|
|
50
|
+
<View>
|
|
51
|
+
<TextInput placeholder="my placeholder" onBlur={onBlurMock} />
|
|
52
|
+
</View>,
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
// you can omit the `on` prefix
|
|
56
|
+
await fireEvent(screen.getByPlaceholderText('my placeholder'), 'blur');
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
FireEvent exposes convenience methods for common events like: `press`, `changeText`, `scroll`.
|
|
60
|
+
|
|
61
|
+
### `fireEvent.press`
|
|
62
|
+
|
|
63
|
+
> [!NOTE]
|
|
64
|
+
> Use the User Event [`press()`](./user-event.md#press) helper instead. It simulates press interactions more realistically, including pressable support.
|
|
65
|
+
|
|
66
|
+
```tsx
|
|
67
|
+
fireEvent.press: (
|
|
68
|
+
instance: TestInstance,
|
|
69
|
+
eventProps?: Record<string, unknown>,
|
|
70
|
+
) => Promise<void>
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Builds a press event object, merges `eventProps` into it, and invokes the `press` handler on the element or nearest eligible parent.
|
|
74
|
+
|
|
75
|
+
```jsx
|
|
76
|
+
import { View, Text, TouchableOpacity } from 'react-native';
|
|
77
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
78
|
+
|
|
79
|
+
const onPressMock = jest.fn();
|
|
80
|
+
const eventData = {
|
|
81
|
+
nativeEvent: {
|
|
82
|
+
pageX: 20,
|
|
83
|
+
pageY: 30,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
await render(
|
|
88
|
+
<View>
|
|
89
|
+
<TouchableOpacity onPress={onPressMock}>
|
|
90
|
+
<Text>Press me</Text>
|
|
91
|
+
</TouchableOpacity>
|
|
92
|
+
</View>,
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
await fireEvent.press(screen.getByText('Press me'), eventData);
|
|
96
|
+
expect(onPressMock).toHaveBeenCalledWith(expect.objectContaining(eventData));
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### `fireEvent.changeText`
|
|
100
|
+
|
|
101
|
+
> [!NOTE]
|
|
102
|
+
> Use the User Event [`type()`](./user-event.md#type) helper instead. It simulates text change interactions more realistically, including key-by-key typing, element focus, and other editing events.
|
|
103
|
+
|
|
104
|
+
```tsx
|
|
105
|
+
fireEvent.changeText: (
|
|
106
|
+
instance: TestInstance,
|
|
107
|
+
text: string,
|
|
108
|
+
) => Promise<void>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Invokes `changeText` event handler on the element or parent element in the tree.
|
|
112
|
+
|
|
113
|
+
```jsx
|
|
114
|
+
import { View, TextInput } from 'react-native';
|
|
115
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
116
|
+
|
|
117
|
+
const onChangeTextMock = jest.fn();
|
|
118
|
+
const CHANGE_TEXT = 'content';
|
|
119
|
+
|
|
120
|
+
await render(
|
|
121
|
+
<View>
|
|
122
|
+
<TextInput placeholder="Enter data" onChangeText={onChangeTextMock} />
|
|
123
|
+
</View>,
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
await fireEvent.changeText(screen.getByPlaceholderText('Enter data'), CHANGE_TEXT);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### `fireEvent.scroll`
|
|
130
|
+
|
|
131
|
+
> [!NOTE]
|
|
132
|
+
> Prefer [`user.scrollTo`](./user-event.md#scrollto) over `fireEvent.scroll` for `ScrollView`, `FlatList`, and `SectionList` components. User Event simulates events more realistically based on React Native runtime behavior.
|
|
133
|
+
|
|
134
|
+
```tsx
|
|
135
|
+
fireEvent.scroll: (
|
|
136
|
+
instance: TestInstance,
|
|
137
|
+
eventProps?: Record<string, unknown>,
|
|
138
|
+
) => Promise<void>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
Builds a scroll event object, merges `eventProps` into it, and invokes the `scroll` handler on the element or nearest eligible parent.
|
|
142
|
+
|
|
143
|
+
#### On a `ScrollView`
|
|
144
|
+
|
|
145
|
+
```jsx
|
|
146
|
+
import { ScrollView, Text } from 'react-native';
|
|
147
|
+
import { render, screen, fireEvent } from '@testing-library/react-native';
|
|
148
|
+
|
|
149
|
+
const onScrollMock = jest.fn();
|
|
150
|
+
const eventData = {
|
|
151
|
+
nativeEvent: {
|
|
152
|
+
contentOffset: {
|
|
153
|
+
y: 200,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
await render(
|
|
159
|
+
<ScrollView testID="scroll-view" onScroll={onScrollMock}>
|
|
160
|
+
<Text>Content</Text>
|
|
161
|
+
</ScrollView>,
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
await fireEvent.scroll(screen.getByTestId('scroll-view'), eventData);
|
|
165
|
+
```
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
# Jest matchers
|
|
2
|
+
|
|
3
|
+
This guide covers the built-in Jest matchers. These matchers make your tests easier to read and work better with accessibility features.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
No setup needed. Matchers are available when you import from `@testing-library/react-native`.
|
|
8
|
+
|
|
9
|
+
## Checking element existence
|
|
10
|
+
|
|
11
|
+
### `toBeOnTheScreen()`
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
expect(element).toBeOnTheScreen();
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Checks if an element is attached to the element tree. If you have a reference to an element and it gets unmounted during the test, this assertion will fail.
|
|
18
|
+
|
|
19
|
+
## Element Content
|
|
20
|
+
|
|
21
|
+
### `toHaveTextContent()`
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
expect(element).toHaveTextContent(
|
|
25
|
+
text: string | RegExp,
|
|
26
|
+
options?: {
|
|
27
|
+
exact?: boolean;
|
|
28
|
+
normalizer?: (text: string) => string;
|
|
29
|
+
},
|
|
30
|
+
)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Checks if an element has the specified text content. Accepts `string` or `RegExp`, with optional [text match options](./queries.md#text-match-options) like `exact` and `normalizer`.
|
|
34
|
+
|
|
35
|
+
### `toContainElement()`
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
expect(container).toContainElement(
|
|
39
|
+
instance: TestInstance | null,
|
|
40
|
+
)
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Checks if a container element contains another element.
|
|
44
|
+
|
|
45
|
+
### `toBeEmptyElement()`
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
expect(element).toBeEmptyElement();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Checks if an element has no child elements or text content.
|
|
52
|
+
|
|
53
|
+
## Checking element state
|
|
54
|
+
|
|
55
|
+
### `toHaveDisplayValue()`
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
expect(element).toHaveDisplayValue(
|
|
59
|
+
value: string | RegExp,
|
|
60
|
+
options?: {
|
|
61
|
+
exact?: boolean;
|
|
62
|
+
normalizer?: (text: string) => string;
|
|
63
|
+
},
|
|
64
|
+
)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Checks if a `TextInput` has the specified display value. Accepts `string` or `RegExp`, with optional [text match options](./queries.md#text-match-options) like `exact` and `normalizer`.
|
|
68
|
+
|
|
69
|
+
### `toHaveAccessibilityValue()`
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
expect(element).toHaveAccessibilityValue(
|
|
73
|
+
value: {
|
|
74
|
+
min?: number;
|
|
75
|
+
max?: number;
|
|
76
|
+
now?: number;
|
|
77
|
+
text?: string | RegExp;
|
|
78
|
+
},
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Checks if an element has the specified accessible value.
|
|
83
|
+
|
|
84
|
+
The matcher reads accessibility values from `aria-valuemin`, `aria-valuemax`, `aria-valuenow`, `aria-valuetext`, and `accessibilityValue` props. It only checks the values you specify, so the element can have other accessibility value entries and still match.
|
|
85
|
+
|
|
86
|
+
For the `text` entry, you can use a string or `RegExp`.
|
|
87
|
+
|
|
88
|
+
### `toBeEnabled()` / `toBeDisabled`
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
expect(element).toBeEnabled();
|
|
92
|
+
expect(element).toBeDisabled();
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Checks if an element is enabled or disabled from `aria-disabled` or `accessibilityState.disabled` props. An element is disabled if it or any ancestor is disabled.
|
|
96
|
+
|
|
97
|
+
> [!NOTE]
|
|
98
|
+
> These matchers are opposites. Both are available so you can avoid double negations like `expect(element).not.toBeDisabled()`.
|
|
99
|
+
|
|
100
|
+
### `toBeSelected()`
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
expect(element).toBeSelected();
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Checks if an element is selected from `aria-selected` or `accessibilityState.selected` props.
|
|
107
|
+
|
|
108
|
+
### `toBeChecked()` / `toBePartiallyChecked()`
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
expect(element).toBeChecked();
|
|
112
|
+
expect(element).toBePartiallyChecked();
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Checks if an element is checked or partially checked from `aria-checked` or `accessibilityState.checked` props.
|
|
116
|
+
|
|
117
|
+
> [!NOTE]
|
|
118
|
+
>
|
|
119
|
+
> - `toBeChecked()` only works on `Switch` host elements and elements with `checkbox`, `radio`, or `switch` role.
|
|
120
|
+
> - `toBePartiallyChecked()` only works on elements with `checkbox` role.
|
|
121
|
+
|
|
122
|
+
### `toBeExpanded()` / `toBeCollapsed()`
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
expect(element).toBeExpanded();
|
|
126
|
+
expect(element).toBeCollapsed();
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Checks if an element is expanded or collapsed from `aria-expanded` or `accessibilityState.expanded` props.
|
|
130
|
+
|
|
131
|
+
> [!NOTE]
|
|
132
|
+
> These matchers are opposites for expandable elements (those with explicit `aria-expanded` or `accessibilityState.expanded` props). For non-expandable elements, neither matcher will pass.
|
|
133
|
+
|
|
134
|
+
### `toBeBusy()`
|
|
135
|
+
|
|
136
|
+
```ts
|
|
137
|
+
expect(element).toBeBusy();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Checks if an element is busy from `aria-busy` or `accessibilityState.busy` props.
|
|
141
|
+
|
|
142
|
+
## Checking element style
|
|
143
|
+
|
|
144
|
+
### `toBeVisible()`
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
expect(element).toBeVisible();
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Checks if an element is visible.
|
|
151
|
+
|
|
152
|
+
An element is invisible if it or any ancestor has `display: none` or `opacity: 0` styles, or if it's hidden from accessibility.
|
|
153
|
+
|
|
154
|
+
### `toHaveStyle()`
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
expect(element).toHaveStyle(
|
|
158
|
+
style: StyleProp<Style>,
|
|
159
|
+
)
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
Checks if an element has specific styles.
|
|
163
|
+
|
|
164
|
+
## Other matchers
|
|
165
|
+
|
|
166
|
+
### `toHaveAccessibleName()`
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
expect(element).toHaveAccessibleName(
|
|
170
|
+
name?: string | RegExp,
|
|
171
|
+
options?: {
|
|
172
|
+
exact?: boolean;
|
|
173
|
+
normalizer?: (text: string) => string;
|
|
174
|
+
},
|
|
175
|
+
)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Checks if an element has the specified accessible name. Accepts `string` or `RegExp`, with optional [text match options](./queries.md#text-match-options) like `exact` and `normalizer`.
|
|
179
|
+
|
|
180
|
+
The accessible name comes from `aria-labelledby`, `accessibilityLabelledBy`, `aria-label`, and `accessibilityLabel` props. For `Image` elements, the `alt` prop is also used. If none are present, the element's text content is used.
|
|
181
|
+
|
|
182
|
+
When `accessibilityLabelledBy` references multiple elements with an array, their text content is joined with spaces in the referenced order and matched as a single accessible name. `aria-labelledby` follows React Native's single `nativeID` value behavior.
|
|
183
|
+
|
|
184
|
+
Without a `name` parameter (or with `undefined`), it only checks whether the element has any accessible name.
|
|
185
|
+
|
|
186
|
+
### `toHaveProp()`
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
expect(element).toHaveProp(
|
|
190
|
+
name: string,
|
|
191
|
+
value?: unknown,
|
|
192
|
+
)
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
Checks if an element has a prop. Without a `value` (or with `undefined`), it only checks if the prop exists. With a `value`, it checks if the prop's value matches.
|
|
196
|
+
|
|
197
|
+
> [!NOTE]
|
|
198
|
+
> Use this matcher as a last resort when other matchers don't fit your needs.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# Other helpers
|
|
2
|
+
|
|
3
|
+
## `within`
|
|
4
|
+
|
|
5
|
+
```jsx
|
|
6
|
+
function within(instance: TestInstance): Queries {}
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
`within` performs [queries](./queries.md) scoped to given element.
|
|
10
|
+
|
|
11
|
+
> [!NOTE]
|
|
12
|
+
> Please note that additional `render` specific operations like `rerender`, `unmount`, `debug`, `toJSON` are _not_ included.
|
|
13
|
+
|
|
14
|
+
```jsx
|
|
15
|
+
const detailsScreen = within(screen.getByHintText('Details Screen'));
|
|
16
|
+
expect(detailsScreen.getByText('Some Text')).toBeOnTheScreen();
|
|
17
|
+
expect(detailsScreen.getByDisplayValue('Some Value')).toBeOnTheScreen();
|
|
18
|
+
expect(detailsScreen.queryByLabelText('Some Label')).toBeOnTheScreen();
|
|
19
|
+
await expect(detailsScreen.findByHintText('Some Label')).resolves.toBeOnTheScreen();
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Use cases for scoped queries include:
|
|
23
|
+
|
|
24
|
+
- queries scoped to a single item inside a FlatList containing many items
|
|
25
|
+
- queries scoped to a single screen in tests involving screen transitions (e.g. with react-navigation)
|
|
26
|
+
|
|
27
|
+
## `act`
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
function act<T>(callback: () => T | Promise<T>): Promise<T>;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Wraps code that causes React state updates to ensure all updates are processed before assertions. By default any `render`, `rerender`, `fireEvent`, and `waitFor` calls are wrapped by this function, so there is no need to wrap it manually.
|
|
34
|
+
|
|
35
|
+
**In v14, `act` is now async by default and always returns a Promise**. This works with async React features like `Suspense` boundaries and the `use()` hook. All pending React updates are executed before the Promise resolves.
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { act } from '@testing-library/react-native';
|
|
39
|
+
|
|
40
|
+
it('should update state', async () => {
|
|
41
|
+
await act(() => {
|
|
42
|
+
setState('new value');
|
|
43
|
+
});
|
|
44
|
+
expect(state).toBe('new value');
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
**Note**: Even if your callback is synchronous, you should still use `await act(...)` as `act` now always returns a Promise.
|
|
49
|
+
|
|
50
|
+
Consult our [Understanding Act function](../guides/understanding-act.md) document for more understanding of its intricacies.
|
|
51
|
+
|
|
52
|
+
## `cleanup`
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
function cleanup(): Promise<void>;
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Unmounts React trees that were mounted with `render` and clears `screen` variable that holds latest `render` output.
|
|
59
|
+
|
|
60
|
+
> [!INFO]
|
|
61
|
+
> 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.
|
|
62
|
+
|
|
63
|
+
For example, if you're using the `jest` testing framework, then you would need to use the `afterEach` hook like so:
|
|
64
|
+
|
|
65
|
+
```jsx
|
|
66
|
+
import { cleanup, render } from '@testing-library/react-native/pure';
|
|
67
|
+
import { View } from 'react-native';
|
|
68
|
+
|
|
69
|
+
afterEach(async () => {
|
|
70
|
+
await cleanup();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('renders a view', async () => {
|
|
74
|
+
await render(<View />);
|
|
75
|
+
// ...
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
The `afterEach(cleanup)` call also works in `describe` blocks:
|
|
80
|
+
|
|
81
|
+
```jsx
|
|
82
|
+
describe('when logged in', () => {
|
|
83
|
+
afterEach(async () => {
|
|
84
|
+
await cleanup();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('renders the user', async () => {
|
|
88
|
+
await render(<SiteHeader />);
|
|
89
|
+
// ...
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
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).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# API Overview
|
|
2
|
+
|
|
3
|
+
React Native Testing Library consists of following APIs:
|
|
4
|
+
|
|
5
|
+
- [`render` function](./render.md) - render your UI components for testing purposes
|
|
6
|
+
- [`screen` object](./screen.md) - access rendered UI:
|
|
7
|
+
- [Queries](./queries.md) - find relevant components by various predicates: role, text, test ids, etc
|
|
8
|
+
- Lifecycle methods: [`rerender`](./screen.md#rerender), [`unmount`](./screen.md#unmount)
|
|
9
|
+
- Helpers: [`debug`](./screen.md#debug), [`toJSON`](./screen.md#tojson), [`root`](./screen.md#root)
|
|
10
|
+
- [Jest matchers](./jest-matchers.md) - validate assumptions about your UI
|
|
11
|
+
- [User Event](./user-event.md) - simulate common user interactions like [`press`](./user-event.md#press) or [`type`](./user-event.md#type) in a realistic way
|
|
12
|
+
- [Fire Event](./fire-event.md) - simulate any component event in a simplified way purposes
|
|
13
|
+
- Misc APIs:
|
|
14
|
+
- [`renderHook` function](./render-hook.md) - render hooks for testing
|
|
15
|
+
- [Async utils](./async-utilities.md): `findBy*` queries, `waitFor`, `waitForElementToBeRemoved`
|
|
16
|
+
- [Configuration](./configuration.md): `configure`, `resetToDefaults`
|
|
17
|
+
- [Accessibility](./accessibility.md): `isHiddenFromAccessibility`
|
|
18
|
+
- [Other](./other-helpers.md): `within`, `act`, `cleanup`
|