Skip to content

Instantly share code, notes, and snippets.

@mdboop
Last active January 5, 2017 19:39
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 mdboop/3fbb07a3ce0c69452c0e396f5dd54d41 to your computer and use it in GitHub Desktop.
Save mdboop/3fbb07a3ce0c69452c0e396f5dd54d41 to your computer and use it in GitHub Desktop.
import configureMockStore from 'redux-mock-store';
import { createEpicMiddleware, combineEpics } from 'redux-observable';
import { Observable } from 'rxjs';
import userEpic from './userEpic.js';
import { fetchUser } from '../actionCreators/users.js';
describe('userEpic', () => {
// set up our mock data and mock request function
const user = { name: 'John Doe', email: 'john@foo.com' };
const payload = { response: user };
const request = () => Observable.of(payload);
// Wrap the epic we want to test in a function that accepts actions and store, so we can pass it our dependency.
const rootEpic = (actions, store) => userEpic(actions, store, { request });
const epicMiddleware = createEpicMiddleware(rootEpic);
const mockStore = configureMockStore([epicMiddleware]);
let store;
const initialState = {
user: null,
};
beforeEach(() => {
store = mockStore(fromJS(initialState));
});
afterEach(() => {
epicMiddleware.replaceEpic(allReviewsEpic);
});
it('should listen on a FETCH_REVIEWS action', () => {
store.dispatch(fetchUser());
expect(store.getActions()).toEqual([
fetchUser(),
fetchUserSuccess(payload.response),
]);
});
});
/* in your epic file... */
import { ajax } from 'rxjs/observable/dom/ajax';
// Default to our Observable ajax functions when wer're not testing.
export const fetchUserEpic = (action$, store, { request } = { request: ajax })) =>
action$.ofType(FETCH_REVIEWS)
.mergeMap(() => request('/user')
.map(({ response }) => fetchUserSuccess(response))
.catch((response) => [fetchUserFailure(response.err)])
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment