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: content/blog/2019-02-04-react-v16.8.0.md
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -48,6 +48,55 @@ Note that React Hooks don't cover *all* use cases for classes yet but they're [v
48
48
49
49
Even while Hooks were in alpha, the React community created many interesting [examples](https://codesandbox.io/react-hooks) and [recipes](https://usehooks.com) using Hooks for animations, forms, subscriptions, integrating with other libraries, and so on. We're excited about Hooks because they make code reuse easier, helping you write your components in a simpler way and make great user experiences. We can't wait to see what you'll create next!
50
50
51
+
## Testing Hooks
52
+
53
+
We have added a new API called `ReactTestUtils.act()` in this release. It ensures that the behavior in your tests matches what happens in the browser more closely. We recommend to wrap any code rendering and triggering updates to your components into `act()` calls. Testing libraries like [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) can also wrap their APIs with it.
54
+
55
+
For example, the counter example from [this page](/docs/hooks-effect.html) can be tested like this:
The calls to `act()` will also flush the effects inside of them.
95
+
96
+
If you need to test a custom Hook, you can do so by creating a component in your test, and using your Hook from it. Then you can test the component you wrote.
97
+
98
+
To reduce the boilerplate, we recommend using [`react-testing-library`](https://git.io/react-testing-library) which is designed to encourage writing tests that use your components as the end users do.
99
+
51
100
## Thanks
52
101
53
102
We'd like to thank everybody who commented on the [Hooks RFC](https://github.com/reactjs/rfcs/pull/68) for sharing their feedback. We've read all of your comments and made some adjustments to the final API based on them.
@@ -19,12 +19,11 @@ var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
19
19
20
20
> Note:
21
21
>
22
-
> Airbnb has released a testing utility called Enzyme, which makes it easy to assert, manipulate, and traverse your React Components' output. If you're deciding on a unit testing utility to use together with Jest, or any other test runner, it's worth checking out: [http://airbnb.io/enzyme/](http://airbnb.io/enzyme/)
22
+
> We recommend using [`react-testing-library`](https://git.io/react-testing-library)which is designed to enable and encourage writing tests that use your components as the end users do.
23
23
>
24
-
> Alternatively, there is another testing utility called react-testing-library designed to enable and encourage writing tests that use your components as the end users use them. It also works with any test runner: [https://git.io/react-testing-library](https://git.io/react-testing-library)
24
+
> Alternatively, Airbnb has released a testing utility called [Enzyme](http://airbnb.io/enzyme/), which makes it easy to assert, manipulate, and traverse your React Components' output.
25
25
26
-
-[`Simulate`](#simulate)
27
-
-[`renderIntoDocument()`](#renderintodocument)
26
+
-[`act()`](#act)
28
27
-[`mockComponent()`](#mockcomponent)
29
28
-[`isElement()`](#iselement)
30
29
-[`isElementOfType()`](#iselementoftype)
@@ -38,68 +37,88 @@ var ReactTestUtils = require('react-dom/test-utils'); // ES5 with npm
When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component "one level deep" and assert facts about what its render method returns, without worrying about the behavior of child components, which are not instantiated or rendered. This does not require a DOM.
47
-
48
-
> Note:
49
-
>
50
-
> The shallow renderer has moved to `react-test-renderer/shallow`.<br>
51
-
> [Learn more about shallow rendering on its reference page.](/docs/shallow-renderer.html)
52
-
53
-
## Other Utilities
54
-
55
-
### `Simulate`
56
-
57
-
```javascript
58
-
Simulate.{eventName}(
59
-
element,
60
-
[eventData]
61
-
)
45
+
### `act()`
46
+
47
+
To prepare a component for assertions, wrap the code rendering it and performing updates inside an `act()` call. This makes your test run closer to how React works in the browser.
48
+
49
+
For example, let's say we have this `Counter` component:
> You will have to provide any event property that you're using in your component (e.g. keyCode, which, etc...) as React is not creating any of these for you.
89
-
90
-
* * *
91
-
92
-
### `renderIntoDocument()`
93
-
94
-
```javascript
95
-
renderIntoDocument(element)
96
-
```
97
-
98
-
Render a React element into a detached DOM node in the document. **This function requires a DOM.**
99
-
100
-
> Note:
101
-
>
102
-
> You will need to have `window`, `window.document` and `window.document.createElement` globally available **before** you import `React`. Otherwise React will think it can't access the DOM and methods like `setState` won't work.
121
+
Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a helper like [`react-testing-library`](https://github.com/kentcdodds/react-testing-library) to reduce the boilerplate code.
Same as [`scryRenderedComponentsWithType()`](#scryrenderedcomponentswithtype) but expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one.
267
286
287
+
***
288
+
289
+
### `renderIntoDocument()`
290
+
291
+
```javascript
292
+
renderIntoDocument(element)
293
+
```
294
+
295
+
Render a React element into a detached DOM node in the document. **This function requires a DOM.** It is effectively equivalent to:
296
+
297
+
```js
298
+
constdomContainer=document.createElement('div');
299
+
ReactDOM.render(element, domContainer);
300
+
```
301
+
302
+
> Note:
303
+
>
304
+
> You will need to have `window`, `window.document` and `window.document.createElement` globally available **before** you import `React`. Otherwise React will think it can't access the DOM and methods like `setState` won't work.
305
+
268
306
* * *
269
307
308
+
## Other Utilities
309
+
310
+
### `Simulate`
311
+
312
+
```javascript
313
+
Simulate.{eventName}(
314
+
element,
315
+
[eventData]
316
+
)
317
+
```
318
+
319
+
Simulate an event dispatch on a DOM node with optional `eventData` event data.
320
+
321
+
`Simulate` has a method for [every event that React understands](/docs/events.html#supported-events).
> You will have to provide any event property that you're using in your component (e.g. keyCode, which, etc...) as React is not creating any of these for you.
Copy file name to clipboardExpand all lines: content/docs/hooks-faq.md
+62Lines changed: 62 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -113,8 +113,70 @@ Importantly, custom Hooks give you the power to constrain React API if you'd lik
113
113
114
114
From React's point of view, a component using Hooks is just a regular component. If your testing solution doesn't rely on React internals, testing components with Hooks shouldn't be different from how you normally test components.
115
115
116
+
For example, let's say we have this counter component:
117
+
118
+
```js
119
+
functionExample() {
120
+
const [count, setCount] =useState(0);
121
+
useEffect(() => {
122
+
document.title=`You clicked ${count} times`;
123
+
});
124
+
return (
125
+
<div>
126
+
<p>You clicked {count} times</p>
127
+
<button onClick={() =>setCount(count +1)}>
128
+
Click me
129
+
</button>
130
+
</div>
131
+
);
132
+
}
133
+
```
134
+
135
+
We'll test it using React DOM. To make sure that the behavior matches what happens in the browser, we'll wrap the code rendering and updating it into [`ReactTestUtils.act()`](/docs/test-utils.html#act) calls:
The calls to `act()` will also flush the effects inside of them.
175
+
116
176
If you need to test a custom Hook, you can do so by creating a component in your test, and using your Hook from it. Then you can test the component you wrote.
117
177
178
+
To reduce the boilerplate, we recommend using [`react-testing-library`](https://git.io/react-testing-library) which is designed to encourage writing tests that use your components as the end users do.
179
+
118
180
### What exactly do the [lint rules](https://www.npmjs.com/package/eslint-plugin-react-hooks) enforce?
119
181
120
182
We provide an [ESLint plugin](https://www.npmjs.com/package/eslint-plugin-react-hooks) that enforces [rules of Hooks](/docs/hooks-rules.html) to avoid bugs. It assumes that any function starting with "`use`" and a capital letter right after it is a Hook. We recognize this heuristic isn't perfect and there may be some false positives, but without an ecosystem-wide convention there is just no way to make Hooks work well -- and longer names will discourage people from either adopting Hooks or following the convention.
0 commit comments