Skip to content
This repository was archived by the owner on Aug 1, 2020. It is now read-only.

Commit e364ba5

Browse files
committed
Revise in preparation for 3.0.0 release
1 parent 58588c1 commit e364ba5

25 files changed

+222
-748
lines changed

docs/api-async.md

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
id: api-async
3-
title: Async
4-
sidebar_label: Async
3+
title: Async Utilities
4+
sidebar_label: Async Utilities
55
---
66

77
Several utilities are provided for dealing with asynchronous code. These can be useful to wait for
@@ -26,8 +26,8 @@ expectations to pass. The `wait` function is a small wrapper around the
2626
example:
2727

2828
```javascript
29-
await wait(() => getByLabelText(container, 'username'));
30-
getByLabelText(container, 'username').value = 'chucknorris';
29+
await wait(() => getByLabelText('username'));
30+
getByLabelText('username').value = 'chucknorris';
3131
```
3232

3333
This can be useful if you have a unit test that mocks API calls and you need to wait for your mock
@@ -49,38 +49,70 @@ the event loop (in a `setTimeout`) before starting the intervals.
4949
function waitForElement<T>(
5050
callback: () => T,
5151
options?: {
52-
container?: ReactTestInstance;
5352
timeout?: number;
5453
interval?: number;
5554
},
5655
): Promise<T>;
5756
```
5857

59-
When in need to wait for DOM elements to appear, disappear, or change you can use `waitForElement`.
60-
The `waitForElement` function is a similar to `wait`, but is a helper specifically intended to wait
61-
for an element.
58+
When you need to wait for elements to appear, you can use `waitForElement`. The `waitForElement`
59+
function is a similar to `wait`, but is specifically intended to wait for an element to appear.
60+
Additionally, the result is returned for you to use.
6261

6362
Here's a simple example:
6463

6564
```javascript
66-
const usernameElement = await waitForElement(() => getByA11yLabel(container, 'username'), {
67-
container,
68-
});
69-
expect(usernameElement.props.children).toBe('chucknorris');
65+
const usernameElement = await waitForElement(() => getByA11yLabel('username'));
66+
expect(usernameElement).toHaveTextContent('chucknorris');
7067
```
7168

7269
You can also wait for multiple elements at once:
7370

7471
```javascript
75-
const [usernameElement, passwordElement] = await waitForElement(
76-
() => [getByA11yLabel(container, 'username'), getByA11yLabel(container, 'password')],
77-
{ container },
78-
);
72+
const [usernameElement, passwordElement] = await waitForElement(() => [
73+
getByA11yLabel('username'),
74+
getByA11yLabel('password'),
75+
]);
7976
```
8077

81-
The default `container` is the root `ReactTestInstance` that is the result of `render`. Make sure
82-
the elements you wait for will be attached to it, or set a different `ReactTestInstance` as a
83-
container.
78+
The default `timeout` is `4500ms` which will keep you under
79+
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).
80+
81+
The default `interval` is `50ms`. However it will run your callback immediately on the next tick of
82+
the event loop (in a `setTimeout`) before starting the intervals.
83+
84+
## `waitForElementToBeRemoved`
85+
86+
```typescript
87+
function waitForElementToBeRemoved<T>(
88+
callback: () => T,
89+
options?: {
90+
timeout?: number;
91+
interval?: number;
92+
},
93+
): Promise<T>;
94+
```
95+
96+
When you need to wait for elements to be removed, or you can use `waitForElementToBeRemoved`. The
97+
`waitForElementToBeRemoved` function is a similar to `wait`, but is a helper specifically intended
98+
to wait for an element to be removed from the tree. Similarly to `waitForElement` the result of the
99+
callback is returned as a Promise, but in most cases you won't need it.
100+
101+
Here's a simple example:
102+
103+
```javascript
104+
const listItems = await waitForElementToBeRemoved(() => queryAllByA11yLabel('list-item'));
105+
expect(listItems).toHaveLength(0);
106+
```
107+
108+
You can also wait for multiple elements to be removed at once:
109+
110+
```javascript
111+
const [usernameElement, passwordElement] = await waitForElementToBeRemoved(() => [
112+
queryByA11yLabel('username'),
113+
queryByA11yLabel('password'),
114+
]);
115+
```
84116

85117
The default `timeout` is `4500ms` which will keep you under
86118
[Jest's default timeout of `5000ms`](https://facebook.github.io/jest/docs/en/jest-object.html#jestsettimeouttimeout).

docs/api-events.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ fireEvent[eventName](node: FiberRoot, eventProperties: NativeEvent)
2020
```
2121

2222
Convenience methods for firing events. Check out
23-
[src/events.js](https://github.com/testing-library/native-testing-library/blob/master/src/events.js) for
24-
a full list as well as `validTargets` for every event type.
23+
[src/events.js](https://github.com/testing-library/native-testing-library/blob/master/src/events.js)
24+
for a full list as well as `validTargets` for every event type.
2525

2626
```javascript
2727
import { fireEvent, render } from 'native-testing-library';

docs/api-helpers.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ sidebar_label: Helpers
66

77
## Custom Queries
88

9-
`native-testing-library` exposes many of the helper functions that are used to implement the default
9+
`native-testing-library` exposes some of the helper functions that are used to implement the default
1010
queries. You can use the helpers to build custom queries. For example, the code below shows a way to
1111
query your TestInstance by a `style` prop. Note: test files would need to now import `test-utils.js`
1212
instead of using `native-testing-library` directly. Also note, please never actually implement this
1313
helper, it's just an example of what's possible.
1414

15-
```js
15+
```javascript
1616
// test-utils.js
1717
import * as nativeTestingLib from 'native-testing-library';
1818

@@ -54,10 +54,10 @@ export {
5454
getNodeText(node: React.ReactElement<any>)
5555
```
5656

57-
Returns the complete text content of a html element, removing any extra whitespace, and joining
58-
children that are an array. The intention is to treat text in nodes exactly as how it is perceived
59-
by users in a browser, where any extra whitespace within words in the html code is not meaningful
60-
when the text is rendered, and all text appears as one cohesive string regardless of the code.
57+
Returns the complete text content of an element, removing any extra whitespace, and joining children
58+
that are an array. The intention is to treat text in nodes exactly as how it is perceived by users
59+
in a browser, where any extra whitespace within words in the html code is not meaningful when the
60+
text is rendered, and all text appears as one cohesive string regardless of the code.
6161

6262
```javascript
6363
const inputNode = <TextInput value="words" />;
@@ -85,30 +85,30 @@ const inputNode = <TextInput value="words" />;
8585
getNodeText(inputNode); // "words"
8686
```
8787

88-
## `getQueriesForElement` APIs
88+
## `within` and ``getQueriesForElement` APIs
8989

90-
`getQueriesForElement` takes a FiberRoot and binds it to the raw query functions, allowing them to
91-
be used without specifying a container.
90+
`within` (an alias to `getQueriesForElement`) takes a `ReactTestRendererInstance` and binds it to
91+
the raw query functions, allowing them to be used without manually specifying a container.
9292

9393
Example: To get the username input of a login form within a `<LoginModal />`, you could do:
9494

9595
```js
96-
import { render, getQueriesForElement } from 'react-testing-library';
96+
import { render, within } from 'native-testing-library';
9797

9898
const { getByA11yLabel } = render(<LoginModal />);
9999
const loginForm = getByA11yLabel('login-form');
100100

101-
getQueriesForElement(loginForm).getByPlaceholder('Username');
101+
within(loginForm).getByPlaceholder('Username');
102102
```
103103

104104
## Debugging
105105

106-
When you use any `get` calls in your test cases, the current state of the `container` (React tree)
107-
gets printed on the console. For example:
106+
When you use any `get` calls in your test cases, the current contents of the `baseElement` get
107+
printed on the console. For example:
108108

109109
```javascript
110110
// <Text>Hello world</Text>
111-
getByText(container, 'Goodbye world'); // will fail by throwing error
111+
getByText('Goodbye world'); // will fail by throwing error
112112
```
113113

114114
The above test case will fail, however it prints the state of your React tree being tested, so you

docs/api-queries.md

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,29 @@ title: Queries
44
sidebar_label: Queries
55
---
66

7+
## Using queries
8+
9+
All queries are exported directly from the entry point, but you likely won't need to use them that
10+
way in most cases. For instance, you can do the following:
11+
12+
```javascript
13+
import { getByText } from 'native-testing-library';
14+
15+
getByText(container, 'hello world');
16+
```
17+
18+
but you likely won't need to. Most queries you will run are on the result of render. You can use
19+
those in this way:
20+
21+
```javascript
22+
const { getByText } = render(<Text>hello world</Text>);
23+
24+
getByText('hello world');
25+
```
26+
27+
This page is written in the format of the first example because it is documentation for the query
28+
API, not documentation for the render result.
29+
730
## Variants
831

932
> `getBy` queries are shown by default in the [query documentation](#queries) below.
@@ -62,7 +85,7 @@ See [TextMatch](#textmatch) for documentation on what can be passed to a query.
6285
6386
```typescript
6487
getByA11yHint(
65-
container: ReactTestInstance,
88+
container: ReactTestRendererInstance,
6689
match: TextMatch,
6790
options?: {
6891
exact?: boolean = true,
@@ -91,7 +114,7 @@ getByA11yHint('summary'); // returns the View node
91114
92115
```typescript
93116
getByA11yLabel(
94-
container: ReactTestInstance,
117+
container: ReactTestRendererInstance,
95118
match: TextMatch,
96119
options?: {
97120
exact?: boolean = true,
@@ -131,7 +154,7 @@ getByA11yLabel('username'); // returns the TextInput node
131154
132155
```typescript
133156
getByA11yRole(
134-
container: ReactTestInstance,
157+
container: ReactTestRendererInstance,
135158
match: TextMatch,
136159
options?: {
137160
exact?: boolean = true,
@@ -160,7 +183,7 @@ getByA11yRole('summary'); // returns the View node
160183
161184
```typescript
162185
getByA11yStates(
163-
container: ReactTestInstance,
186+
container: ReactTestRendererInstance,
164187
match: Array<string>
165188
): FiberRoot
166189
```
@@ -183,7 +206,7 @@ getByA11yStates(['disabled']); // returns the View node
183206
184207
```typescript
185208
getByA11yTraits(
186-
container: ReactTestInstance,
209+
container: ReactTestRendererInstance,
187210
match: Array<string>,
188211
): FiberRoot
189212
```
@@ -206,7 +229,7 @@ getByA11yTraits(['button']); // returns the View node
206229
207230
```typescript
208231
getByPlaceholder(
209-
container: ReactTestInstance,
232+
container: ReactTestRendererInstance,
210233
match: TextMatch,
211234
options?: {
212235
exact?: boolean = true,
@@ -234,10 +257,9 @@ getByPlaceholder('Username'); // returns the TextInput node
234257
235258
```typescript
236259
getByText(
237-
container: ReactTestInstance,
260+
container: ReactTestRendererInstance,
238261
match: TextMatch,
239262
options?: {
240-
types?: array = ['Text', 'TextInput'],
241263
trim?: boolean = true,
242264
collapseWhitespace?: boolean = true,
243265
exact?: boolean = true,
@@ -256,13 +278,40 @@ const { getByText } = render(<Text>About ℹ</Text>);
256278
getByText(/about/i); // returns the Text node
257279
```
258280

281+
### `ByTitle`
282+
283+
> getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle, findAllByTitle
284+
285+
```typescript
286+
getByTitle(
287+
container: ReactTestRendererInstance,
288+
match: TextMatch,
289+
options?: {
290+
trim?: boolean = true,
291+
collapseWhitespace?: boolean = true,
292+
exact?: boolean = true,
293+
normalizer?: NormalizerFn,
294+
}): FiberRoot
295+
```
296+
297+
This will search for all elements with `props.title` matching the given by their value
298+
[`TextMatch`](#textmatch).
299+
300+
```js
301+
import { render } from 'native-testing-library';
302+
303+
const { getByTitle } = render(<Button title="About" />);
304+
305+
getByTitle(/about/i); // returns the Button node
306+
```
307+
259308
### `ByValue`
260309

261310
> getByValue, queryByValue, getAllByValue, queryAllByValue, findByValue, findAllByValue
262311
263312
```typescript
264313
getByValue(
265-
container: ReactTestInstance,
314+
container: ReactTestRendererInstance,
266315
match: TextMatch,
267316
options?: {
268317
exact?: boolean = true,
@@ -290,7 +339,7 @@ getByValue(/about/i); // returns the Input node
290339
291340
```typescript
292341
getByTestId(
293-
container: ReactTestInstance,
342+
container: ReactTestRendererInstance,
294343
match: TextMatch,
295344
options?: {
296345
trim?: boolean = true,

0 commit comments

Comments
 (0)