Skip to content

Commit 5286c9c

Browse files
committed
Simplify counter example
1 parent 188b758 commit 5286c9c

File tree

12 files changed

+95
-269
lines changed

12 files changed

+95
-269
lines changed

examples/counter/actions/counter.js

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,52 @@
11
import React, { Component, PropTypes } from 'react'
22

33
class Counter extends Component {
4+
constructor(props) {
5+
super(props)
6+
this.incrementAsync = this.incrementAsync.bind(this)
7+
this.incrementIfOdd = this.incrementIfOdd.bind(this)
8+
}
9+
10+
incrementIfOdd() {
11+
if (this.props.value % 2 === 1) {
12+
this.props.onIncrement()
13+
}
14+
}
15+
16+
incrementAsync() {
17+
setTimeout(this.props.onIncrement, 1000)
18+
}
19+
420
render() {
5-
const { increment, incrementIfOdd, incrementAsync, decrement, counter } = this.props
21+
const { value, onIncrement, onDecrement } = this.props
622
return (
723
<p>
8-
Clicked: {counter} times
24+
Clicked: {value} times
925
{' '}
10-
<button onClick={increment}>+</button>
26+
<button onClick={onIncrement}>
27+
+
28+
</button>
1129
{' '}
12-
<button onClick={decrement}>-</button>
30+
<button onClick={onDecrement}>
31+
-
32+
</button>
1333
{' '}
14-
<button onClick={incrementIfOdd}>Increment if odd</button>
34+
<button onClick={this.incrementIfOdd}>
35+
Increment if odd
36+
</button>
1537
{' '}
16-
<button onClick={() => incrementAsync()}>Increment async</button>
38+
<button onClick={this.incrementAsync}>
39+
Increment async
40+
</button>
1741
</p>
1842
)
1943
}
2044
}
2145

2246
Counter.propTypes = {
23-
increment: PropTypes.func.isRequired,
24-
incrementIfOdd: PropTypes.func.isRequired,
25-
incrementAsync: PropTypes.func.isRequired,
26-
decrement: PropTypes.func.isRequired,
27-
counter: PropTypes.number.isRequired
47+
value: PropTypes.number.isRequired,
48+
onIncrement: PropTypes.func.isRequired,
49+
onDecrement: PropTypes.func.isRequired
2850
}
2951

3052
export default Counter

examples/counter/containers/App.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

examples/counter/index.js

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import React from 'react'
2-
import { render } from 'react-dom'
3-
import { Provider } from 'react-redux'
4-
import App from './containers/App'
5-
import configureStore from './store/configureStore'
2+
import ReactDOM from 'react-dom'
3+
import { createStore } from 'redux'
4+
import Counter from './components/Counter'
5+
import counter from './reducers'
66

7-
const store = configureStore()
7+
const store = createStore(counter)
8+
const rootEl = document.getElementById('root')
89

9-
render(
10-
<Provider store={store}>
11-
<App />
12-
</Provider>,
13-
document.getElementById('root')
14-
)
10+
function render() {
11+
ReactDOM.render(
12+
<Counter
13+
value={store.getState()}
14+
onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
15+
onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
16+
/>,
17+
rootEl
18+
)
19+
}
20+
21+
render()
22+
store.subscribe(render)

examples/counter/package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@
1919
"dependencies": {
2020
"react": "^0.14.6",
2121
"react-dom": "^0.14.6",
22-
"react-redux": "^4.0.6",
23-
"redux": "^3.0.6",
24-
"redux-thunk": "^1.0.3"
22+
"redux": "^3.0.6"
2523
},
2624
"devDependencies": {
2725
"babel-core": "^5.6.18",

examples/counter/reducers/counter.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/counter/reducers/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { combineReducers } from 'redux'
2-
import counter from './counter'
3-
4-
const rootReducer = combineReducers({
5-
counter
6-
})
7-
8-
export default rootReducer
1+
export default function counter(state = 0, action) {
2+
switch (action.type) {
3+
case 'INCREMENT':
4+
return state + 1
5+
case 'DECREMENT':
6+
return state - 1
7+
default:
8+
return state
9+
}
10+
}

examples/counter/store/configureStore.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

examples/counter/test/actions/counter.spec.js

Lines changed: 0 additions & 76 deletions
This file was deleted.

examples/counter/test/components/Counter.spec.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import React from 'react'
33
import TestUtils from 'react-addons-test-utils'
44
import Counter from '../../components/Counter'
55

6-
function setup() {
6+
function setup(value = 0) {
77
const actions = {
8-
increment: expect.createSpy(),
9-
incrementIfOdd: expect.createSpy(),
10-
incrementAsync: expect.createSpy(),
11-
decrement: expect.createSpy()
8+
onIncrement: expect.createSpy(),
9+
onDecrement: expect.createSpy()
1210
}
13-
const component = TestUtils.renderIntoDocument(<Counter counter={1} {...actions} />)
11+
const component = TestUtils.renderIntoDocument(
12+
<Counter value={value} {...actions} />
13+
)
1414
return {
1515
component: component,
1616
actions: actions,
@@ -22,30 +22,39 @@ function setup() {
2222
describe('Counter component', () => {
2323
it('should display count', () => {
2424
const { p } = setup()
25-
expect(p.textContent).toMatch(/^Clicked: 1 times/)
25+
expect(p.textContent).toMatch(/^Clicked: 0 times/)
2626
})
2727

28-
it('first button should call increment', () => {
28+
it('first button should call onIncrement', () => {
2929
const { buttons, actions } = setup()
3030
TestUtils.Simulate.click(buttons[0])
31-
expect(actions.increment).toHaveBeenCalled()
31+
expect(actions.onIncrement).toHaveBeenCalled()
3232
})
3333

34-
it('second button should call decrement', () => {
34+
it('second button should call onDecrement', () => {
3535
const { buttons, actions } = setup()
3636
TestUtils.Simulate.click(buttons[1])
37-
expect(actions.decrement).toHaveBeenCalled()
37+
expect(actions.onDecrement).toHaveBeenCalled()
3838
})
3939

40-
it('third button should call incrementIfOdd', () => {
41-
const { buttons, actions } = setup()
40+
it('third button should not call onIncrement if the counter is even', () => {
41+
const { buttons, actions } = setup(42)
42+
TestUtils.Simulate.click(buttons[2])
43+
expect(actions.onIncrement).toNotHaveBeenCalled()
44+
})
45+
46+
it('third button should call onIncrement if the counter is odd', () => {
47+
const { buttons, actions } = setup(43)
4248
TestUtils.Simulate.click(buttons[2])
43-
expect(actions.incrementIfOdd).toHaveBeenCalled()
49+
expect(actions.onIncrement).toHaveBeenCalled()
4450
})
4551

46-
it('fourth button should call incrementAsync', () => {
52+
it('fourth button should call onIncrement in a second', (done) => {
4753
const { buttons, actions } = setup()
4854
TestUtils.Simulate.click(buttons[3])
49-
expect(actions.incrementAsync).toHaveBeenCalled()
55+
setTimeout(() => {
56+
expect(actions.onIncrement).toHaveBeenCalled()
57+
done()
58+
}, 1000)
5059
})
5160
})

0 commit comments

Comments
 (0)