* Add 'Test Utils' docs back to main navigation **why make this change?:** We accidentally removed this - still supporting the use of Test Utilities, so we should have them in the docs. **test plan:** Manually tested the website - will insert a screenshot. **issue:** https://github.com/facebook/react/issues/9651 * Move test-utils docs to reference section **what is the change?:** Moved from 'advanced guides' to 'reference' **why make this change?:** It makes more sense as a reference **test plan:** Visual inspection (flarnie may add a screenshot) **issue:** * Add back the shallow renderer docs and remove outdated docs **what is the change?:** - Remove outdated 'shallow renderer' docs on 'test utils' page, and point to the updated 'shallow renderer' docs. - Re-add a link to the updated 'shallow renderer' docs on the main navigation. **why make this change?:** This was already approved in https://github.com/facebook/react/pull/9331 which was then cherry-picked to https://github.com/facebook/react/pull/9359/commits and landed on master. I'm not sure why some of these changes didn't persist. For now just adding back the changes we need. **test plan:** Manually inspected website - will insert screenshots. **issue:** * Further improvements to 'shallow rendering' and 'test utils' docs Thanks @gaearon for the improvements! **what is the change?:** - Remove <hr/> from end of 'shallow rendering' docs - 'documents' -> 'documentation' - Move 'shallow rendering' redirection section to top of 'test utils' docs - Add intro sentence about testing to 'shallow rendering' docs **why make this change?:** Documentation helps people learn. **test plan:** Visual inspection
2.2 KiB
id, title, permalink, layout, category
| id | title | permalink | layout | category |
|---|---|---|---|---|
| shallow-renderer | Shallow Renderer | docs/shallow-renderer.html | docs | Reference |
Importing
import ReactShallowRenderer from 'react-test-renderer/shallow'; // ES6
var ReactShallowRenderer = require('react-test-renderer/shallow'); // ES5 with npm
Shallow Rendering
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.
You can think of the shallowRenderer as a "place" to render the component you're testing, and from which you can extract the component's output.
shallowRenderer.render() is similar to ReactDOM.render() but it doesn't require DOM and only renders a single level deep. This means you can test components isolated from how their children are implemented.
After shallowRenderer.render() has been called, you can use shallowRenderer.getRenderOutput() to get the shallowly rendered output.
You can then begin to assert facts about the output. For example, if you have the following component:
function MyComponent() {
return (
<div>
<span className="heading">Title</span>
<Subcomponent foo="bar" />
</div>
);
}
Then you can assert:
const ReactShallowRenderer = require('react-test-renderer/shallow');
const shallowRenderer = new ReactShallowRenderer();
shallowRenderer.render(<MyComponent />);
const result = shallowRenderer.getRenderOutput();
expect(result.type).toBe('div');
expect(result.props.children).toEqual([
<span className="heading">Title</span>,
<Subcomponent foo="bar" />
]);
Shallow testing currently has some limitations, namely not supporting refs.
We also recommend checking out Enzyme's Shallow Rendering API. It provides a nicer higher-level API over the same functionality.