You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A nice thing about React components is that they are usually small and only rely on their props. That makes them easy to test.
257
257
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.
259
259
260
260
```
261
-
npm install --save-dev react-addons-test-utils
261
+
npm install --save-dev enzyme
262
262
```
263
263
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.
expect(todoInputProps.placeholder).toEqual('What needs to be done?')
339
331
})
340
332
341
333
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
+
constinput=enzymeWrapper.find('TodoTextInput')
336
+
input.props().onSave('')
345
337
expect(props.addTodo.calls.length).toBe(0)
346
-
input.props.onSave('Use Redux')
338
+
input.props().onSave('Use Redux')
347
339
expect(props.addTodo.calls.length).toBe(1)
348
340
})
349
341
})
350
342
})
351
-
```
352
-
353
-
#### Fixing Broken `setState()` in older React versions
354
343
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:
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`:
-[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.
495
457
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.
497
459
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.
0 commit comments