Skip to content

Commit e8a817c

Browse files
thymikeeEsemesek
authored andcommitted
docs: add and update queries documentation (#151)
### Summary Adds new "Queries" section inspired by https://testing-library.com/docs/dom-testing-library/api-queries ### Test plan Reading carefully
1 parent 34eb921 commit e8a817c

File tree

4 files changed

+162
-82
lines changed

4 files changed

+162
-82
lines changed

docs/API.md

Lines changed: 45 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -29,88 +29,36 @@ import { render } from 'react-native-testing-library';
2929
const { getByTestId, getByText /*...*/ } = render(<Component />);
3030
```
3131

32-
Returns a `RenderResult` object with following properties:
32+
Returns a `RenderResult` object with [Queries](./Queries.md) and following helpers:
3333

34-
### `getByTestId: (testID: string)`
34+
### `update`
3535

36-
A method returning a `ReactTestInstance` with matching `testID` prop. Throws when no matches.
37-
38-
_Note: most methods like this one return a [`ReactTestInstance`](https://reactjs.org/docs/test-renderer.html#testinstance) with following properties that you may be interested in:_
36+
_Also available under `rerender` alias_
3937

40-
```jsx
41-
type ReactTestInstance = {
42-
type: string | Function,
43-
props: { [propName: string]: any },
44-
parent: null | ReactTestInstance,
45-
children: Array<ReactTestInstance | string>,
46-
};
38+
```ts
39+
update(element: React.Element<any>): void
40+
rerender(element: React.Element<any>): void
4741
```
4842

49-
### `getByText: (text: string | RegExp)`
50-
51-
A method returning a `ReactTestInstance` with matching text – may be a string or regular expression. Throws when no matches.
52-
53-
This method will join `<Text>` siblings to find matches, similarly to [how React Native handles these components](https://facebook.github.io/react-native/docs/text#containers). This will allow for querying for strings that will be visually rendered together, but may be semantically separate React components.
54-
55-
### `getAllByText: (text: string | RegExp)`
56-
57-
A method returning an array of `ReactTestInstance`s with matching text – may be a string or regular expression.
58-
59-
### `getByPlaceholder: (placeholder: string | RegExp)`
60-
61-
A method returning a `ReactTestInstance` for a `TextInput` with a matching placeholder – may be a string or regular expression. Throws when no matches.
62-
63-
### `getAllByPlaceholder: (placeholder: string | RegExp)`
64-
65-
A method returning an array of `ReactTestInstance`s for `TextInput`'s with a matching placeholder – may be a string or regular expression.
66-
67-
### `getByProps: (props: { [propName: string]: any })`
68-
69-
A method returning a `ReactTestInstance` with matching props object. Throws when no matches.
70-
71-
### `getAllByProps: (props: { [propName: string]: any })`
72-
73-
A method returning an array of `ReactTestInstance`s with matching props object.
74-
75-
### `getByType: (type: React.ComponentType<*>)`
76-
77-
> Note: added in v1.4
78-
79-
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
80-
81-
### `getAllByType: (type: React.ComponentType<*>)`
82-
83-
> Note: added in v1.4
84-
85-
A method returning an array of `ReactTestInstance`s with matching a React component type.
86-
87-
### `[DEPRECATED] getByName: (name: React.ComponentType<*>)`
88-
89-
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
90-
91-
> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. It will be removed in next major release (v2.0). Use [`getByType`](#getbytype-type-reactcomponenttype) instead.
92-
93-
### `[DEPRECATED] getAllByName: (name: React.ComponentType<*>)`
94-
95-
A method returning an array of `ReactTestInstance`s with matching a React component type.
96-
97-
> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. It will be removed in next major release (v2.0). Use [`getAllByType`](#getallbytype-type-reactcomponenttype) instead.
98-
99-
### `update: (element: React.Element<any>) => void`
100-
101-
_Also available under `rerender` alias_
102-
10343
Re-render the in-memory tree with a new root element. This simulates a React update at the root. If the new element has the same type and key as the previous element, the tree will be updated; otherwise, it will re-mount a new tree. This is useful when testing for `componentDidUpdate` behavior, by passing updated props to the component.
10444

10545
[Example code](https://github.com/callstack/react-native-testing-library/blob/f96d782d26dd4815dbfd01de6ef7a647efd1f693/src/__tests__/act.test.js#L31-L37)
10646

107-
### `unmount: () => void`
47+
### `unmount`
48+
49+
```ts
50+
unmount(): void
51+
```
10852

10953
Unmount the in-memory tree, triggering the appropriate lifecycle events
11054

11155
When using React context providers, like Redux Provider, you'll likely want to wrap rendered component with them. In such cases it's convenient to create your custom `render` method. [Follow this great guide on how to set this up](https://github.com/kentcdodds/react-testing-library#custom-render).
11256

113-
### `debug: (message?: string) => void`
57+
### `debug`
58+
59+
```ts
60+
debug(message?: string): void
61+
```
11462

11563
Prints deeply rendered component passed to `render` with optional message on top. Uses [debug.deep](#debug) under the hood, but it's easier to use.
11664

@@ -133,11 +81,15 @@ optional message
13381
</TouchableOpacity>
13482
```
13583

136-
### `debug.shallow: (message?: string) => void`
84+
#### `debug.shallow`
13785

13886
Prints shallowly rendered component passed to `render` with optional message on top. Uses [debug.shallow](#debug) under the hood, but it's easier to use.
13987

140-
### `toJSON: () => ?ReactTestRendererJSON`
88+
### `toJSON`
89+
90+
```ts
91+
toJSON(): ReactTestRendererJSON | null
92+
```
14193

14294
Get the rendered component JSON representation, e.g. for snapshot testing.
14395

@@ -158,24 +110,27 @@ test('Component has a structure', () => {
158110

159111
## `fireEvent`
160112

161-
- [`Example code`](https://github.com/callstack/react-native-testing-library/blob/master/src/__tests__/fireEvent.test.js)
162-
163-
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree. The three most common events (`press`, `changeText`, and `scroll`) have been aliased for convenience.
113+
```ts
114+
fireEvent(element: ReactTestInstance, eventName: string, data?: *): void
115+
```
164116

165-
### `fireEvent: (element: ReactTestInstance, eventName: string, data?: *) => void`
117+
Fires native-like event with data.
166118

167-
Invokes named event handler on the element or parent element in the tree. For better readability, `fireEvent` strips the `on` part of the handler prop name, so it will fire `onMyCustomEvent` when `myCustomEvent` is passed as `eventName`.
119+
Invokes a given event handler (whether native or custom) on the element, bubbling to the root of the rendered tree.
168120

169121
```jsx
170122
import { render, fireEvent } from 'react-native-testing-library';
171-
import { MyComponent } from './MyComponent';
172123

173-
const onEventMock = jest.fn();
174-
const { getByTestId } = render(
175-
<MyComponent testID="custom" onMyCustomEvent={onEventMock} />
176-
);
124+
test('fire changeText event', () => {
125+
const onEventMock = jest.fn();
126+
const { getByTestId } = render(
127+
// MyComponent renders TextInput with `onChangeText` bound to handleChangeText
128+
<MyComponent testID="change" handleChangeText={onEventMock} />
129+
);
177130

178-
fireEvent(getByTestId('custom'), 'myCustomEvent');
131+
fireEvent(getByTestId('change'), 'onChangeText', 'ab');
132+
expect(onEventMock).toHaveBeenCalledWith('ab');
133+
});
179134
```
180135

181136
An example using `fireEvent` with native events that aren't already aliased by the `fireEvent` api.
@@ -192,9 +147,18 @@ const { getByPlaceholder } = render(
192147
</View>
193148
);
194149

150+
// you can omit the `on` prefix
195151
fireEvent(getByPlaceholder('my placeholder'), 'blur');
196152
```
197153

154+
## `fireEvent[eventName]`
155+
156+
```ts
157+
fireEvent[eventName](element: ReactTestInstance, data?: *): void
158+
```
159+
160+
Convenience methods for common events like: `press`, `changeText`, `scroll`.
161+
198162
### `fireEvent.press: (element: ReactTestInstance) => void`
199163

200164
Invokes `press` event handler on the element or parent element in the tree.

docs/Queries.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
---
2+
id: api-queries
3+
title: Queries
4+
---
5+
6+
## Variants
7+
8+
> `getBy` queries are shown by default in the [query documentation](#queries)
9+
> below.
10+
11+
### getBy
12+
13+
`getBy*` queries returns the first matching node for a query, and throws an
14+
error if no elements match.
15+
16+
### getAllBy
17+
18+
`getAllBy*` queries return an array of all matching nodes for a query, and
19+
throws an error if no elements match.
20+
21+
### queryBy
22+
23+
`queryBy*` queries returns the first matching node for a query, and return
24+
`null` if no elements match. This is useful for asserting an element is not
25+
present.
26+
27+
### queryAllBy
28+
29+
`queryAllBy*` queries return an array of all matching nodes for a query, and
30+
return an empty array (`[]`) if no elements match.
31+
32+
## Queries
33+
34+
_Note: most methods like this one return a [`ReactTestInstance`](https://reactjs.org/docs/test-renderer.html#testinstance) with following properties that you may be interested in:_
35+
36+
```jsx
37+
type ReactTestInstance = {
38+
type: string | Function,
39+
props: { [propName: string]: any },
40+
parent: null | ReactTestInstance,
41+
children: Array<ReactTestInstance | string>,
42+
};
43+
```
44+
45+
### `ByText`
46+
47+
> getByText, getAllByText, queryByText, queryAllByText
48+
49+
Returns a `ReactTestInstance` with matching text – may be a string or regular expression.
50+
51+
This method will join `<Text>` siblings to find matches, similarly to [how React Native handles these components](https://facebook.github.io/react-native/docs/text#containers). This will allow for querying for strings that will be visually rendered together, but may be semantically separate React components.
52+
53+
```jsx
54+
import { render } from 'react-native-testing-library';
55+
56+
const { getByText } = render(<MyComponent />);
57+
const element = getByText('banana');
58+
```
59+
60+
### `ByPlaceholder`
61+
62+
> getByPlaceholder, getAllByPlaceholder, queryByPlaceholder, queryAllByPlaceholder
63+
64+
Returns a `ReactTestInstance` for a `TextInput` with a matching placeholder – may be a string or regular expression.
65+
66+
```jsx
67+
import { render } from 'react-native-testing-library';
68+
69+
const { getByPlaceholder } = render(<MyComponent />);
70+
const element = getByPlaceholder('username');
71+
```
72+
73+
### `ByTestId`
74+
75+
> getByTestId, getAllByTestId, queryByTestId, queryAllByTestId
76+
77+
Returns a `ReactTestInstance` with matching `testID` prop.
78+
79+
```jsx
80+
import { render } from 'react-native-testing-library';
81+
82+
const { getByTestId } = render(<MyComponent />);
83+
const element = getByTestId('unique-id');
84+
```
85+
86+
## Unit testing helpers
87+
88+
> Use sparingly and responsibly, escape hatches here
89+
90+
`render` from `react-native-testing-library` exposes additional queries that **should not be used in component integration testing**, but some users (like component library creators) interested in unit testing some components may find helpful.
91+
92+
<details>
93+
<summary>Queries helpful in unit testing</summary>
94+
95+
The interface is the same as for other queries, but we won't provide full names so that they're harder to find by search engines.
96+
97+
### `ByType`
98+
99+
> Note: added in v1.4
100+
101+
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
102+
103+
### `ByProps`
104+
105+
A method returning a `ReactTestInstance` with matching props object
106+
107+
### `ByName`
108+
109+
> This method has been **deprecated** because using it results in fragile tests that may break between minor React Native versions. **DON'T USE IT**. It will be removed in next major release (v2.0). Use [`getByTestId`](#bytestid) instead. It's listed here only for back-compat purposes for early adopters of the library
110+
111+
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
112+
113+
</details>

website/i18n/en.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
},
1111
"getting-started": {
1212
"title": "Getting Started"
13+
},
14+
"api-queries": {
15+
"title": "Queries"
1316
}
1417
},
1518
"links": {

website/sidebars.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"docs": {
33
"Introduction": ["getting-started"],
4-
"API Reference": ["api"]
4+
"API Reference": ["api", "api-queries"]
55
}
66
}

0 commit comments

Comments
 (0)