Skip to content

Commit 9100734

Browse files
Converts counter away from redux-actions
1 parent 5fe4bc2 commit 9100734

File tree

10 files changed

+52
-42
lines changed

10 files changed

+52
-42
lines changed

src/components/CounterComponent-spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('CounterComponent', () => {
1010

1111
beforeEach(() => {
1212
props = {
13-
counter: { value: 0 },
13+
counter: 0,
1414
increaseCounter: sinon.spy(),
1515
decreaseCounter: sinon.spy(),
1616
};
@@ -32,11 +32,11 @@ describe('CounterComponent', () => {
3232

3333
describe('display', () => {
3434
it('renders initial value', () => {
35-
expect(instance.refs.displayValue.innerHTML).to.equal(props.counter.value.toString());
35+
expect(instance.refs.displayValue.innerHTML).to.equal(props.counter.toString());
3636
});
3737

3838
it('renders new value', () => {
39-
props.counter.value = 100;
39+
props.counter = 100;
4040
instance.forceUpdate();
4141
expect(instance.refs.displayValue.innerHTML).to.equal('100');
4242
});

src/components/CounterComponent.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as React from 'react';
22
import { ICounterState } from '../store/counter';
33

44
export interface CounterProps {
5-
increaseCounter(amount: number);
6-
decreaseCounter(amount: number);
5+
increaseCounter();
6+
decreaseCounter();
77
counter: ICounterState;
88
}
99

@@ -13,9 +13,9 @@ export default class CounterComponent extends React.Component<CounterProps, any>
1313

1414
return (
1515
<div>
16-
value????: <span ref="displayValue">{this.props.counter.value}</span>
17-
<button ref="increase" onClick={() => increaseCounter(+1) }>increase</button>
18-
<button ref="decrease" onClick={() => decreaseCounter(1) }>decrease</button>
16+
value????: <span ref="displayValue">{this.props.counter}</span>
17+
<button ref="increase" onClick={() => increaseCounter() }>increase</button>
18+
<button ref="decrease" onClick={() => decreaseCounter() }>decrease</button>
1919
</div>
2020
);
2121
}

src/store/counter/actions-spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { expect } from 'chai';
22
import { INCREASE_COUNTER, DECREASE_COUNTER, increaseCounter, decreaseCounter } from './';
33

4-
describe('counter-actions', () => {
4+
describe('counter / actions', () => {
55
it('increaseCounter', () =>
6-
expect(increaseCounter(1)).to.eql({
6+
expect(increaseCounter()).to.eql({
77
type: INCREASE_COUNTER,
8-
payload: { value: 1 }
8+
payload: 1,
99
})
1010
);
1111

1212
it('decreaseCounter', () =>
13-
expect(decreaseCounter(1)).to.eql({
13+
expect(decreaseCounter()).to.eql({
1414
type: DECREASE_COUNTER,
15-
payload: { value: 1 }
15+
payload: -1,
1616
})
1717
);
1818
});

src/store/counter/actions.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { INCREASE_COUNTER, DECREASE_COUNTER } from './constants';
2-
import { ICounterState } from './state';
3-
import { createAction } from 'redux-actions';
1+
import { INCREASE_COUNTER, DECREASE_COUNTER, ICounterAction } from './types';
42

5-
export const increaseCounter = createAction<number, ICounterState>(INCREASE_COUNTER, value => ({ value }));
6-
export const decreaseCounter = createAction<number, ICounterState>(DECREASE_COUNTER, value => ({ value }));
3+
export function increaseCounter(): ICounterAction {
4+
return {
5+
type: INCREASE_COUNTER,
6+
payload: 1,
7+
};
8+
}
9+
10+
export function decreaseCounter(): ICounterAction {
11+
return {
12+
type: DECREASE_COUNTER,
13+
payload: -1,
14+
};
15+
}

src/store/counter/constants.ts

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

src/store/counter/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './actions';
2-
export * from './state';
3-
export * from './constants';
2+
export * from './types';
43
export * from './reducer';

src/store/counter/reducer-spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { expect } from 'chai';
22
import { counterReducer, increaseCounter, decreaseCounter } from './';
33

4-
describe.only('counter-reducer', () => {
5-
it('sets the initial state', () => expect(counterReducer(undefined, increaseCounter(1))).to.eql({ value: 1 }));
6-
it('handles INCREASE_COUNTER', () => expect(counterReducer({ value: 10 }, increaseCounter(1))).to.eql({ value: 11 }));
7-
it('handles DECREASE_COUNTER', () => expect(counterReducer({ value: 10 }, decreaseCounter(1))).to.eql({ value: 9 }));
4+
describe('counter / reducer', () => {
5+
it('sets the initial state', () => expect(counterReducer(undefined, increaseCounter())).to.eql(1));
6+
it('handles INCREASE_COUNTER', () => expect(counterReducer(10, increaseCounter())).to.eql(11));
7+
it('handles DECREASE_COUNTER', () => expect(counterReducer(10, decreaseCounter())).to.eql(9));
88
});

src/store/counter/reducer.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import { INCREASE_COUNTER, DECREASE_COUNTER, ICounterState } from './';
2-
import { handleActions } from 'redux-actions';
1+
import { INCREASE_COUNTER, DECREASE_COUNTER, ICounterState, ICounterAction } from './types';
32

4-
const initialState: ICounterState = {
5-
value: 0
6-
};
3+
const initialState: ICounterState = 0;
74

8-
export const counterReducer = handleActions<ICounterState>({
9-
[INCREASE_COUNTER]: (state, action) => ({
10-
value: state.value + action.payload.value
11-
}),
5+
function updateCounter(state: ICounterState, action: ICounterAction): ICounterState {
6+
return state + action.payload;
7+
}
128

13-
[DECREASE_COUNTER]: (state, action) => ({
14-
value: state.value - action.payload.value
15-
}),
16-
}, initialState);
9+
export function counterReducer(state: ICounterState = initialState, action: ICounterAction): ICounterState {
10+
switch (action.type) {
11+
case INCREASE_COUNTER: return updateCounter(state, action);
12+
case DECREASE_COUNTER: return updateCounter(state, action);
13+
}
14+
15+
return state;
16+
}

src/store/counter/state.ts

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

src/store/counter/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { IPayloadAction } from '../interfaces';
2+
3+
export type ICounterState = number;
4+
export interface ICounterAction extends IPayloadAction<ICounterState> { }
5+
6+
export const INCREASE_COUNTER: string = 'INCREASE_COUNTER';
7+
export const DECREASE_COUNTER: string = 'DECREASE_COUNTER';

0 commit comments

Comments
 (0)