Skip to content

Instantly share code, notes, and snippets.

@jayphelps
Created February 11, 2017 21:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jayphelps/f3ce4cb13c9076cec22f77ae2776ae21 to your computer and use it in GitHub Desktop.
Save jayphelps/f3ce4cb13c9076cec22f77ae2776ae21 to your computer and use it in GitHub Desktop.
testing epics by mocking
// 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