A little helper to test [redux-saga][rs].
$ npm install --save-dev redux-saga-test
The saga under test.
function * testSaga () {
try {
yield put({type: 'FETCHING'})
const data = yield call(loadData)
yield put({type: 'FETCHED', payload: data})
} catch (e) {
yield put({type: 'FETCHED', payload: e})
}
The test looks like this:
const fromGenerator = require('redux-saga-test');
test('saga', (t) => {
const expect = fromGenerator(t, testSaga()) // <= pass your assert library with a `deepEqual` method.
expect.next().put({type: 'FETCHING'})
expect.next().call(loadData)
expect.next(mockData).put({type: 'FETCHED', payload: mockData})
})
To test a watcher
you can use expect.takeEvery/takeLatest
.
The watcher:
function * watchEvery () {
yield * takeEvery('TEST_ACTION', testSaga)
}
The test:
test('takeEvery', (t) => {
const expect = fromGenerator(t, watchEvery())
expect.takeEvery('TEST_ACTION', testSaga)
})
For more examples look at the tests
MIT © Stoeffel