Skip to content

Commit ac25e60

Browse files
committed
rest of the docs
1 parent 5a45abf commit ac25e60

File tree

15 files changed

+99
-116
lines changed

15 files changed

+99
-116
lines changed

README-v13.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

1515
## The problem
1616

17-
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 tests 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.
17+
You want to write maintainable tests for your React Native components. Your tests should avoid implementation details and focus on giving you confidence. They should remain maintainable so refactors (changes to implementation but not functionality) don't break your tests and slow you and your team down.
1818

1919
## This solution
2020

21-
The React Native Testing Library (RNTL) is a comprehensive solution for testing React Native components. It provides React Native runtime simulation on top of `react-test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
21+
The React Native Testing Library (RNTL) tests React Native components. It simulates the React Native runtime on top of `react-test-renderer` and encourages better testing practices. Its primary guiding principle is:
2222

2323
> The more your tests resemble the way your software is used, the more confidence they can give you.
2424
@@ -48,8 +48,8 @@ You can use the built-in Jest matchers automatically by having any import from `
4848
import { render, screen, userEvent } from '@testing-library/react-native';
4949
import { QuestionsBoard } from '../QuestionsBoard';
5050

51-
// It is recommended to use userEvent with fake timers
52-
// Some events involve duration so your tests may take a long time to run.
51+
// Use userEvent with fake timers
52+
// Some events involve duration, so tests may take a long time to run.
5353
jest.useFakeTimers();
5454

5555
test('form submits two answers', async () => {
@@ -87,8 +87,8 @@ React Native Testing Library consists of following APIs:
8787
- Lifecycle methods: [`rerender`](https://callstack.github.io/react-native-testing-library/docs/api/screen#rerender), [`unmount`](https://callstack.github.io/react-native-testing-library/docs/api/screen#unmount)
8888
- Helpers: [`debug`](https://callstack.github.io/react-native-testing-library/docs/api/screen#debug), [`toJSON`](https://callstack.github.io/react-native-testing-library/docs/api/screen#tojson), [`root`](https://callstack.github.io/react-native-testing-library/docs/api/screen#root)
8989
- [Jest matchers](https://callstack.github.io/react-native-testing-library/docs/api/jest-matchers) - validate assumptions about your UI
90-
- [User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type) in a realistic way
91-
- [Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event in a simplified way
90+
- [User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type)
91+
- [Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event
9292
- [`renderHook` function](https://callstack.github.io/react-native-testing-library/docs/api/misc/render-hook) - render hooks for testing purposes
9393
- Miscellaneous APIs:
9494
- [Async utils](https://callstack.github.io/react-native-testing-library/docs/api/misc/async): `findBy*` queries, `waitFor`, `waitForElementToBeRemoved`

README.md

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
2020
## The problem
2121

22-
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 tests 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.
22+
You want to write maintainable tests for your React Native components. Your tests should avoid implementation details and focus on giving you confidence. They should remain maintainable so refactors (changes to implementation but not functionality) don't break your tests and slow you and your team down.
2323

2424
## This solution
2525

26-
The React Native Testing Library (RNTL) is a comprehensive solution for testing React Native components. It provides React Native runtime simulation on top of `test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
26+
The React Native Testing Library (RNTL) tests React Native components. It simulates the React Native runtime on top of [Test Renderer](https://github.com/mdjastrzebski/test-renderer) and encourages better testing practices. Its primary guiding principle is:
2727

2828
> The more your tests resemble the way your software is used, the more confidence they can give you.
2929
@@ -34,20 +34,12 @@ This project is inspired by [React Testing Library](https://github.com/testing-l
3434
Open a Terminal in your project's folder and run:
3535

3636
```sh
37-
# Yarn install:
38-
yarn add --dev @testing-library/react-native@alpha
39-
40-
# NPM install
4137
npm install --save-dev @testing-library/react-native@alpha
4238
```
4339

4440
This library has a `peerDependencies` listing for [Test Renderer](https://github.com/mdjastrzebski/test-renderer). Make sure to install it as a dev dependency:
4541

4642
```sh
47-
# Yarn install:
48-
yarn add --dev test-renderer
49-
50-
# NPM install
5143
npm install --save-dev test-renderer
5244
```
5345

@@ -61,10 +53,6 @@ You can use the built-in Jest matchers automatically by having any import from `
6153
import { render, screen, userEvent } from '@testing-library/react-native';
6254
import { QuestionsBoard } from '../QuestionsBoard';
6355

64-
// It is recommended to use userEvent with fake timers
65-
// Some events involve duration so your tests may take a long time to run.
66-
jest.useFakeTimers();
67-
6856
test('form submits two answers', async () => {
6957
const questions = ['q1', 'q2'];
7058
const onSubmit = jest.fn();
@@ -100,8 +88,8 @@ React Native Testing Library consists of following APIs:
10088
- Lifecycle methods: [`rerender`](https://callstack.github.io/react-native-testing-library/docs/api/screen#rerender), [`unmount`](https://callstack.github.io/react-native-testing-library/docs/api/screen#unmount)
10189
- Helpers: [`debug`](https://callstack.github.io/react-native-testing-library/docs/api/screen#debug), [`toJSON`](https://callstack.github.io/react-native-testing-library/docs/api/screen#tojson), [`root`](https://callstack.github.io/react-native-testing-library/docs/api/screen#root), [`container`](https://callstack.github.io/react-native-testing-library/docs/api/screen#container)
10290
- [Jest matchers](https://callstack.github.io/react-native-testing-library/docs/api/jest-matchers) - validate assumptions about your UI
103-
- [User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type) in a realistic way
104-
- [Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event in a simplified way
91+
- [User Event](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event) - simulate common user interactions like [`press`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#press) or [`type`](https://callstack.github.io/react-native-testing-library/docs/api/events/user-event#type)
92+
- [Fire Event](https://callstack.github.io/react-native-testing-library/docs/api/events/fire-event) - simulate any component event
10593
- [`renderHook` function](https://callstack.github.io/react-native-testing-library/docs/api/misc/render-hook) - render hooks for testing purposes
10694
- Miscellaneous APIs:
10795
- [Async utils](https://callstack.github.io/react-native-testing-library/docs/api/misc/async): `findBy*` queries, `waitFor`, `waitForElementToBeRemoved`
@@ -112,8 +100,6 @@ React Native Testing Library consists of following APIs:
112100
## Migration Guides
113101

114102
- **[Migration to 14.0](https://callstack.github.io/react-native-testing-library/docs/migration/v14)** - Drops React 18, async APIs by default
115-
- [Migration to 13.0](https://callstack.github.io/react-native-testing-library/docs/migration/v13)
116-
- [Migration to built-in Jest Matchers](https://callstack.github.io/react-native-testing-library/docs/migration/jest-matchers)
117103

118104
## Troubleshooting
119105

website/docs/14.x/docs/advanced/testing-env.mdx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This document is intended for a more advanced audience who want to understand th
66

77
:::
88

9-
React Native Testing Library allows you to write integration and component tests for your React Native app or library. While the JSX code used in tests closely resembles your React Native app, things are not as simple as they might appear. This document will describe the key elements of our testing environment and highlight things to be aware of when writing more advanced tests or diagnosing issues.
9+
React Native Testing Library lets you write integration and component tests for your React Native app or library. While the JSX code in tests closely resembles your React Native app, the underlying environment differs. This document describes the key elements of our testing environment and highlights things to be aware of when writing advanced tests or diagnosing issues.
1010

1111
## React renderers
1212

@@ -20,21 +20,19 @@ When you run your tests in the React Native Testing Library, somewhat contrary t
2020

2121
## Test Renderer
2222

23-
Instead, RNTL uses [Test Renderer](https://github.com/mdjastrzebski/test-renderer), a modern, actively maintained renderer that allows rendering to pure JavaScript objects without access to mobile OS and can run in a Node.js environment using Jest (or any other JavaScript test runner). Test Renderer replaces the deprecated `react-test-renderer` package and provides better compatibility with React 19 and improved type safety.
23+
Instead, RNTL uses [Test Renderer](https://github.com/mdjastrzebski/test-renderer), a modern, actively maintained renderer that renders to pure JavaScript objects without access to mobile OS and runs in a Node.js environment using Jest (or any other JavaScript test runner). Test Renderer replaces the deprecated `react-test-renderer` package and has better compatibility with React 19 and improved type safety.
2424

25-
Using Test Renderer has pros and cons.
25+
Using Test Renderer has trade-offs:
2626

2727
Benefits:
28-
29-
- tests can run on most CIs (Linux, etc) and do not require a mobile device or emulator
30-
- faster test execution
31-
- light runtime environment
32-
33-
Disadvantages:
34-
35-
- Tests do not execute native code
36-
- Tests are unaware of the view state that would be managed by native components, e.g., focus, unmanaged text boxes, etc.
37-
- Assertions do not operate on native view hierarchy
28+
- Tests run on most CIs (Linux, etc) without a mobile device or emulator
29+
- Faster test execution
30+
- Light runtime environment
31+
32+
Limitations:
33+
- Tests don't execute native code
34+
- Tests are unaware of view state managed by native components, e.g., focus, unmanaged text boxes, etc.
35+
- Assertions don't operate on native view hierarchy
3836
- Runtime behaviors are simulated, sometimes imperfectly
3937

4038
It's worth noting that the React Testing Library (web one) works a bit differently. While RTL also runs in Jest, it has access to a simulated browser DOM environment from the `jsdom` package, which allows it to use a regular React DOM renderer. Unfortunately, there is no similar React Native runtime environment package. This is probably because while the browser environment is well-defined and highly standardized, the React Native environment constantly evolves in sync with the evolution of underlying OS-es. Maintaining such an environment would require duplicating countless React Native behaviors and keeping them in sync as React Native develops.
@@ -90,13 +88,13 @@ Not all React Native components are organized this way, e.g., when you use `Pres
9088

9189
### Host-only element tree
9290

93-
In RNTL v14, [Test Renderer](https://github.com/mdjastrzebski/test-renderer) only exposes host elements in the element tree. Composite components are not visible in the tree - you only see their host element output. This is an intentional design choice that aligns with Testing Library's philosophy: tests should focus on what users can see and interact with (host elements), not on implementation details (composite components).
91+
In RNTL v14, [Test Renderer](https://github.com/mdjastrzebski/test-renderer) only exposes host elements in the element tree. Composite components aren't visible in the treeyou only see their host element output. This aligns with Testing Library's philosophy: tests should focus on what users can see and interact with (host elements), not implementation details (composite components).
9492

9593
For a `HostElement`, the `type` prop is always a string value representing the host component name, e.g., `"View"`, `"Text"`, `"TextInput"`.
9694

9795
## Tree nodes
9896

99-
RNTL v14 queries and the element tree only expose host elements. This aligns with Testing Library's philosophy: tests should assert on what users can see and interact with. Host elements represent the actual UI controls that users interact with, while composite components exist purely in the JavaScript domain.
97+
RNTL v14 queries and the element tree only expose host elements. Tests assert on what users can see and interact with. Host elements represent the actual UI controls users interact with, while composite components exist purely in the JavaScript domain.
10098

10199
### Understanding props
102100

website/docs/14.x/docs/advanced/understanding-act.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Understanding `act` function
22

3-
When writing RNTL tests one of the things that confuses developers the most are cryptic [`act()`](https://react.dev/link/wrap-tests-with-act) function errors logged into console. In this article I will try to build an understanding of the purpose and behaviour of `act()` so you can build your tests with more confidence.
3+
When writing RNTL tests, cryptic [`act()`](https://react.dev/link/wrap-tests-with-act) function errors logged to console often confuse developers. This article explains the purpose and behavior of `act()` so you can write tests with more confidence.
44

55
## `act` warning
66

@@ -28,7 +28,7 @@ This function is intended only for using in automated tests and works only in de
2828

2929
The responsibility for `act` function is to make React renders and updates work in tests in a similar way they work in real application by grouping and executing related units of interaction (e.g. renders, effects, etc) together.
3030

31-
To showcase that behaviour let make a small experiment. First we define a function component that uses `useEffect` hook in a trivial way.
31+
Let's demonstrate this with a small experiment. First, define a function component that uses `useEffect`:
3232

3333
```jsx
3434
function TestComponent() {
@@ -57,7 +57,7 @@ test('render without act', () => {
5757
});
5858
```
5959

60-
When testing without `act` call wrapping rendering call, we see that the assertion runs just after the rendering but before `useEffect` hooks effects are applied. Which is not what we expected in our tests.
60+
When testing without `act` wrapping the render call, the assertion runs just after rendering but before `useEffect` effects are applied. This isn't what we expected.
6161

6262
```jsx
6363
import { createRoot } from 'test-renderer';
@@ -99,7 +99,7 @@ Note that `act` calls can be safely nested and internally form a stack of calls.
9999

100100
The `act` implementation is defined in the [ReactAct.js source file](https://github.com/facebook/react/blob/main/packages/react/src/ReactAct.js) inside React repository. RNTL v14 requires React 19+, which provides the `act` function directly via `React.act`.
101101

102-
RNTL exports `act` for convenience of the users as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). In v14, `act` is now async by default and always returns a Promise, making it compatible with async React features like `Suspense` boundary or `use()` hook. The underlying implementation wraps React's `act` function to ensure consistent async behavior.
102+
RNTL exports `act` for convenience as defined in the [act.ts source file](https://github.com/callstack/react-native-testing-library/blob/main/src/act.ts). In v14, `act` is async by default and always returns a Promise. This works with async React features like `Suspense` boundaries and the `use()` hook. The underlying implementation wraps React's `act` function to ensure consistent async behavior.
103103

104104
**Important**: You should always use `act` exported from `@testing-library/react-native` rather than the one from `react`. The RNTL version automatically ensures async behavior, whereas using `React.act` directly could still trigger synchronous act behavior if used improperly, leading to subtle test issues.
105105

@@ -111,7 +111,7 @@ In v14, `act` is always async and returns a Promise. While the callback you pass
111111

112112
When the callback passed to `act` contains asynchronous operations, the Promise returned by `act` will resolve only after those operations complete.
113113

114-
Lets look at a simple example with component using `setTimeout` call to simulate asynchronous behaviour:
114+
Here's a simple example with a component using `setTimeout` to simulate asynchronous behavior:
115115

116116
```jsx
117117
function TestAsyncComponent() {
@@ -139,7 +139,7 @@ If we test our component in a native way without handling its asynchronous behav
139139

140140
### Solution with fake timers
141141

142-
First solution is to use Jest's fake timers inside out tests:
142+
Use Jest's fake timers:
143143

144144
```jsx
145145
test('render with fake timers', async () => {
@@ -159,7 +159,7 @@ That way we can wrap `jest.runAllTimers()` call which triggers the `setTimeout`
159159

160160
### Solution with real timers
161161

162-
If we wanted to stick with real timers then things get a bit more complex. Let's start by applying a crude solution of opening async `act()` call for the expected duration of components updates:
162+
With real timers, things get more complex. Start with a simple solution: wrap an async `act()` call for the expected duration of component updates:
163163

164164
```jsx
165165
test('render with real timers - sleep', async () => {
@@ -174,7 +174,7 @@ test('render with real timers - sleep', async () => {
174174

175175
This works correctly as we use an explicit async `act()` call that resolves the console error. However, it relies on our knowledge of exact implementation details which is a bad practice.
176176

177-
Let's try more elegant solution using `waitFor` that will wait for our desired state:
177+
A better solution uses `waitFor` to wait for the desired state:
178178

179179
```jsx
180180
test('render with real timers - waitFor', async () => {

0 commit comments

Comments
 (0)