Skip to content

Commit

Permalink
Merge pull request reduxjs#1288 from rackt/simplify-counter
Browse files Browse the repository at this point in the history
Simplify counter example
  • Loading branch information
gaearon committed Jan 27, 2016
2 parents 188b758 + 5286c9c commit 1539f63
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 269 deletions.
34 changes: 0 additions & 34 deletions examples/counter/actions/counter.js

This file was deleted.

44 changes: 33 additions & 11 deletions examples/counter/components/Counter.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
import React, { Component, PropTypes } from 'react'

class Counter extends Component {
constructor(props) {
super(props)
this.incrementAsync = this.incrementAsync.bind(this)
this.incrementIfOdd = this.incrementIfOdd.bind(this)
}

incrementIfOdd() {
if (this.props.value % 2 === 1) {
this.props.onIncrement()
}
}

incrementAsync() {
setTimeout(this.props.onIncrement, 1000)
}

render() {
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props
const { value, onIncrement, onDecrement } = this.props
return (
<p>
Clicked: {counter} times
Clicked: {value} times
{' '}
<button onClick={increment}>+</button>
<button onClick={onIncrement}>
+
</button>
{' '}
<button onClick={decrement}>-</button>
<button onClick={onDecrement}>
-
</button>
{' '}
<button onClick={incrementIfOdd}>Increment if odd</button>
<button onClick={this.incrementIfOdd}>
Increment if odd
</button>
{' '}
<button onClick={() => incrementAsync()}>Increment async</button>
<button onClick={this.incrementAsync}>
Increment async
</button>
</p>
)
}
}

Counter.propTypes = {
increment: PropTypes.func.isRequired,
incrementIfOdd: PropTypes.func.isRequired,
incrementAsync: PropTypes.func.isRequired,
decrement: PropTypes.func.isRequired,
counter: PropTypes.number.isRequired
value: PropTypes.number.isRequired,
onIncrement: PropTypes.func.isRequired,
onDecrement: PropTypes.func.isRequired
}

export default Counter
16 changes: 0 additions & 16 deletions examples/counter/containers/App.js

This file was deleted.

30 changes: 19 additions & 11 deletions examples/counter/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
import React from 'react'
import { render } from 'react-dom'
import { Provider } from 'react-redux'
import App from './containers/App'
import configureStore from './store/configureStore'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import Counter from './components/Counter'
import counter from './reducers'

const store = configureStore()
const store = createStore(counter)
const rootEl = document.getElementById('root')

render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById('root')
)
function render() {
ReactDOM.render(
<Counter
value={store.getState()}
onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
/>,
rootEl
)
}

render()
store.subscribe(render)
4 changes: 1 addition & 3 deletions examples/counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@
"dependencies": {
"react": "^0.14.6",
"react-dom": "^0.14.6",
"react-redux": "^4.0.6",
"redux": "^3.0.6",
"redux-thunk": "^1.0.3"
"redux": "^3.0.6"
},
"devDependencies": {
"babel-core": "^5.6.18",
Expand Down
12 changes: 0 additions & 12 deletions examples/counter/reducers/counter.js

This file was deleted.

18 changes: 10 additions & 8 deletions examples/counter/reducers/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { combineReducers } from 'redux'
import counter from './counter'

const rootReducer = combineReducers({
counter
})

export default rootReducer
export default function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
21 changes: 0 additions & 21 deletions examples/counter/store/configureStore.js

This file was deleted.

76 changes: 0 additions & 76 deletions examples/counter/test/actions/counter.spec.js

This file was deleted.

41 changes: 25 additions & 16 deletions examples/counter/test/components/Counter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import React from 'react'
import TestUtils from 'react-addons-test-utils'
import Counter from '../../components/Counter'

function setup() {
function setup(value = 0) {
const actions = {
increment: expect.createSpy(),
incrementIfOdd: expect.createSpy(),
incrementAsync: expect.createSpy(),
decrement: expect.createSpy()
onIncrement: expect.createSpy(),
onDecrement: expect.createSpy()
}
const component = TestUtils.renderIntoDocument(<Counter counter={1} {...actions} />)
const component = TestUtils.renderIntoDocument(
<Counter value={value} {...actions} />
)
return {
component: component,
actions: actions,
Expand All @@ -22,30 +22,39 @@ function setup() {
describe('Counter component', () => {
it('should display count', () => {
const { p } = setup()
expect(p.textContent).toMatch(/^Clicked: 1 times/)
expect(p.textContent).toMatch(/^Clicked: 0 times/)
})

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

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

it('third button should call incrementIfOdd', () => {
const { buttons, actions } = setup()
it('third button should not call onIncrement if the counter is even', () => {
const { buttons, actions } = setup(42)
TestUtils.Simulate.click(buttons[2])
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])
expect(actions.incrementIfOdd).toHaveBeenCalled()
expect(actions.onIncrement).toHaveBeenCalled()
})

it('fourth button should call incrementAsync', () => {
it('fourth button should call onIncrement in a second', (done) => {
const { buttons, actions } = setup()
TestUtils.Simulate.click(buttons[3])
expect(actions.incrementAsync).toHaveBeenCalled()
setTimeout(() => {
expect(actions.onIncrement).toHaveBeenCalled()
done()
}, 1000)
})
})
Loading

0 comments on commit 1539f63

Please sign in to comment.