Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing redux-saga select

I have a simple saga of this form:

const getAccountDetails = function * () {
   const { url } = yield select(state => state.appConfig)
   const accountDetails = yield call(apiFetchAccountDetails, url)
}

I'm trying to write a unit test:

describe('getAccountDetails', () => {
    const iterator = getAccountDetails()
    it("should yield an Effect 'select(state=> state.appConfig)'", () => {
       const effect = iterator.next().value
       const expected = select(state => state.appConfig)
       expect(effect).to.deep.eql(expected)
    })

This test fails. Although effect and expected are very similar, they are not identical.

At least one of the differences is buried in payload.selector.scopes, where the yielded effect and expected are as follows:

screen shot

As the scopes of these two will always be different, how can these tests ever be made to work?

eta: this pattern is adapted from the example linked to from the redux-saga docs

like image 305
Will Jenkins Avatar asked Jun 22 '26 07:06

Will Jenkins


1 Answers

Cracked it after finding this issue from way back.

The fix is to create a named function to do the select and export it from the module where the saga under test lives, and then use this same function in tests. All is well.

export const selectAppConfig = state => state.appConfig

const getAccountDetails = function * () {
   const { url } = yield select(selectAppConfig)
   const accountDetails = yield call(apiFetchAccountDetails, url)
}
    import {selectAppConfig} from './sagaToTest'

    describe('getAccountDetails', () => {
        const iterator = getAccountDetails()
        it("should yield an Effect 'select(state=> state.appConfig)'", () => {
           const effect = iterator.next().value
           const expected = select(selectAppConfig)
           expect(effect).to.deep.eql(expected)
    })




like image 67
Will Jenkins Avatar answered Jun 26 '26 10:06

Will Jenkins



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!