Skip to content

Instantly share code, notes, and snippets.

@jimbol
Last active December 21, 2016 14:29
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 jimbol/fe660007487e57a6e172ba26c3ada23b to your computer and use it in GitHub Desktop.
Save jimbol/fe660007487e57a6e172ba26c3ada23b to your computer and use it in GitHub Desktop.
Spying on redux-saga effects
// In test file
const spy = effectSpy(generator)
.next('init')
.next('getToken', token) // name and pass in yield values
.next('getUpdatedFooBars', fooBars)
.next('putFooBars');
// in beforeEach
mySpyRun = spy.run({
getToken: 'invalid-token' // accepts overrides
});
// in `it` statement
expect(mySpyRun.getToken.value).to.deep.equal(select(getToken))
expect(mySpyRun.putFooBars.value)
.to.deep.equal(put(fooBarsAction, fooBars))
// In test utils file
export const effectRunner = function effectRunner(generator) {
const steps = [];
const next = function (name, value) {
steps.push({ name, value });
return { next, run };
};
const run = function (overrides = {}) {
let g;
const stepOutput = {};
let prevValue;
steps.forEach(({ name, value }) => {
if (g) {
stepOutput[name] = g.next(prevValue);
prevValue = overrides[name] || value;
return;
}
g = generator(value);
});
return stepOutput;
};
return { next, run };
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment