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

Commit 88a4d88

Browse files
committed
version 3.0.0
1 parent 0fd36ca commit 88a4d88

28 files changed

+745
-464
lines changed

docs/api-async.md

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,16 @@ Additionally, the result is returned for you to use.
6262
Here's a simple example:
6363

6464
```javascript
65-
const usernameElement = await waitForElement(() => getByA11yLabel('username'));
65+
const usernameElement = await waitForElement(() => getByLabelText('username'));
6666
expect(usernameElement).toHaveTextContent('chucknorris');
6767
```
6868

6969
You can also wait for multiple elements at once:
7070

7171
```javascript
7272
const [usernameElement, passwordElement] = await waitForElement(() => [
73-
getByA11yLabel('username'),
74-
getByA11yLabel('password'),
73+
getByLabelText('username'),
74+
getByLabelText('password'),
7575
]);
7676
```
7777

@@ -101,17 +101,7 @@ callback is returned as a Promise, but in most cases you won't need it.
101101
Here's a simple example:
102102

103103
```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-
]);
104+
await waitForElementToBeRemoved(() => queryAllByLabelText('list-item'));
115105
```
116106

117107
The default `timeout` is `4500ms` which will keep you under

docs/api-events.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ 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
@@ -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: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,45 +60,30 @@ in a browser, where any extra whitespace within words in the html code is not me
6060
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

