You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-334Lines changed: 7 additions & 334 deletions
Original file line number
Diff line number
Diff line change
@@ -64,342 +64,15 @@ This library has a peerDependencies listing for `react-test-renderer` and, of co
64
64
65
65
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.
66
66
67
-
## Usage
67
+
## API / Usage
68
68
69
-
## `render`
69
+
The [public API](docs/api.md) of `react-native-testing-library` is focused around these essential methods:
70
70
71
-
Defined as:
72
-
73
-
```jsx
74
-
functionrender(
75
-
component:React.Element<*>,
76
-
options?: {
77
-
/* You won't often use this, but it's helpful when testing refs */
Returns a `RenderResult` object with following properties:
92
-
93
-
### `getByTestId: (testID: string)`
94
-
95
-
A method returning a `ReactTestInstance` with matching `testID` prop. Throws when no matches.
96
-
97
-
_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:_
98
-
99
-
```jsx
100
-
type ReactTestInstance = {
101
-
type: string |Function,
102
-
props: { [propName: string]: any },
103
-
parent:null| ReactTestInstance,
104
-
children:Array<ReactTestInstance | string>,
105
-
};
106
-
```
107
-
108
-
### `getByText: (text: string | RegExp)`
109
-
110
-
A method returning a `ReactTestInstance` with matching text – may be a string or regular expression. Throws when no matches.
111
-
112
-
### `getAllByText: (text: string | RegExp)`
113
-
114
-
A method returning an array of `ReactTestInstance`s with matching text – may be a string or regular expression.
115
-
116
-
### `getByProps: (props: { [propName: string]: any })`
117
-
118
-
A method returning a `ReactTestInstance` with matching props object. Throws when no matches.
119
-
120
-
### `getAllByProps: (props: { [propName: string]: any })`
121
-
122
-
A method returning an array of `ReactTestInstance`s with matching props object.
123
-
124
-
### `getByType: (type: React.ComponentType<*>)`
125
-
126
-
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
A method returning a `ReactTestInstance` with matching a React component type. Throws when no matches.
135
-
136
-
> 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.
A method returning an array of `ReactTestInstance`s with matching a React component type.
141
-
142
-
> 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.
143
-
144
-
### `update: (element: React.Element<*>) => void`
145
-
146
-
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.
147
-
148
-
### `unmount: () => void`
149
-
150
-
Unmount the in-memory tree, triggering the appropriate lifecycle events
151
-
152
-
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).
153
-
154
-
### `debug: (message?: string) => void`
155
-
156
-
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.
157
-
158
-
```jsx
159
-
const { debug } =render(<Component />);
160
-
161
-
debug('optional message');
162
-
```
163
-
164
-
logs optional message and colored JSX:
165
-
166
-
```jsx
167
-
optional message
168
-
169
-
<TouchableOpacity
170
-
activeOpacity={0.2}
171
-
onPress={[Function bound fn]}
172
-
>
173
-
<Text>Press me</Text>
174
-
</TouchableOpacity>
175
-
```
176
-
177
-
### `debug.shallow: (message?: string) => void`
178
-
179
-
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.
180
-
181
-
### `toJSON: () => ?ReactTestRendererJSON`
182
-
183
-
Get the rendered component JSON representation, e.g. for snapshot testing.
184
-
185
-
## `shallow`
186
-
187
-
Shallowly render given React component. Since it doesn't return helpers to query the output, it's mostly advised to used for snapshot testing (short snapshots are best for code reviewers).
Wait for non-deterministic periods of time until your element appears or times out. `waitForExpect` periodically calls `expectation` every `interval` milliseconds to determine whether the element appeared or not.
If you're using Jest's [Timer Mocks](https://jestjs.io/docs/en/timer-mocks#docsNav), remember not to use `async/await` syntax as it will stall your tests.
309
-
310
-
## `debug`
311
-
312
-
Log prettified shallowly rendered component or test instance (just like snapshot) to stdout.
Each of the get APIs listed in the render section above have a complimentary query API. The get APIs will throw errors if a proper node cannot be found. This is normally the desired effect. However, if you want to make an assertion that an element is not present in the hierarchy, then you can use the query API instead:
expect(submitButton).toBeNull(); // it doesn't exist
390
-
```
391
-
392
-
## `queryAll` APIs
393
-
394
-
Each of the query APIs have a corresponding queryAll version that always returns an Array of matching nodes. getAll is the same but throws when the array has a length of 0.
0 commit comments