Last active
January 5, 2017 19:39
-
-
Save mdboop/3fbb07a3ce0c69452c0e396f5dd54d41 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
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