Skip to content

Commit

Permalink
Merge pull request #1482 from fshowalter/use_enzyme_in_tests
Browse files Browse the repository at this point in the history
use enzyme in tests (resolves #1481)
  • Loading branch information
gaearon committed Mar 6, 2016
2 parents 952b45d + 17ed918 commit 3598a8d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
4 changes: 2 additions & 2 deletions examples/counter/package.json
Expand Up @@ -4,7 +4,7 @@
"description": "Redux counter example",
"scripts": {
"start": "node server.js",
"test": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-register --require ./test/setup.js",
"test": "cross-env NODE_ENV=test mocha --recursive --compilers js:babel-register",
"test:watch": "npm test -- --watch"
},
"repository": {
Expand All @@ -30,9 +30,9 @@
"babel-preset-react-hmre": "^1.0.1",
"babel-register": "^6.3.13",
"cross-env": "^1.0.7",
"enzyme": "^2.0.0",
"expect": "^1.6.0",
"express": "^4.13.3",
"jsdom": "^5.6.1",
"mocha": "^2.2.5",
"node-libs-browser": "^0.5.2",
"react-addons-test-utils": "^0.14.7",
Expand Down
23 changes: 12 additions & 11 deletions examples/counter/test/components/Counter.spec.js
@@ -1,63 +1,64 @@
import expect from 'expect'
import React from 'react'
import TestUtils from 'react-addons-test-utils'
import { shallow } from 'enzyme'
import Counter from '../../components/Counter'

function setup(value = 0) {
const actions = {
onIncrement: expect.createSpy(),
onDecrement: expect.createSpy()
}
const component = TestUtils.renderIntoDocument(
const component = shallow(
<Counter value={value} {...actions} />
)

return {
component: component,
actions: actions,
buttons: TestUtils.scryRenderedDOMComponentsWithTag(component, 'button'),
p: TestUtils.findRenderedDOMComponentWithTag(component, 'p')
buttons: component.find('button'),
p: component.find('p')
}
}

describe('Counter component', () => {
it('should display count', () => {
const { p } = setup()
expect(p.textContent).toMatch(/^Clicked: 0 times/)
expect(p.text()).toMatch(/^Clicked: 0 times/)
})

it('first button should call onIncrement', () => {
const { buttons, actions } = setup()
TestUtils.Simulate.click(buttons[0])
buttons.at(0).simulate('click')
expect(actions.onIncrement).toHaveBeenCalled()
})

it('second button should call onDecrement', () => {
const { buttons, actions } = setup()
TestUtils.Simulate.click(buttons[1])
buttons.at(1).simulate('click')
expect(actions.onDecrement).toHaveBeenCalled()
})

it('third button should not call onIncrement if the counter is even', () => {
const { buttons, actions } = setup(42)
TestUtils.Simulate.click(buttons[2])
buttons.at(2).simulate('click')
expect(actions.onIncrement).toNotHaveBeenCalled()
})

it('third button should call onIncrement if the counter is odd', () => {
const { buttons, actions } = setup(43)
TestUtils.Simulate.click(buttons[2])
buttons.at(2).simulate('click')
expect(actions.onIncrement).toHaveBeenCalled()
})

it('third button should call onIncrement if the counter is odd and negative', () => {
const { buttons, actions } = setup(-43)
TestUtils.Simulate.click(buttons[2])
buttons.at(2).simulate('click')
expect(actions.onIncrement).toHaveBeenCalled()
})

it('fourth button should call onIncrement in a second', (done) => {
const { buttons, actions } = setup()
TestUtils.Simulate.click(buttons[3])
buttons.at(3).simulate('click')
setTimeout(() => {
expect(actions.onIncrement).toHaveBeenCalled()
done()
Expand Down
5 changes: 0 additions & 5 deletions examples/counter/test/setup.js

This file was deleted.

0 comments on commit 3598a8d

Please sign in to comment.