Skip to content

Commit f6b0b63

Browse files
committed
updates
1 parent 15db1b0 commit f6b0b63

10 files changed

+293
-245
lines changed

Diff for: content/blog/2019-02-06-react-v16.8.0.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Even while Hooks were in alpha, the React community created many interesting [ex
5050

5151
## Testing Hooks {#testing-hooks}
5252

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 can also wrap their APIs with it (for example, [`react-testing-library`](https://github.com/kentcdodds/react-testing-library)'s `render` and `fireEvent` utilities do this).
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 can also wrap their APIs with it (for example, [`react-testing-library`](https://testing-library.com/react)'s `render` and `fireEvent` utilities do this).
5454

5555
For example, the counter example from [this page](/docs/hooks-effect.html) can be tested like this:
5656

@@ -95,7 +95,7 @@ The calls to `act()` will also flush the effects inside of them.
9595

9696
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.
9797

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.
98+
To reduce the boilerplate, we recommend using [`react-testing-library`](https://testing-library.com/react) which is designed to encourage writing tests that use your components as the end users do.
9999

100100
## Thanks {#thanks}
101101

Diff for: content/community/nav.yml

-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@
3434
title: Model Management
3535
- id: data-fetching
3636
title: Data Fetching
37-
- id: testing
38-
title: Testing
3937
- id: ui-components
4038
title: UI Components
4139
- id: misc

Diff for: content/docs/act.md

-109
This file was deleted.

Diff for: content/docs/addons-test-utils.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ it('can render and update a counter', () => {
122122
});
123123
```
124124

125-
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.
125+
- Don't forget that dispatching DOM events only works when the DOM container is added to the `document`. You can use a library like [React Testing Library](https://testing-library.com/react) to reduce the boilerplate code.
126+
127+
- The [`recipes`](/docs/recipes.html) document contains more details on how `act()` behaves, with examples and usage.
126128

127129
* * *
128130

@@ -139,7 +141,7 @@ Pass a mocked component module to this method to augment it with useful methods
139141

140142
> Note:
141143
>
142-
> `mockComponent()` is a legacy API. We recommend using [shallow rendering](/docs/shallow-renderer.html) or [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) instead.
144+
> `mockComponent()` is a legacy API. We recommend using [`jest.mock()`](https://facebook.github.io/jest/docs/en/tutorial-react-native.html#mock-native-modules-using-jestmock) instead.
143145
144146
* * *
145147

Diff for: content/docs/hooks-faq.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ The calls to `act()` will also flush the effects inside of them.
180180

181181
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.
182182

183-
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.
183+
To reduce the boilerplate, we recommend using [React Testing Library](https://testing-library.com/react) which is designed to encourage writing tests that use your components as the end users do.
184184

185185
### What exactly do the [lint rules](https://www.npmjs.com/package/eslint-plugin-react-hooks) enforce? {#what-exactly-do-the-lint-rules-enforce}
186186

Diff for: content/docs/nav.yml

+8-2
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@
7373
title: Static Type Checking
7474
- id: strict-mode
7575
title: Strict Mode
76-
- id: testing
77-
title: Testing
7876
- id: typechecking-with-proptypes
7977
title: Typechecking With PropTypes
8078
- id: uncontrolled-components
@@ -125,6 +123,14 @@
125123
title: Hooks API Reference
126124
- id: hooks-faq
127125
title: Hooks FAQ
126+
- title: Testing
127+
items:
128+
- id: testing
129+
title: Testing Overview
130+
- id: testing-environments
131+
title: Testing Environments
132+
- id: testing-recipes
133+
title: Testing Recipes
128134
- title: Contributing
129135
items:
130136
- id: how-to-contribute

Diff for: content/docs/reference-test-renderer.md

+31
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ expect(testInstance.findByProps({className: "sub"}).children).toEqual(['Sub']);
7070
### TestRenderer {#testrenderer}
7171

7272
* [`TestRenderer.create()`](#testrenderercreate)
73+
* [`TestRenderer.act()`](#testrendereract)
7374

7475
### TestRenderer instance {#testrenderer-instance}
7576

@@ -104,6 +105,36 @@ TestRenderer.create(element, options);
104105

105106
Create a `TestRenderer` instance with the passed React element. It doesn't use the real DOM, but it still fully renders the component tree into memory so you can make assertions about it. The returned instance has the following methods and properties.
106107

108+
### `TestRenderer.act()` {#testrendereract}
109+
110+
```javascript
111+
TestRenderer.act(callback);
112+
```
113+
114+
Similar to the [`act()` helper from `react-dom/test-utils`](/docs/test-utils.html#act), `TestRenderer.act` prepares a component for assertions. Use this version of `act()` to wrap calls to `TestRenderer.create` and `testRenderer.update`.
115+
116+
```javascript
117+
import {create, act} from 'react-test-renderer';
118+
import App from './app.js'; // The component being tested
119+
120+
// render the component
121+
let root;
122+
act(() => {
123+
root = create(<App value={1}/>)
124+
});
125+
126+
// make assertions on root
127+
expect(root.toJSON()).toMatchSnapshot();
128+
129+
// update with some different props
130+
act(() => {
131+
root = root.update(<App value={2}/>);
132+
})
133+
134+
// make assertions on root
135+
expect(root.toJSON()).toMatchSnapshot();
136+
```
137+
107138
### `testRenderer.toJSON()` {#testrenderertojson}
108139

109140
```javascript

0 commit comments

Comments
 (0)