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

Commit c36d3f9

Browse files
authored
Merge pull request #5 from testing-library/next
Revise in preparation for 3.0.0 release
2 parents 58588c1 + 88a4d88 commit c36d3f9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3834
-316
lines changed

docs/api-async.md

Lines changed: 41 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,60 @@ 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(() => getByLabelText('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+
getByLabelText('username'),
74+
getByLabelText('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+
await waitForElementToBeRemoved(() => queryAllByLabelText('list-item'));
105+
```
84106

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

docs/api-events.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ sidebar_label: Firing Events
77
## Basic example
88

99
```javascript
10-
import { fireEvent, NativeEvent, render } from 'native-testing-library';
10+
import { fireEvent, NativeTestEvent, render } from 'native-testing-library';
1111

1212
const { getByText } = render(<Button title="Submit" />);
13-
fireEvent(getByText(container, 'Submit'), new NativeEvent('press'));
13+
fireEvent(getByText(container, 'Submit'), new NativeTestEvent('press'));
1414
```
1515

1616
## `fireEvent[eventName]`
1717

1818
```typescript
19-
fireEvent[eventName](node: FiberRoot, eventProperties: NativeEvent)
19+
fireEvent[eventName](node: FiberRoot, eventProperties: NativeTestEvent)
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';
@@ -35,19 +35,22 @@ called nativeEvent. When you fire an event using this library, you will have to
3535
config. You will use this particularly for events like a change event:
3636

3737
```javascript
38-
fireEvent.change(getByA11yLabel(/username/i), { nativeEvent: { text: 'a' } });
38+
fireEvent.change(getByLabelText(/username/i), { nativeEvent: { text: 'a' } });
3939
```
4040

4141
**changeText**: `Text` has a method for value updating called `onChangeText`. Since this is such a
4242
commonly used method, there is a special case in the library for this method.
4343

4444
```javascript
45-
fireEvent.changeText(getByA11yLabel(/username/i), 'a');
45+
fireEvent.changeText(getByLabelText(/username/i), 'a');
4646
```
4747

4848
**customEvent**: You may be using a library that has custom event listeners that you want to be able
4949
to fire. This is how you would fire one of these events:
5050

5151
```javascript
52-
fireEvent(getByTestId('swiper'), new NativeEvent('myEvent', { nativeEvent: { value: 'testing' } }));
52+
fireEvent(
53+
getByTestId('swiper'),
54+
new NativeTestEvent('myEvent', { nativeEvent: { value: 'testing' } }),
55+
);
5356
```

docs/api-helpers.md

Lines changed: 22 additions & 37 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,61 +54,46 @@ 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
63-
const inputNode = <TextInput value="words" />;
64-
const textNode = (
63+
getNodeText(
6564
<Text>
6665
{`
67-
Hello
68-
World !
69-
`}
70-
</Text>
71-
);
72-
73-
getNodeText(node); // "Hello World !"
74-
```
75-
76-
This function is also used internally when querying nodes by their text content. This enables
77-
functions like `getByText` and `queryByText` to work as expected, finding elements in the tree
78-
similarly to how users would do.
79-
80-
The function works for for input elements as well:
81-
82-
```javascript
83-
const inputNode = <TextInput value="words" />;
84-
85-
getNodeText(inputNode); // "words"
66+
Hello
67+
World !
68+
`}
69+
</Text>,
70+
); // "Hello World !"
8671
```
8772

88-
## `getQueriesForElement` APIs
73+
## `within` and `getQueriesForElement` APIs
8974

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

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

9580
```js
96-
import { render, getQueriesForElement } from 'react-testing-library';
81+
import { render, within } from 'native-testing-library';
9782

98-
const { getByA11yLabel } = render(<LoginModal />);
99-
const loginForm = getByA11yLabel('login-form');
83+
const { getByLabelText } = render(<LoginModal />);
84+
const loginForm = getByLabelText('login-form');
10085

101-
getQueriesForElement(loginForm).getByPlaceholder('Username');
86+
within(loginForm).getByPlaceholderText('Username');
10287
```
10388

10489
## Debugging
10590

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:
91+
When you use any `get` calls in your test cases, the current contents of the `baseElement` get
92+
printed on the console. For example:
10893

10994
```javascript
11095
// <Text>Hello world</Text>
111-
getByText(container, 'Goodbye world'); // will fail by throwing error
96+
getByText('Goodbye world'); // will fail by throwing error
11297
```
11398

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

0 commit comments

Comments
 (0)