如何在sagas中测试多个takeEvery?

时间:2018-08-05 23:50:54

标签: javascript reactjs jestjs redux-saga redux-saga-test-plan

我有以下的传奇故事,它们监听着不同类型的action

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

从本质上讲,这个传奇故事在收到某些动作时会具有多种行为。

如果将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)
  })
})

我想念什么?

1 个答案:

答案 0 :(得分:1)

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)
  })
})