如何使用玩笑测试redux

如何使用玩笑测试redux

本文介绍了如何使用玩笑测试redux-saga的所有效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function* mySaga() {
  const [customers, products] = yield all([
    call(fetchCustomers),
    call(fetchProducts)
  ])
}

我想测试一下所有效果,但得到:Invalid attempt to destructure non-iterable instance

I want to test all effect in jest but I get: Invalid attempt to destructure non-iterable instance

我的代码是:

    const generator = mySaga()
    expect(generator.next().value).toEqual(all([
      call(fetchCustomers),
      call(fetchProducts)
    ]))

推荐答案

要处理错误,有必要首先了解函数生成器的一般工作原理,而与redux-saga库无关.每个yield操作都会执行中断生成器实例,并在输入/输出条目中指向yield关键字.

To deal with an error, it is necessary to understand at first how function generator generally works, irrespective of redux-saga library. Every yield operation performs interrupting generator instance, and point with yield keyword in input/output entry.

因此,yield的右值成为generator.next().value结果,并且generator.next(RESULT)成为yield关键字的左值. Redux-saga效果不会执行任何特殊的工作,只是制作特殊的动作对象( https://github.com/redux-saga/redux-saga/blob/master/src/internal/io.js )

So, right value of yield becomes generator.next().value result, and generator.next(RESULT) becomes left value of yield keyword. Redux-saga effects does not perform any special work, just makes special actions objects (https://github.com/redux-saga/redux-saga/blob/master/src/internal/io.js )

因此,要解决原始任务,只需将值传递给next()生成器函数,该函数将在const [customers, products] = yield语句中被破坏.

So, to solve original task, just pass value to next() generator function, which will be destructured in const [customers, products] = yield statement.

这篇关于如何使用玩笑测试redux-saga的所有效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:55