A Redux-Saga test runner for Mocha
- Switching the order of instructions should be easy
- Associate instructions with their results
- Changing instruction results should be easy
Lets start with a basic effect.
export function* getFoos(action) {
const token = yield select(getToken);
const response = yield call(fetchFoos, action.bars, token);
yield put(handleResponse(response));
}In our mocha tests, we can stub a run like so. Its good to see it all together up front.
let action = {};
let token = 'abc-123';
const response = {};
beforeEach(() => {
run = effectRunner(getFoos)
.next('init', action)
.next('getToken', token)
.next('fetchFoos', response)
.next('finish')
.run();
});First we pass the effect into the runner
effectRunner(getFoos)The first step after invoking effectRunner represents the invocation of the effect itself.
effectRunner(getFoos)
.next('init', action)This is like calling getFoos(action).
After that we define each step, giving each a name and result (if needed).
runner.next('getToken', token)The last step will be the final yield.
runner.next('finish');Once you are ready to test, run the runner.
run = runner.run();In our tests we can assert against pieces of the run
expect(run.getToken).to.deep.equal(select(getToken));
expect(run.fetchFoos).to.deep.equal(call(fetchFoos, action.bars, token));Just pass in overrides for a given action when you call run.
run = runner.run({
getToken: 'invalid-token',
});