Last active
December 21, 2016 14:29
-
-
Save jimbol/fe660007487e57a6e172ba26c3ada23b to your computer and use it in GitHub Desktop.
Spying on redux-saga effects
This file contains hidden or 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
// 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