如何在 sagas 中测试多个 takeEvery?

How to test multiple takeEvery in sagas?

我有以下 saga 听不同类型的 action:

export default function *() {
  yield takeEvery('FOO', listener)
  yield takeEvery('BAR', listener2)
  yield takeEvery('HELLO_WORLD', listener3)
}

本质上,这个 saga 在收到特定动作时具有多重行为。

如果它收到 FOO 作为动作类型,它将调用 listener 函数等

目前,我很难只在这 3 行中开玩笑地编写测试覆盖率。

我原以为写点什么也行,但运气不好:

describe('for action type that has "FOO"', () => {
  const actionPayload = {
    type: 'FOO',
  }
  const gen = saga({ type: actionPayload })

  it('listens to "FOO" and yield action', () => {
    const actual = gen.next()
    const expected = takeEvery('FOO', listener)
    expect(actual.value).toEqual(expected)
  })
})

我错过了什么?

takeEvery其实就是一个fork。所以你应该这样测试:

describe('for action type that has "SMS_API_REQUEST"', () => {
  const actionPayload = {
    type: 'FOO',
  }
  const gen = saga({ type: actionPayload })

  it('listens to "FOO" and yield action', () => {
    const actual = gen.next()
    const expected = fork(takeEvery, 'FOO', listener)
    expect(actual.value).toEqual(expected)
  })
})