Skip to content

Instantly share code, notes, and snippets.

@kotarella1110
Last active November 21, 2018 04:26
Show Gist options
  • Save kotarella1110/b029eb6ed5325309e65a29f7a6c89f9d to your computer and use it in GitHub Desktop.
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
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);
});
});
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