-
-
Save rradczewski/cb9e4c45dafa029ce6aa6305b7959e6a to your computer and use it in GitHub Desktop.
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
// 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