8873
## `within` and `getQueriesForElement` APIs
8974

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.
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
9681
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-
within(loginForm).getByPlaceholder('Username');
86+
within(loginForm).getByPlaceholderText('Username');
10287
```
10388

10489
## Debugging

docs/api-queries.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ See [TextMatch](#textmatch) for documentation on what can be passed to a query.
6262
6363
```typescript
6464
getByHintText(
65-
container: ReactTestRenderer | NativeTestInstance,
65+
container: NativeTestInstance,
6666
match: TextMatch,
6767
options?: {
6868
exact?: boolean = true,
@@ -91,7 +91,7 @@ getByHintText('summary'); // returns the View node
9191
9292
```typescript
9393
getByLabelText(
94-
container: ReactTestRenderer | NativeTestInstance,
94+
container: NativeTestInstance,
9595
match: TextMatch,
9696
options?: {
9797
exact?: boolean = true,
@@ -130,7 +130,7 @@ getByLabelText('username'); // returns the TextInput node
130130
131131
```typescript
132132
getByRole(
133-
container: ReactTestRenderer | NativeTestInstance,
133+
container: NativeTestInstance,
134134
match: TextMatch,
135135
options?: {
136136
selector?: SelectorFn,
@@ -158,7 +158,7 @@ getByRole('summary'); // returns the View node
158158
159159
```typescript
160160
getByPlaceholderText(
161-
container: ReactTestRenderer | NativeTestInstance,
161+
container: NativeTestInstance,
162162
match: TextMatch,
163163
options?: {
164164
exact?: boolean = true,
@@ -186,7 +186,7 @@ getByPlaceholderText('Username'); // returns the TextInput node
186186
187187
```typescript
188188
getByText(
189-
container: ReactTestRenderer | NativeTestInstance,
189+
container: NativeTestInstance,
190190
match: TextMatch,
191191
options?: {
192192
exact?: boolean = true,
@@ -198,8 +198,7 @@ getByText(
198198
```
199199

200200
This will search for all elements of type `Text` with `props.children` matching the given. It will
201-
also search `TextInput` elements by their value and `Button` elements by their `title`
202-
[`TextMatch`](#textmatch).
201+
also search for `Button` elements by their `title` [`TextMatch`](#textmatch).
203202

204203
```js
205204
import { render } from 'native-testing-library';
@@ -215,7 +214,7 @@ getByText(/about/i); // returns the Text node
215214
216215
```typescript
217216
getByTitle(
218-
container: ReactTestRenderer | NativeTestInstance,
217+
container: NativeTestInstance,
219218
match: TextMatch,
220219
options?: {
221220
exact?: boolean = true,
@@ -237,13 +236,14 @@ const { getByTitle } = render(<Button title="About" />);
237236
getByTitle(/about/i); // returns the Button node
238237
```
239238

240-
### `ByValue`
239+
### `ByDisplayValue`
241240

242-
> getByValue, queryByValue, getAllByValue, queryAllByValue, findByValue, findAllByValue
241+
> getByDisplayValue, queryByDisplayValue, getAllByDisplayValue, queryAllByDisplayValue,
242+
> findByDisplayValue, findAllByDisplayValue
243243
244244
```typescript
245-
getByValue(
246-
container: ReactTestRenderer | NativeTestInstance,
245+
getByDisplayValue(
246+
container: NativeTestInstance,
247247
match: TextMatch,
248248
options?: {
249249
exact?: boolean = true,
@@ -254,15 +254,15 @@ getByValue(
254254
}): NormalizerOptions
255255
```
256256

257-
This will search for all `TextInput` elements with a `value` prop or `Picker` elements with a
258-
`selectedValue` prop and find ones that matches the given [`TextMatch`](#textmatch).
257+
This will search for all `TextInput` elements with a `value` prop and `Picker` or `Switch` elements
258+
with a `selectedValue` prop and find ones that matches the given [`TextMatch`](#textmatch).
259259

260260
```js
261261
import { render } from 'native-testing-library';
262262

263-
const { getByValue } = render(<Input value="About ℹ" onChangeText={() => ({})} />);
263+
const { getByDisplayValue } = render(<Input value="About ℹ" onChangeText={() => ({})} />);
264264

265-
getByValue(/about/i); // returns the Input node
265+
getByDisplayValue(/about/i); // returns the Input node
266266
```
267267

268268
### `ByTestId`
@@ -271,7 +271,7 @@ getByValue(/about/i); // returns the Input node
271271
272272
```typescript
273273
getByTestId(
274-
container: ReactTestRenderer | NativeTestInstance,
274+
container: NativeTestInstance,
275275
match: TextMatch,
276276
options?: {
277277
trim?: boolean = true,

docs/api-test-instance.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
---
2+
id: api-test-instance
3+
title: NativeTestInstance
4+
sidebar_label: NativeTestInstance
5+
---
6+
7+
`NativeTestInstance` is a proxied react-test-renderer TestInstance. Not all properties are
8+
available, so you should only rely on the ones listed in this documentation.
9+
10+
Properties are excluded or overridden in an effort to make it easier to follow the guiding
11+
principles of testing-library. If there are other assertions you'd like to make, that aren't
12+
supported by this library there should be no problem running other react-test-renderer tests in the
13+
same codebase.
14+
15+
> Before assuming you need access to something not exposed, try to ask yourself what you would do if
16+
> you were using dom testing library.
17+
>
18+
> We believe all of the utilities provided by this package allow you to write tests the way you
19+
> would using that library, so if you're struggling to write a test, try to take a step back and
20+
> evaluate if you're trying to test implementation details or making an assertion that doesn't map
21+
> to something your users could do.
22+
23+
## `getProp`
24+
25+
This is simply a helper that allows you to get the value of a native node prop. It is meant to
26+
mirror the `getAttribute` API in the DOM. You should rarely need to use this method in your tests.
27+
28+
```javascript
29+
const nativeNode = getByValue('hello world');
30+
const value = nativeNode.getProp('value');
31+
32+
expect(value).toBe('hello world');
33+
```
34+
35+
## `findAll`
36+
37+
This method will return you all children nodes that are valid native nodes. **This will not find
38+
custom components, it will only return core components defined in the preset.** It is meant to be
39+
similar to `document.querySelectorAll`.
40+
41+
```javascript
42+
const { container } = render(<MyComponent />);
43+
const textNodes = container.findAll(node => node.getProp('accessibilityLabel') === 'hello world');
44+
45+
expect(textNodes).toHaveLength(2);
46+
```
47+
48+
## `type`
49+
50+
This will be the type of the native node. For all queries you can make in this library, this value
51+
will be a string.
52+
53+
```javascript
54+
const { findByText } = render(<Text>hello world</Text>);
55+
56+
expect(findByText(/hello world/i).type).toBe('hello world');
57+
```
58+
59+
## `props`
60+
61+
The props of the element you've queried for. These are meant to be the equivalent of a DOM
62+
"attribute". You should rarely need to make assertions on these values, so try not to rely heavily
63+
on using them directly.
64+
65+
```javascript
66+
const { findByText } = render(<Text style={{}}>hello world</Text>);
67+
68+
console.log(findByText(/hello world/i).props);
69+
```
70+
71+
## `parentNode`
72+
73+
This will return only valid native node parents. This differs from the react-test-renderer's
74+
`parent` property in that way. You will not be able to get your implementation components by
75+
traversing the parents of your tree.
76+
77+
```javascript
78+
const { findByText } = render(
79+
<View>
80+
<CustomComponent>
81+
<Text>hello world</Text>
82+
</CustomComponent>
83+
</View>,
84+
);
85+
86+
expect(findByText(/hello world/i).parentNode.type).toBe('View'); // NOT CustomComponent
87+
```
88+
89+
## `children`
90+
91+
This will return only valid native node children. This differs from the react-test-renderer's
92+
children property in that way. You will not be able to get your implementation components by
93+
traversing the children of your tree.
94+
95+
```javascript
96+
const { findByTestID } = render(
97+
<View testID="my-wrapper">
98+
<CustomComponent>
99+
<Text>hello world</Text>
100+
</CustomComponent>
101+
</View>,
102+
);
103+
104+
expect(findByTestID('my-wrapper').children[0].type).toBe('Text'); // NOT CustomComponent
105+
```

0 commit comments

Comments
 (0)