Created
February 11, 2017 21:54
-
-
Save jayphelps/f3ce4cb13c9076cec22f77ae2776ae21 to your computer and use it in GitHub Desktop.
testing epics by mocking
This file contains hidden or 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
// api.js | |
// your API call helpers | |
import { ajax } from 'rxjs/observable/dom/ajax'; | |
export const fetchSomething = id => | |
ajax.getJSON(`/somethings/${id}`); | |
// the epic | |
export const somethingEpic = (action$, store, { api }) => | |
action$.ofType(SOMETHING) | |
.mergeMap(action => | |
api.fetchSomething(action.id) | |
.map(response => somethingFulfilled(response)) | |
); | |
// inject your dependencies into all of your epics | |
// when making the root epic | |
import * as api from './api' | |
export const rootEpic = (...args) => | |
combineEpics(somethingEpic, anotherEpic)(...args, { api }); | |
// later to test, just mock the dependencies | |
const action$ = ActionsObservable.of({ type: SOMETHING, id: '123' }); | |
const response = { id: '123', something: 'example' }; | |
const api = { fetchSomething: id => Observable.of(response) }; | |
somethingEpic(action$, null, { api }) | |
.toArray() | |
.subscribe(actions => { | |
assertDeepEqual(actions, [ | |
somethingFulfilled(response) | |
]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment