Skip to content

Commit aa411c0

Browse files
committed
docs: refresh readme and getting started guide
1 parent 7909558 commit aa411c0

File tree

2 files changed

+77
-76
lines changed

2 files changed

+77
-76
lines changed

README.md

+36-41
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,30 @@
11
<div align='center'>
2+
<h1>React Native Testing Library</h1>
23
<img
34
height="80"
45
width="80"
56
alt="owl"
67
src="https://raw.githubusercontent.com/callstack/react-native-testing-library/master/website/static/img/owl.png"
78
/>
8-
<h1>React Native Testing Library</h1>
9-
10-
<p>Lightweight React Native testing utilities helping you write better tests with less effort.</P>
9+
<p>Simple and complete React Native testing utilities that encourage good testing practices.</P>
1110
</div>
1211

1312
[![Version][version-badge]][package]
1413
[![PRs Welcome][prs-welcome-badge]][prs-welcome]
1514
[![Chat][chat-badge]][chat]
1615
[![Sponsored by Callstack][callstack-badge]][callstack]
1716

18-
_Appreciation notice: This project is heavily inspired by [react-testing-library](https://github.com/testing-library/react-testing-library). Go check it out and use it to test your web React apps._
19-
2017
## The problem
2118

22-
You want to write maintainable tests for your React Native components without testing implementation details, but then you're told to use Enzyme, which you learn has no React Native adapter, meaning only shallow rendering is supported. And you want to render deep! But deep rendering may otherwise require jsdom (React Native isn't the web!), while doing deep rendering with `react-test-renderer` is so painful.
23-
24-
You would also like to use the newest React features, but you need to wait for your testing library's abstractions to catch up and it takes a while.
25-
26-
You finally want to approach testing using only best practices, while Enzyme may encourage assertions on implementation details.
19+
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.
2720

2821
## This solution
2922

30-
The React Native Testing Library is a lightweight solution for testing your React Native components. It provides light utility functions on top of `react-test-renderer` letting you always be up to date with latest React features and write any component tests you like. But really not any, it prevents you from testing implementation details because we stand this is a very bad practice.
31-
32-
This library is a replacement for [Enzyme](http://airbnb.io/enzyme/). It is tested to work with Jest, but it should work with other test runners as well.
33-
34-
## Example
35-
36-
```jsx
37-
import { render, fireEvent } from '@testing-library/react-native';
38-
import { QuestionsBoard } from '../QuestionsBoard';
39-
40-
test('form submits two answers', () => {
41-
const allQuestions = ['q1', 'q2'];
42-
const mockFn = jest.fn();
43-
44-
const { getAllByA11yLabel, getByText } = render(
45-
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
46-
);
47-
48-
const answerInputs = getAllByA11yLabel('answer input');
23+
The React Native Testing Library (RNTL) is a lightweight solution for testing React Native components. It provides light utility functions on top of `react-test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
4924

50-
fireEvent.changeText(answerInputs[0], 'a1');
51-
fireEvent.changeText(answerInputs[1], 'a2');
52-
fireEvent.press(getByText('Submit'));
25+
> The more your tests resemble the way your software is used, the more confidence they can give you.
5326
54-
expect(mockFn).toBeCalledWith({
55-
'1': { q: 'q1', a: 'a1' },
56-
'2': { q: 'q2', a: 'a2' },
57-
});
58-
});
59-
```
60-
61-
You can find the source of `QuestionsBoard` component and this example [here](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/questionsBoard.test.js).
27+
This project is inspired by [React Testing Library](https://github.com/testing-library/react-testing-library). Tested to work with Jest, but it should work with other test runners as well.
6228

6329
## Installation
6430

@@ -78,6 +44,8 @@ npm install --save-dev @testing-library/react-native
7844

7945
This library has a peerDependencies listing for `react-test-renderer` and, of course, `react`. Make sure to install them too!
8046

47+
> In order to properly use helpers for async tests (`findBy` queries and `waitFor`) you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.60 (which comes with React >=16.9.0).
48+
8149
### Additional Jest matchers
8250

8351
In order to use addtional React Native-specific jest matchers from [@testing-library/jest-native](https://github.com/testing-library/jest-native) package add it to your project:
@@ -111,7 +79,34 @@ Note for [Flow](https://flow.org) users – you'll also need to install typings
11179
flow-typed install react-test-renderer
11280
```
11381

114-
As you may have noticed, it's not tied to React Native at all – you can safely use it in your React components if you feel like not interacting directly with DOM.
82+
## Example
83+
84+
```jsx
85+
import { render, fireEvent } from '@testing-library/react-native';
86+
import { QuestionsBoard } from '../QuestionsBoard';
87+
88+
test('form submits two answers', () => {
89+
const allQuestions = ['q1', 'q2'];
90+
const mockFn = jest.fn();
91+
92+
const { getAllByA11yLabel, getByText } = render(
93+
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
94+
);
95+
96+
const answerInputs = getAllByA11yLabel('answer input');
97+
98+
fireEvent.changeText(answerInputs[0], 'a1');
99+
fireEvent.changeText(answerInputs[1], 'a2');
100+
fireEvent.press(getByText('Submit'));
101+
102+
expect(mockFn).toBeCalledWith({
103+
'1': { q: 'q1', a: 'a1' },
104+
'2': { q: 'q2', a: 'a2' },
105+
});
106+
});
107+
```
108+
109+
You can find the source of `QuestionsBoard` component and this example [here](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/questionsBoard.test.js).
115110

116111
## API / Usage
117112

website/docs/GettingStarted.md

+41-35
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,15 @@ title: Getting Started
55

66
## The problem
77

8-
You want to write maintainable tests for your React Native components without testing implementation details, but then you're told to use Enzyme, which you learn has no React Native adapter, meaning only shallow rendering is supported. And you want to render deep! But deep rendering may otherwise require jsdom (React Native isn't the web!), while doing deep rendering with `react-test-renderer` is so painful.
9-
10-
You would also like to use the newest React features, but you need to wait for your testing library's abstractions to catch up and it takes a while.
11-
12-
You finally want to approach testing using only best practices, while Enzyme may encourage assertions on implementation details.
8+
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.
139

1410
## This solution
1511

16-
The React Native Testing Library is a lightweight solution for testing your React Native components. It provides light utility functions on top of `react-test-renderer` letting you always be up to date with latest React features and write any component tests you like. It also prevents you from testing implementation details because we believe this is a very bad practice.
17-
18-
This library is a replacement for [Enzyme](http://airbnb.io/enzyme/).
19-
20-
## Example
21-
22-
```jsx
23-
import { render, fireEvent } from '@testing-library/react-native';
24-
import { QuestionsBoard } from '../QuestionsBoard';
25-
26-
test('form submits two answers', () => {
27-
const allQuestions = ['q1', 'q2'];
28-
const mockFn = jest.fn();
29-
30-
const { getAllByA11yLabel, getByText } = render(
31-
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
32-
);
33-
34-
const answerInputs = getAllByA11yLabel('answer input');
12+
The React Native Testing Library (RNTL) is a lightweight solution for testing React Native components. It provides light utility functions on top of `react-test-renderer`, in a way that encourages better testing practices. Its primary guiding principle is:
3513

36-
fireEvent.changeText(answerInputs[0], 'a1');
37-
fireEvent.changeText(answerInputs[1], 'a2');
38-
fireEvent.press(getByText('Submit'));
14+
> The more your tests resemble the way your software is used, the more confidence they can give you.
3915
40-
expect(mockFn).toBeCalledWith({
41-
'1': { q: 'q1', a: 'a1' },
42-
'2': { q: 'q2', a: 'a2' },
43-
});
44-
});
45-
```
16+
This project is inspired by [React Testing Library](https://github.com/testing-library/react-testing-library). Tested to work with Jest, but it should work with other test runners as well.
4617

4718
You can find the source of `QuestionsBoard` component and this example [here](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/questionsBoard.test.js).
4819

@@ -64,8 +35,6 @@ npm install --save-dev @testing-library/react-native
6435

6536
This library has a peerDependencies listing for `react-test-renderer` and, of course, `react`. Make sure to install them too!
6637

67-
As you may have noticed, it's not tied to React Native at all – you can safely use it in your React components if you feel like not interacting directly with DOM.
68-
6938
:::info
7039
In order to properly use helpers for async tests (`findBy` queries and `waitFor`) you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.60 (which comes with React >=16.9.0).
7140
:::
@@ -94,3 +63,40 @@ Then automatically add it to your jest tests by using `setupFilesAfterEnv` optio
9463
"setupFilesAfterEnv": ["@testing-library/jest-native/extend-expect"]
9564
}
9665
```
66+
67+
### Flow
68+
69+
Note for [Flow](https://flow.org) users – you'll also need to install typings for `react-test-renderer`:
70+
71+
```sh
72+
flow-typed install react-test-renderer
73+
```
74+
75+
## Example
76+
77+
```jsx
78+
import { render, fireEvent } from '@testing-library/react-native';
79+
import { QuestionsBoard } from '../QuestionsBoard';
80+
81+
test('form submits two answers', () => {
82+
const allQuestions = ['q1', 'q2'];
83+
const mockFn = jest.fn();
84+
85+
const { getAllByA11yLabel, getByText } = render(
86+
<QuestionsBoard questions={allQuestions} onSubmit={mockFn} />
87+
);
88+
89+
const answerInputs = getAllByA11yLabel('answer input');
90+
91+
fireEvent.changeText(answerInputs[0], 'a1');
92+
fireEvent.changeText(answerInputs[1], 'a2');
93+
fireEvent.press(getByText('Submit'));
94+
95+
expect(mockFn).toBeCalledWith({
96+
'1': { q: 'q1', a: 'a1' },
97+
'2': { q: 'q2', a: 'a2' },
98+
});
99+
});
100+
```
101+
102+
You can find the source of `QuestionsBoard` component and this example [here](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/questionsBoard.test.js).

0 commit comments

Comments
 (0)