Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing redux-saga takeEvery

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' } }  
like image 311
David Bradshaw Avatar asked May 12 '26 08:05

David Bradshaw


1 Answers

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)
like image 182
jfairbank Avatar answered May 15 '26 06:05

jfairbank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!