Saga Helpers
Redux Saga Test Plan also offers assertions for the saga helper effects
takeEvery, takeLatest, and throttle. These helpers come in two flavors.
Your sagas can delegate (i.e. use yield*) to these helpers if you import them
directly from the redux-saga module. This is now a deprecated pattern
according to Redux Saga. You are encouraged to now import the equivalent effect
creators for these helpers from the redux-saga/effects module and just yield
them like other effect creators. Redux Saga still supports delegating to the
helpers, but if it removes support for delegation, then Redux Saga Test Plan
will also necessarily remove support for testing delegations.
To decide which assertions you need for testing your saga, please review the possibilities below:
If you are using the helper effect creators from
redux-saga/effects, you can use the respective methodstakeEveryEffect,takeLatestEffect, andthrottleEffectfrom Redux Saga Test Plan.If your saga delegates to any of these helpers from
redux-sagadirectly, then you can use the respectivetakeEvery,takeLatest, andthrottlemethods from Redux Saga Test Plan. The difference between these assertions and the normal effect creator assertions is that you shouldn't callnexton your test saga beforehand. These methods will automatically advance the saga for you.DEPRECATION NOTICE:
Delegating is now a deprecated pattern for Redux Saga. Please move to using the effect creators added in Redux Saga v0.14.0. If Redux Saga removes this pattern, then Redux Saga Test Plan will also remove the respective assertion methods.Starting in Redux Saga v0.12.0, instead of delegating to the helpers from the
redux-sagamodule, you could instead yield them like normal effect creators such ascallorput. These are non-blocking yields; internally, Redux Saga will fork them like the normalforkeffect creator. If you yield instead of delegating, then you can use the respectivetakeEveryFork,takeLatestFork, andthrottleForkmethods from Redux Saga Test Plan. Because these assertions are like the normal effect creator assertions, you WILL need to callnextprior to these assertions.DEPRECATION NOTICE:
Because these are the same helpers you can delegate to, this pattern may also be removed by Redux Saga, meaning Redux Saga Test Plan will also remove the respective assertion methods.
Summary
| Helper | Effect Creator | Delegating Helper DEPRECATED |
Yielding Helper DEPRECATED |
|---|---|---|---|
takeEvery |
Use takeEveryEffect assertion.Call next before. |
Use takeEvery assertion.Don't call next before. |
Use takeEveryFork assertion.Call next before. |
takeLatest |
Use takeLatestEffect assertion.Call next before. |
Use takeLatest assertion.Don't call next before. |
Use takeLatestFork assertion.Call next before. |
throttle |
Use throttleEffect assertion.Call next before. |
Use throttle assertion.Don't call next before. |
Use throttleFork assertion.Call next before. |
Delegating Example
import { takeEvery } from 'redux-saga';
import { call } from 'redux-saga/effects';
import testSaga from 'redux-saga-test-plan';
function identity(value) {
return value;
}
function* otherSaga(action, value) {
yield call(identity, value);
}
function* anotherSaga(action) {
yield call(identity, action.payload);
}
function* mainSaga() {
yield call(identity, 'foo');
yield* takeEvery('READY', otherSaga, 42);
}
// All good
testSaga(mainSaga)
.next()
.call(identity, 'foo')
.takeEvery('READY', otherSaga, 42)
.finish()
.isDone();
// Will throw
testSaga(mainSaga)
.next()
.call(identity, 'foo')
.takeEvery('READY', anotherSaga, 42)
.finish()
.isDone();
// SagaTestError:
// Assertion 2 failed: expected takeEvery to fork anotherSaga
//
// Expected
// --------
// [Function: anotherSaga]
//
// Actual
// ------
// [Function: otherSaga]
Yielding Example
import { takeEvery } from 'redux-saga';
import { call } from 'redux-saga/effects';
import testSaga from 'redux-saga-test-plan';
function identity(value) {
return value;
}
function* otherSaga(action, value) {
yield call(identity, value);
}
function* anotherSaga(action) {
yield call(identity, action.payload);
}
function* mainSaga() {
yield call(identity, 'foo');
yield takeEvery('READY', otherSaga, 42);
}
// All good
testSaga(mainSaga)
.next()
.call(identity, 'foo')
.next()
.takeEveryFork('READY', otherSaga, 42)
.finish()
.isDone();
// Will throw
testSaga(mainSaga)
.next()
.call(identity, 'foo')
.next()
.takeEveryFork('READY', anotherSaga, 42)
.finish()
.isDone();
// SagaTestError:
// Assertion 2 failed: expected takeEvery to fork anotherSaga
//
// Expected
// --------
// [Function: anotherSaga]
//
// Actual
// ------
// [Function: otherSaga]