Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rradczewski
Forked from Arturszott/redux-pre-thunk.js
Last active July 20, 2016 18:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rradczewski/cb9e4c45dafa029ce6aa6305b7959e6a to your computer and use it in GitHub Desktop.
Save rradczewski/cb9e4c45dafa029ce6aa6305b7959e6a to your computer and use it in GitHub Desktop.
// snip for production
const store = createStore(thunk.withExtraArgument({fetch}));
// snip effect
import { createAction } from 'redux-actions';
const createEffect = (name, fun) => (...args) => dispatch => {
const action = createAction(name);
dispatch({ type: `${name}_STARTED` });
return dispatch(fun(...args))
.then(
payload => dispatch(action(payload)),
error => dispatch(action(error))
));
};
const saveScore = createEffect(
'SAVE_SCORE',
score => (_, __, {fetch}) =>
fetch('/scores', { method: 'POST', { score } })
);
const mapDispatchToProps = ({ saveScore });
// snip connected test
import { expectRedux, storeSpy } from 'expect-redux';
it('dispatches a SAVE_SCORE after saving via dispatcher', () => {
const fetch = createSpy().andReturn(Promise.resolve());
const store = createStore(compose(storeSpy, thunk.withExtraArgument({fetch})));
const connectedComponent = shallow(<Component store={store}/>);
connectedComponent.props().saveScore(5);
return expectRedux(store)
.toDispatchAnAction()
.matching({type: 'SAVE_SCORE'})
.then(() => {
expect(fetch).toHaveBeenCalledWith('/scores', { method: 'POST', { score: 5 }});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment