@samitouri / QOS-React-2 / commits / f6804d2504

Add documentation for shallow testing

See #2393 for the original issue, and #2497 for the implementation.

Scott Feeney committed Jan 27, 2015 at 13:44 UTC f6804d2504ae4112cfc5fa509f5a539c363f577e
1 file changed +40
docs/docs/10.4-test-utils.md
+40
@@ -8,6 +8,46 @@ next: clone-with-props.html
8
9 `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/)).
10
11 +## Shallow rendering
12 +
13 +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.
14 +
15 +```javascript
16 +ReactShallowRenderer createRenderer()
17 +```
18 +
19 +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.
20 +
21 +```javascript
22 +shallowRenderer.render(ReactElement element)
23 +```
24 +
25 +Similar to `React.render`.
26 +
27 +```javascript
28 +ReactComponent shallowRenderer.getRenderOutput()
29 +```
30 +
31 +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:
32 +
33 +```javascript
34 +<div>
35 + <span className="heading">Title</span>
36 + <Subcomponent foo="bar" />
37 +</div>
38 +```
39 +
40 +Then you can assert:
41 +
42 +```javascript
43 +result = renderer.getRenderOutput();
44 +expect(result.type).toBe('div');
45 +expect(result.props.children).toEqual([
46 + <span className="heading">Title</span>
47 + <Subcomponent foo="bar" />
48 +]);
49 +```
50 +
51 ### Simulate
52
53 ```javascript