Skip to content

use enzyme in tests (resolves #1481) #1482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/counter/package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.