From f6804d2504ae4112cfc5fa509f5a539c363f577e Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Tue, 27 Jan 2015 13:44:20 -0800 Subject: [PATCH 1/2] Add documentation for shallow testing See #2393 for the original issue, and #2497 for the implementation. --- docs/docs/10.4-test-utils.md | 40 ++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/docs/docs/10.4-test-utils.md b/docs/docs/10.4-test-utils.md index f768b69ae77..0e1bdc4971b 100644 --- a/docs/docs/10.4-test-utils.md +++ b/docs/docs/10.4-test-utils.md @@ -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 +
+ Title + +
+``` + +Then you can assert: + +```javascript +result = renderer.getRenderOutput(); +expect(result.type).toBe('div'); +expect(result.props.children).toEqual([ + Title + +]); +``` + ### Simulate ```javascript From 76f5453cc7551d0daa9a5a2f63b6ec06778a9383 Mon Sep 17 00:00:00 2001 From: Scott Feeney Date: Tue, 27 Jan 2015 17:09:16 -0800 Subject: [PATCH 2/2] Mention shallow rendering's limitations Also, move it to the bottom of the test utils documentation and mention that it's experimental. --- docs/docs/10.4-test-utils.md | 83 +++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/docs/docs/10.4-test-utils.md b/docs/docs/10.4-test-utils.md index 0e1bdc4971b..7c34ee2e32d 100644 --- a/docs/docs/10.4-test-utils.md +++ b/docs/docs/10.4-test-utils.md @@ -8,46 +8,6 @@ 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 -
- Title - -
-``` - -Then you can assert: - -```javascript -result = renderer.getRenderOutput(); -expect(result.type).toBe('div'); -expect(result.props.children).toEqual([ - Title - -]); -``` - ### Simulate ```javascript @@ -170,3 +130,46 @@ ReactComponent findRenderedComponentWithType(ReactComponent tree, function compo ``` Same as `scryRenderedComponentsWithType()` but expects there to be one result and returns that one result, or throws exception if there is any other number of matches besides one. + + +## Shallow rendering + +Shallow rendering is an experimental feature that 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. + +```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 +
+ Title + +
+``` + +Then you can assert: + +```javascript +result = renderer.getRenderOutput(); +expect(result.type).toBe('div'); +expect(result.props.children).toEqual([ + Title + +]); +``` + +Shallow testing currently has some limitations, namely not supporting refs. We're releasing this feature early and would appreciate the React community's feedback on how it should evolve.