12 - "wrap.find().simulate('click')"
13 - "wrap.contains(<div/>)"
20 [Enzyme](http://airbnb.io/enzyme) lets you write unit tests for React components. This guide covers Enzyme 3.x.
22 - [Enzyme website](https://enzymejs.github.io/enzyme/) _(enzymejs.github.io)_
28 import {shallow, mount} from 'enzyme'
33 wrap = shallow(<MyComponent />)
37 wrap = mount(<MyComponent />)
40 Shallow wrapping doesn't descend down to sub-components.
41 A full mount also mounts sub-components.
43 See: [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html),
44 [Full rendering](http://airbnb.io/enzyme/docs/api/mount.html)
49 console.log(wrap.debug())
52 Shows HTML for debugging purposes.
54 See: [debug()](http://airbnb.io/enzyme/docs/api/ReactWrapper/debug.html)
63 import { shallow } from 'enzyme'
64 import MyComponent from '../MyComponent'
71 <MyComponent name='Groot' />
74 expect(wrap.text()).toEqual('I am Groot')
83 wrap.setProps({ name: 'Moe' })
84 wrap.setState({ show: true })
90 expect(wrap.prop('name')).toEqual('Moe')
91 expect(wrap.state('show')).toEqual(true)
95 expect('name' in wrap.props()).toEqual('Moe')
96 expect('show' in wrap.state()).toEqual(true)
103 wrap.containsMatchingElement(
104 <span>I am groot</span>
109 `containsMatchingElement()` is probably the most useful assertion in Jest.
114 expect(wrap).toMatchSnapshot()
117 Be sure you've set up enzyme-to-json for snapshots (see [Installing](#installing) below).
123 wrap.find('button').text()
127 Use `.find()` to traverse down to nodes. It will return wrapper objects, too.
129 ### Simulating events
132 wrap.find('input').simulate('click')
135 #### With event object props
138 wrap.find('input').simulate('change', {
139 target: { value: 'hello' }
148 npm install --save-dev enzyme \
149 enzyme-adapter-react-16 \
157 import Enzyme from 'enzyme'
158 import Adapter from 'enzyme-adapter-react-16'
160 Enzyme.configure({ adapter: new Adapter() })
173 This configures Enzyme for React v16, and Jest to automatically configure Enzyme for you. There are other adapters in Enzyme's installation instructions.
175 See: [Installation](http://airbnb.io/enzyme/#installation)
180 npm install --save-dev enzyme-to-json
188 "snapshotSerializers": [
189 "enzyme-to-json/serializer"
198 wrap = mount(<MyComponent />)
199 expect(wrap).toMatchSnapshot()
203 Optional, but recommended: This allows you to use Enzyme wrappers with Jest snapshots.
205 See: [enzyme-to-json](https://www.npmjs.com/package/enzyme-to-json)
212 wrap.find('button') // → ReactWrapper
213 wrap.filter('button') // → ReactWrapper
214 wrap.not('span') // → ReactWrapper (inverse of filter())
215 wrap.children() // → ReactWrapper
216 wrap.parent() // → ReactWrapper
217 wrap.closest('div') // → ReactWrapper
218 wrap.childAt(0) // → ReactWrapper
219 wrap.at(0) // → ReactWrapper
220 wrap.first() // → ReactWrapper
221 wrap.last() // → ReactWrapper
225 wrap.get(0) // → ReactElement
226 wrap.getElement() // → ReactElement
227 wrap.getElements() // → Array<ReactElement>
228 wrap.getDOMNode() // → DOMComponent
231 See: [Full rendering API](http://airbnb.io/enzyme/docs/api/mount.html)
236 wrap.simulate('click')
242 wrap.setState({ ··· })
243 wrap.setProps({ ··· })
244 wrap.setContext({ ··· })
248 wrap.state() // get full state
249 wrap.props() // get full props
250 wrap.context() // get full context
254 wrap.state('key') // → any
255 wrap.prop('key') // → any
256 wrap.context('key') // → any
260 wrap.instance() // → ReactComponent
268 wrap.update() // calls forceUpdate()
274 wrap.debug() // → string
275 wrap.html() // → string
276 wrap.text() // → string
277 wrap.type() // → string | function
278 wrap.name() // → string
279 wrap.is('.classname') // → boolean
280 wrap.hasClass('class') // → boolean
281 wrap.exists() // → boolean
282 wrap.contains(<div />) // → boolean
283 wrap.contains([ <div /> ]) // → boolean
284 wrap.some('.child') // → boolean
286 wrap.someWhere(n => n.hasClass('foo'))
288 wrap.containsMatchingElement(<div />) // → boolean
289 wrap.containsAllMatchingElements([ <div /> ]) // → boolean
290 wrap.containsAnyMatchingElements([ <div /> ]) // → boolean
295 - [Enzyme website](https://airbnb.io/enzyme) _(airbnb.io)_
296 - [Enzyme v2 cheatsheet](./enzyme@2) _(devhints.io)_ (old version)