Skip to content

Commit 0d4315f

Browse files
tylercolliertimdorr
authored andcommitted
Update doc to use test with Enzyme (reduxjs#1692)
1 parent 310ddba commit 0d4315f

File tree

2 files changed

+22
-59
lines changed

2 files changed

+22
-59
lines changed

docs/recipes/WritingTests.md

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,13 @@ describe('todos reducer', () => {
255255

256256
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
257257

258-
First, we will install [React Test Utilities](https://facebook.github.io/react/docs/test-utils.html):
258+
First, we will install [Enzyme](http://airbnb.io/enzyme/). Enzyme uses the [React Test Utilities](https://facebook.github.io/react/docs/test-utils.html) underneath, but is more convenient, readable, and powerful.
259259

260260
```
261-
npm install --save-dev react-addons-test-utils
261+
npm install --save-dev enzyme
262262
```
263263

264-
To test the components we make a `setup()` helper that passes the stubbed callbacks as props and renders the component with [React shallow renderer](https://facebook.github.io/react/docs/test-utils.html#shallow-rendering). This lets individual tests assert on whether the callbacks were called when expected.
264+
To test the components we make a `setup()` helper that passes the stubbed callbacks as props and renders the component with [shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html). This lets individual tests assert on whether the callbacks were called when expected.
265265

266266
#### Example
267267

@@ -300,85 +300,47 @@ can be tested like:
300300
```js
301301
import expect from 'expect'
302302
import React from 'react'
303-
import TestUtils from 'react-addons-test-utils'
303+
import { shallow } from 'enzyme'
304304
import Header from '../../components/Header'
305-
import TodoTextInput from '../../components/TodoTextInput'
306305

307306
function setup() {
308-
let props = {
307+
const props = {
309308
addTodo: expect.createSpy()
310309
}
311310

312-
let renderer = TestUtils.createRenderer()
313-
renderer.render(<Header {...props} />)
314-
let output = renderer.getRenderOutput()
311+
const enzymeWrapper = shallow(<Header {...props} />)
315312

316313
return {
317314
props,
318-
output,
319-
renderer
315+
enzymeWrapper
320316
}
321317
}
322318

323319
describe('components', () => {
324320
describe('Header', () => {
325-
it('should render correctly', () => {
326-
const { output } = setup()
321+
it('should render self and subcomponents', () => {
322+
const { enzymeWrapper } = setup()
327323

328-
expect(output.type).toBe('header')
329-
expect(output.props.className).toBe('header')
324+
expect(enzymeWrapper.find('header').hasClass('header')).toBe(true)
330325

331-
let [ h1, input ] = output.props.children
326+
expect(enzymeWrapper.find('h1').text()).toBe('todos')
332327

333-
expect(h1.type).toBe('h1')
334-
expect(h1.props.children).toBe('todos')
335-
336-
expect(input.type).toBe(TodoTextInput)
337-
expect(input.props.newTodo).toBe(true)
338-
expect(input.props.placeholder).toBe('What needs to be done?')
328+
const todoInputProps = enzymeWrapper.find('TodoTextInput').props()
329+
expect(todoInputProps.newTodo).toBe(true)
330+
expect(todoInputProps.placeholder).toEqual('What needs to be done?')
339331
})
340332

341333
it('should call addTodo if length of text is greater than 0', () => {
342-
const { output, props } = setup()
343-
let input = output.props.children[1]
344-
input.props.onSave('')
334+
const { enzymeWrapper, props } = setup()
335+
const input = enzymeWrapper.find('TodoTextInput')
336+
input.props().onSave('')
345337
expect(props.addTodo.calls.length).toBe(0)
346-
input.props.onSave('Use Redux')
338+
input.props().onSave('Use Redux')
347339
expect(props.addTodo.calls.length).toBe(1)
348340
})
349341
})
350342
})
351-
```
352-
353-
#### Fixing Broken `setState()` in older React versions
354343

355-
In React <= 0.13, 0.14.4 and 0.14.5, Shallow rendering [used to throw an error if `setState` is called](https://github.com/facebook/react/issues/4019). React seems to expect that, if you use `setState`, the DOM is available. To work around the issue, we use jsdom so React doesn’t throw the exception when the DOM isn’t available. Here’s how to [set it up](https://github.com/facebook/react/issues/5046#issuecomment-146222515):
356-
357-
```
358-
npm install --save-dev jsdom
359-
```
360-
361-
Then create a `setup.js` file in your test directory:
362-
363-
```js
364-
import { jsdom } from 'jsdom'
365-
366-
global.document = jsdom('<!doctype html><html><body></body></html>')
367-
global.window = document.defaultView
368-
global.navigator = global.window.navigator
369-
```
370-
371-
It’s important that this code is evaluated *before* React is imported. To ensure this, modify your `mocha` command to include `--require ./test/setup.js` in the options in your `package.json`:
372-
373-
```js
374-
{
375-
...
376-
"scripts": {
377-
...
378-
"test": "mocha --compilers js:babel-register --recursive --require ./test/setup.js",
379-
},
380-
...
381-
}
382344
```
383345

384346
### Connected Components
@@ -491,8 +453,8 @@ describe('middleware', () => {
491453

492454
### Glossary
493455

494-
- [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test Utilities for React.
456+
- [Enzyme](http://airbnb.io/enzyme/): Enzyme is a JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output.
495457

496-
- [jsdom](https://github.com/tmpvar/jsdom): A plain JavaScript implementation of the DOM API. jsdom allows us to run the tests without browser.
458+
- [React Test Utils](http://facebook.github.io/react/docs/test-utils.html): Test Utilities for React. Used by Enzyme.
497459

498-
- [Shallow rendering](http://facebook.github.io/react/docs/test-utils.html#shallow-rendering): Shallow rendering lets you instantiate a component and get the result of its `render` method just a single level deep instead of rendering components recursively to a DOM. The result of shallow rendering is a [ReactElement](https://facebook.github.io/react/docs/glossary.html#react-elements). That means it is possible to access its children, props and test if it works as expected. This also means that changing a child component won’t affect the tests for parent component.
460+
- [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html): Shallow rendering lets you instantiate a component and effectively get the result of its `render` method just a single level deep instead of rendering components recursively to a DOM. Shallow rendering is useful for unit tests, where you test a particular component only, and importantly not its children. This also means that changing a child component won’t affect the tests for the parent component. Testing a component and all its children can be accomplished with [Enzyme's `mount()` method](http://airbnb.io/enzyme/docs/api/mount.html), aka full DOM rendering.

examples/todomvc/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"babel-preset-react-hmre": "^1.1.1",
3333
"babel-register": "^6.3.13",
3434
"cross-env": "^1.0.7",
35+
"enzyme": "^2.2.0",
3536
"expect": "^1.8.0",
3637
"express": "^4.13.3",
3738
"jsdom": "^5.6.1",

0 commit comments

Comments
 (0)