如何测试使用来自已分派操作的数据的 Saga

How to test Saga that uses data from a dispatched action

我正在使用 redux-saga and testing using expect. Based on the example given in the (amazing) React Boilerplate,测试从商店(使用选择器)获取所需数据的 saga 没有问题。

但是,我的一些 sagas 依赖于调度操作中包含的数据:

export function* startSaga() {
  while (true) {
    const startAction = yield take(START);
    const id = startAction.id || false;
    ...

为了测试这个,我正在导入 saga 然后调用它:

import { startSaga } from '../sagas.js';
startGenerator = startSaga();
...

正因为如此,没有任何动作开始传奇,因此 id 的值始终为假。

我的 actions 和 reducer 是分开测试的:当像这样以独立方式测试 sagas 时,有没有简单的方法让它工作?

当 运行 saga 生成器函数时,您可以控制在每个 yield:

上传回的数据
import { startSaga } from '../sagas.js';
let startGenerator = startSaga();

let takeEffect = startGenerator.next()
expect(takeEffect).toEqual(take(START));
startGenerator.next({id: 234});