|
1 | | -# `react-test-renderer` |
| 1 | +# `react-shallow-renderer` |
2 | 2 |
|
3 | | -This package provides an experimental React renderer that can be used to render React components to pure JavaScript objects, without depending on the DOM or a native mobile environment. |
| 3 | +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. |
4 | 4 |
|
5 | | -Essentially, this package makes it easy to grab a snapshot of the "DOM tree" rendered by a React DOM or React Native component without using a browser or jsdom. |
| 5 | +## Installation |
6 | 6 |
|
7 | | -Documentation: |
| 7 | +```sh |
| 8 | +# npm |
| 9 | +npm install react-shallow-renderer --save-dev |
8 | 10 |
|
9 | | -[https://reactjs.org/docs/test-renderer.html](https://reactjs.org/docs/test-renderer.html) |
| 11 | +# Yarn |
| 12 | +yarn add react-shallow-renderer --dev |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
10 | 16 |
|
11 | | -Usage: |
| 17 | +For example, if you have the following component: |
12 | 18 |
|
13 | 19 | ```jsx |
14 | | -const ReactTestRenderer = require('react-test-renderer'); |
| 20 | +function MyComponent() { |
| 21 | + return ( |
| 22 | + <div> |
| 23 | + <span className="heading">Title</span> |
| 24 | + <Subcomponent foo="bar" /> |
| 25 | + </div> |
| 26 | + ); |
| 27 | +} |
| 28 | +``` |
15 | 29 |
|
16 | | -const renderer = ReactTestRenderer.create( |
17 | | - <Link page="https://www.facebook.com/">Facebook</Link> |
18 | | -); |
| 30 | +Then you can assert: |
19 | 31 |
|
20 | | -console.log(renderer.toJSON()); |
21 | | -// { type: 'a', |
22 | | -// props: { href: 'https://www.facebook.com/' }, |
23 | | -// children: [ 'Facebook' ] } |
| 32 | +```jsx |
| 33 | +import ShallowRenderer from 'react-shallow-renderer'; |
| 34 | +// in your test: |
| 35 | +const renderer = new ShallowRenderer(); |
| 36 | +renderer.render(<MyComponent />); |
| 37 | +const result = renderer.getRenderOutput(); |
| 38 | +expect(result.type).toBe('div'); |
| 39 | +expect(result.props.children).toEqual([ |
| 40 | + <span className="heading">Title</span>, |
| 41 | + <Subcomponent foo="bar" />, |
| 42 | +]); |
24 | 43 | ``` |
25 | 44 |
|
26 | | -You can also use Jest's snapshot testing feature to automatically save a copy of the JSON tree to a file and check in your tests that it hasn't changed: https://facebook.github.io/jest/blog/2016/07/27/jest-14.html. |
| 45 | +Shallow testing currently has some limitations, namely not supporting refs. |
| 46 | + |
| 47 | +> Note: |
| 48 | +> |
| 49 | +> We also recommend checking out Enzyme's [Shallow Rendering API](https://airbnb.io/enzyme/docs/api/shallow.html). It provides a nicer higher-level API over the same functionality. |
| 50 | +
|
| 51 | +## Reference |
| 52 | + |
| 53 | +### `shallowRenderer.render()` |
| 54 | + |
| 55 | +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. |
| 56 | + |
| 57 | +`shallowRenderer.render()` is similar to [`ReactDOM.render()`](https://reactjs.org/docs/react-dom.html#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. |
| 58 | + |
| 59 | +### `shallowRenderer.getRenderOutput()` |
| 60 | + |
| 61 | +After `shallowRenderer.render()` has been called, you can use `shallowRenderer.getRenderOutput()` to get the shallowly rendered output. |
| 62 | + |
| 63 | +You can then begin to assert facts about the output. |
0 commit comments