enyzme: update for v3

This commit is contained in:
Rico Sta. Cruz 2017-10-12 18:10:12 +08:00
parent 33c2309a93
commit fe728b7322
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
4 changed files with 201 additions and 8 deletions

View File

@ -43,6 +43,7 @@ updated: 2017-08-30 # To show in the updated list
ads: false # Add this to disable ads ads: false # Add this to disable ads
weight: -5 # lower number = higher in related posts list weight: -5 # lower number = higher in related posts list
deprecated: true # Don't show in related posts deprecated: true # Don't show in related posts
deprecated_by: /enzyme # Point to latest version
prism_languages: [vim] # Extra syntax highlighting prism_languages: [vim] # Extra syntax highlighting
intro: | intro: |
This is some *Markdown* at the beginning of the article. This is some *Markdown* at the beginning of the article.

View File

@ -33,6 +33,13 @@
</aside> </aside>
{% endif %} {% endif %}
{% if page.deprecated_by %}
<aside class='notice-box'>
<strong>Deprecated:</strong> This guide covers an older version.
<a href='{{ base }}{{ page.deprecated_by }}'>A newer version is available here.</a>
</aside>
{% endif %}
{% if page.intro %} {% if page.intro %}
<div class='intro-content MarkdownBody'> <div class='intro-content MarkdownBody'>
{{ page.intro | markdownify }} {{ page.intro | markdownify }}

View File

@ -2,13 +2,45 @@
title: Enzyme title: Enzyme
category: React category: React
layout: 2017/sheet layout: 2017/sheet
updated: 2017-09-14 updated: 2017-10-12
weight: -1 weight: -1
intro: |
[Enzyme](http://airbnb.io/enzyme) lets you write unit tests for React components. This guide covers Enzyme 3.x.
--- ---
## Getting started ## Getting started
{: .-three-column} {: .-three-column}
### Initial setup
```
npm install --save-dev enyzme \
enzyme-adapter-react-16 \
react-test-renderer
```
{: .-setup}
#### test/setup.js
```js
import Enzyme from 'enzyme'
import Adapter from 'enzyme-adapter-react-16'
Enzyme.configure({ adapter: new Adapter() })
```
#### package.json
```js
"jest": {
"setupFiles": [
"test/setup.js"
]
}
```
This configures Enzyme for React v16, and Jest to automatically configure Enzyme for you. There are other adapters in Enzyme's [installation instructions](http://airbnb.io/enzyme/#installation).
### Mounting ### Mounting
{: .-prime} {: .-prime}
@ -27,24 +59,39 @@ wrap = mount(<MyComponent />)
Shallow wrapping doesn't descend down to sub-components. Shallow wrapping doesn't descend down to sub-components.
A full mount also mounts sub-components. A full mount also mounts sub-components.
See: [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html), See: [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html),
[Full rendering](http://airbnb.io/enzyme/docs/api/mount.html) [Full rendering](http://airbnb.io/enzyme/docs/api/mount.html)
### Jest ### Jest snapshots
```js ```
import toJson from 'enzyme-to-json' npm install --save-dev enzyme-to-json
``` ```
{: .-setup} {: .-setup}
#### package.json
```js
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
```
#### Test
```js ```js
it('works', () => { it('works', () => {
wrap = mount(<MyComponent />) wrap = mount(<MyComponent />)
expect(toJson(wrap)).toMatchSnapshot() expect(wrap).toMatchSnapshot()
}) })
``` ```
Converts an Enzyme wrapper to a format compatible with Jest snapshots. See: [enzyme-to-json](https://www.npmjs.com/package/enzyme-to-json) Converts an Enzyme wrapper to a format compatible with Jest snapshots.
See: [enzyme-to-json](https://www.npmjs.com/package/enzyme-to-json)
### Debugging ### Debugging
@ -52,7 +99,9 @@ Converts an Enzyme wrapper to a format compatible with Jest snapshots. See: [enz
console.log(wrap.debug()) console.log(wrap.debug())
``` ```
Shows HTML for debugging purposes. See: [debug()](http://airbnb.io/enzyme/docs/api/ReactWrapper/debug.html) Shows HTML for debugging purposes.
See: [debug()](http://airbnb.io/enzyme/docs/api/ReactWrapper/debug.html)
## ReactWrapper ## ReactWrapper
@ -129,4 +178,5 @@ wrap.containsAnyMatchingElements([ <div /> ]) // => boolean
## References ## References
- <https://airbnb.io/enzyme> - [Enzyme website](https://airbnb.io/enzyme) _(airbnb.io)_
- [Enzyme v2 cheatsheet](./enzyme@2) _(devhints.io)_ (old version)

135
enzyme@2.md Normal file
View File

@ -0,0 +1,135 @@
---
title: Enzyme v2
category: React
layout: 2017/sheet
updated: 2017-09-14
weight: -1
deprecated_by: /enzyme
intro: |
[Enzyme](http://airbnb.io/enzyme) lets you write unit tests for React components. This guide covers Enzyme 2.x.
---
## Getting started
{: .-three-column}
### Mounting
{: .-prime}
```js
import {shallow, mount} from 'enzyme'
```
{: .-setup}
```js
wrap = shallow(<MyComponent />)
```
```js
wrap = mount(<MyComponent />)
```
Shallow wrapping doesn't descend down to sub-components.
A full mount also mounts sub-components.
See: [Shallow rendering](http://airbnb.io/enzyme/docs/api/shallow.html),
[Full rendering](http://airbnb.io/enzyme/docs/api/mount.html)
### Jest
```js
import toJson from 'enzyme-to-json'
```
{: .-setup}
```js
it('works', () => {
wrap = mount(<MyComponent />)
expect(toJson(wrap)).toMatchSnapshot()
})
```
Converts an Enzyme wrapper to a format compatible with Jest snapshots. See: [enzyme-to-json](https://www.npmjs.com/package/enzyme-to-json)
### Debugging
```js
console.log(wrap.debug())
```
Shows HTML for debugging purposes. See: [debug()](http://airbnb.io/enzyme/docs/api/ReactWrapper/debug.html)
## ReactWrapper
### Traversing
```js
wrap.find('button') // => ReactWrapper
wrap.filter('button') // => ReactWrapper
wrap.not('span') // => ReactWrapper (inverse of filter())
wrap.children() // => ReactWrapper
wrap.parent() // => ReactWrapper
wrap.closest('div') // => ReactWrapper
wrap.childAt(0) // => ReactWrapper
wrap.at(0) // => ReactWrapper
wrap.first() // => ReactWrapper
wrap.last() // => ReactWrapper
```
```js
wrap.get(0) // => ReactElement
wrap.getNode() // => ReactElement
wrap.getNodes() // => Array<ReactElement>
wrap.getDOMNode() // => DOMComponent
```
See: [Full rendering API](http://airbnb.io/enzyme/docs/api/mount.html)
### Actions
```js
wrap.simulate('click')
```
### React components
```js
wrap.setState({ ... })
wrap.setProps({ ... })
wrap.setContext({ ... })
wrap.state() // => Any (get state)
wrap.props() // => object (get props)
wrap.context() // => Any (get context)
wrap.instance() // => ReactComponent
```
### Mount
```js
wrap.mount()
wrap.unmount()
wrap.update() // calls forceUpdate()
```
### Tests
```js
wrap.debug() // => string
wrap.html() // => string
wrap.text() // => string
wrap.type() // => string | function
wrap.name() // => string
wrap.is('.classname') // => boolean
wrap.hasClass('class') // => boolean
wrap.exists() // => boolean
wrap.contains(<div />) // => boolean
wrap.contains([ <div /> ]) // => boolean
wrap.containsMatchingElement(<div />) // => boolean
wrap.containsAllMatchingElements([ <div /> ]) // => boolean
wrap.containsAnyMatchingElements([ <div /> ]) // => boolean
```
## References
- <https://airbnb.io/enzyme>