Last active
November 21, 2018 04:26
-
-
Save kotarella1110/b029eb6ed5325309e65a29f7a6c89f9d to your computer and use it in GitHub Desktop.
Counter app store example - TypeScript + React + Redux + typescript-fsa: : https://gist.github.com/kotarella1110/3c42138ba0d53d1eba0cecd6fee23dbc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { actions, reducer } from './counter'; | |
describe('counter actions', () => { | |
it('increment should create counter/INCREMENT action', () => { | |
expect(actions.increment()).toEqual({ | |
type: 'counter/INCREMENT', | |
}); | |
}); | |
it('decrement should create counter/DECREMENT action', () => { | |
expect(actions.decrement()).toEqual({ | |
type: 'counter/DECREMENT', | |
}); | |
}); | |
}); | |
describe('counter reducer', () => { | |
it('should handle counter/INCREMENT', () => { | |
expect(reducer(0, actions.increment())).toEqual(1); | |
}); | |
it('should handle counter/DECREMENT', () => { | |
expect(reducer(1, actions.decrement())).toEqual(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import actionCreatorFactory from 'typescript-fsa'; | |
import { reducerWithInitialState } from 'typescript-fsa-reducers'; | |
export type Action = | |
| ReturnType<typeof increment> | |
| ReturnType<typeof decrement>; | |
const actionCreator = actionCreatorFactory('counter'); | |
const increment = actionCreator('INCREMENT'); | |
const decrement = actionCreator('DECREMENT'); | |
export const actions = { increment, decrement }; | |
export interface State { | |
readonly counter: number; | |
} | |
const initialState = 0; | |
export const reducer = reducerWithInitialState(initialState) | |
.case(increment, state => state + 1) | |
.case(decrement, state => state - 1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment