mirror of
https://github.com/reactjs/react.dev.git
synced 2026-02-24 04:33:10 +00:00
Add documentation for shallow testing
See #2393 for the original issue, and #2497 for the implementation.
This commit is contained in:
@@ -8,6 +8,46 @@ next: clone-with-props.html
|
||||
|
||||
`React.addons.TestUtils` makes it easy to test React components in the testing framework of your choice (we use [Jest](http://facebook.github.io/jest/)).
|
||||
|
||||
## Shallow rendering
|
||||
|
||||
Shallow rendering allows you to 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.
|
||||
|
||||
```javascript
|
||||
ReactShallowRenderer createRenderer()
|
||||
```
|
||||
|
||||
Call this in your tests to create a shallow renderer. You can think of this as a "place" to render the component you're testing, where it can respond to events and update itself.
|
||||
|
||||
```javascript
|
||||
shallowRenderer.render(ReactElement element)
|
||||
```
|
||||
|
||||
Similar to `React.render`.
|
||||
|
||||
```javascript
|
||||
ReactComponent shallowRenderer.getRenderOutput()
|
||||
```
|
||||
|
||||
After render has been called, returns shallowly rendered output. You can then begin to assert facts about the output. For example, if your component's render method returns:
|
||||
|
||||
```javascript
|
||||
<div>
|
||||
<span className="heading">Title</span>
|
||||
<Subcomponent foo="bar" />
|
||||
</div>
|
||||
```
|
||||
|
||||
Then you can assert:
|
||||
|
||||
```javascript
|
||||
result = renderer.getRenderOutput();
|
||||
expect(result.type).toBe('div');
|
||||
expect(result.props.children).toEqual([
|
||||
<span className="heading">Title</span>
|
||||
<Subcomponent foo="bar" />
|
||||
]);
|
||||
```
|
||||
|
||||
### Simulate
|
||||
|
||||
```javascript
|
||||
|
||||
Reference in New Issue
Block a user