3

I have the following super simple Redux-Saga that I want to test with Jest.

function* nextApi() {
  yield* takeEvery(
    (action) => !!(action.meta && action.meta.next),
    nextApiSaga
  )
}

I've looked at Redux-Sagas-Test-Plan, but that only seems to allow you to unit test functions that contain Saga Effect Creators and doesn't seem to support Saga Helpers. There is also Redux-Saga-Test but that just does a deepEqual on the yielded effect and doesn't test the arrow function.

What I want to be able to do is past the following two objects to takeEvery and see that nextApiSaga is only called in the second case.

{ type: 'foo' }

{ type: 'foo', meta: { next: 'bar' } }  
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70
  • 1
    Hi, David. redux-saga-test-plan has supported saga helpers for a long time actually. You might have missed the docs on those: [Saga Helpers](http://redux-saga-test-plan.jeremyfairbank.com/unit-testing/saga-helpers.html). – jfairbank Jan 30 '17 at 19:23

2 Answers2

2

I left you a comment about redux-saga-test-plan having methods for saga helpers, but you can easily test takeEvery with it. Call testSaga with your saga and then invoke the takeEvery method assertion with the pattern (note I keep a reference to your original anonymous function) and the other saga.

const helper = action => !!(action.meta && action.meta.next)

function* nextApi() {
  yield* takeEvery(
    helper,
    nextApiSaga
  )
}

testSaga(nextApi).takeEvery(helper, nextApiSaga)
jfairbank
  • 151
  • 1
  • 5
0

Taking a different approach, I came up with this. Not sure if it's the best answer, but it seems to work. Adding here in case anyone else has the same problem and still open to better suggestions.

function getTakeEveryFunction(saga) {
  return saga().next().value.TAKE.pattern
}

it('takes actions with meta.next property', () => {
  const func = getTakeEveryFunction(nextApi)
  expect(func({ type:'foo' })).toBe(false)
  expect(func({ type:'foo',  meta: { next: 'bar' } })).toBe(true)
})
David Bradshaw
  • 11,859
  • 3
  • 41
  • 70