Search code examples
javascriptreactjsjestjsredux-sagaredux-saga-test-plan

How to test multiple takeEvery in sagas?


I have the following saga that listens different type of action:

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

Essentially, this saga has a multiple behavior when it receives certain action.

If it recieves FOO as the action type, it will invoke listener function, etc.

At the moment, I am having hard time to write test coverage in jest only in these 3 lines.

I thought writing something along with this would work, but no luck:

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

What am I missing?


Solution

  • takeEvery is actually a fork. So you should test like this:

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