Skip to content

Instantly share code, notes, and snippets.

@pahund
Created August 18, 2017 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pahund/22f53dfa4087cba7c63faaa4844a6b8a to your computer and use it in GitHub Desktop.
Save pahund/22f53dfa4087cba7c63faaa4844a6b8a to your computer and use it in GitHub Desktop.
Code Example for Testing a Redux Saga with Mocha
import { push } from 'react-router-redux';
import { takeLatest, call, put } from 'redux-saga/effects';
import config from '../../../config/client';
import { REQUEST_POST_LIST } from '../actions';
import updatePostList from '../actions/updatePostList';
import fetchPostList from './utils/fetchPostList';
import trackPageViewWithGoogleAnalytics from '../actions/trackPageViewWithGoogleAnalytics';
import trackPageViewWithIvw from '../actions/trackPageViewWithIvw';
export function *requestPostList({ threadId, page }) {
let data;
try {
data = yield call(fetchPostList, threadId, page);
} catch (e) {
yield put(push(`${config.mountPoint}/error/500.html`));
return;
}
yield put(updatePostList(data));
yield put(trackPageViewWithGoogleAnalytics());
yield put(trackPageViewWithIvw());
}
export default function *() {
yield takeLatest(REQUEST_POST_LIST, requestPostList);
}
import mockery from 'mockery';
import { push } from 'react-router-redux';
import { takeLatest, call, put } from 'redux-saga/effects';
import { REQUEST_POST_LIST } from 'src/shared/redux/actions';
import updatePostList from 'src/shared/redux/actions/updatePostList';
import trackPageViewWithGoogleAnalytics from 'src/shared/redux/actions/trackPageViewWithGoogleAnalytics';
import trackPageViewWithIvw from 'src/shared/redux/actions/trackPageViewWithIvw';
import requestPostListWatcher, { requestPostList } from 'src/shared/redux/sagas/requestPostList';
import fetchPostList from 'src/shared/redux/sagas/utils/fetchPostList';
describe('[shared/redux/sagas/requestPostList]', () => {
describe('When I instantiate the “go to page” saga generator', () => {
let generator;
beforeEach(() => generator = requestPostListWatcher());
describe('and I call its “next” method', () => {
let result;
beforeEach(() => result = generator.next());
describe('the value returned by the generator', () => {
it('is a redux-saga “take latest” of the “go to page” action with the argument I passed in', () => {
result.value.should.deep.equal(takeLatest(REQUEST_POST_LIST, requestPostList));
});
});
});
});
describe('When I instantiate the “go to page” generator', () => {
let generator;
beforeEach(() => generator = requestPostList({ threadId: 666, page: 7 }));
describe('and I call its “next” method', () => {
let next;
beforeEach(() => next = generator.next());
describe('the “next” value', () =>
it('is a call to the “fetch post list” function with the arguments I provided', () =>
next.value.should.deep.equal(call(fetchPostList, 666, 7))
)
);
describe('and I call “next” again with some data', () => {
beforeEach(() => next = generator.next('qux'));
describe('the “next” value', () =>
it('is a call to “put” of redux-saga with a “update post list” action with my data', () =>
next.value.should.deep.equal(put(updatePostList('qux')))
)
);
describe('and I call “next” again', () => {
beforeEach(() => next = generator.next());
describe('the “next” value', () =>
it('is a call to “put” with a “track page view with Google Analytics” action', () =>
next.value.should.deep.equal(put(trackPageViewWithGoogleAnalytics()))
)
);
describe('and I call “next” again', () => {
beforeEach(() => next = generator.next());
describe('the “next” value', () =>
it('is a call to “put” with a “track page view with IVW” action', () =>
next.value.should.deep.equal(put(trackPageViewWithIvw()))
)
);
});
});
});
});
});
describe(
'When I instantiate the “go to page” generator, ' +
'rigged to throw an error when trying to fetch data', () => {
let generator;
beforeEach(() => {
mockery.enable({
useCleanCache: true,
warnOnUnregistered: false
});
mockery.registerMock('redux-saga/effects', {
takeLatest,
call() { throw Error('ZOMG!'); },
put
});
mockery.registerMock('../../../config/client', {
mountPoint: '/my-mount-point'
});
const riggedrequestPostList = require('../../../../src/shared/redux/sagas/requestPostList').requestPostList;
generator = riggedrequestPostList({ threadId: 666, page: 7 });
});
describe('and I call its “next” method', () => {
let next;
beforeEach(() => next = generator.next());
describe('the “next” value', () =>
it('is a call to “put” of redux-saga with a “push” action with an error URL', () =>
next.value.should.deep.equal(put(push('/my-mount-point/error/500.html')))
)
);
});
afterEach(() => {
mockery.deregisterAll();
mockery.disable();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment