Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test selectors in saga that take action values as props? #4

Closed
billjohnston opened this issue Jul 1, 2017 · 2 comments
Closed

Comments

@billjohnston
Copy link

Having issues testing a selector that takes other arguments besides state

export function* exampleSaga(action) {
    const selectVar = yield select(state => exampleSelector(state, { id: action.id }))
    ...
}
describe('exampleSaga', () => {
    let it = sagaHelper(exampleSaga(mockAction))
    it('should yield x', (result) => {
        expect(result).toEqual(
            select(state => exampleSelector(state, { id: mockAction.id }))
        )
        return 'x'
    })
})

I've also tried saving the selector as a separate variable:

export const exampleSelectorFn = (action) => (state) => (
    shouldChangeRouteSelector(state, { action: action.id })
)
export function* exampleSaga(action) {
    const selectVar = yield select(exampleSelectorFn(action))
    ...
}
describe('exampleSaga', () => {
    let it = sagaHelper(exampleSaga(mockAction))
    it('should yield x', (result) => {
        expect(result).toEqual(
            select(exampleSelectorFn(mockAction))
        )
        return 'x'
    })
})

Both returns this (using jest):

Expected value to equal:
    {"@@redux-saga/IO": true, "SELECT": {"args": Array [], "selector": [Function anonymous]}}
Received:
    {"@@redux-saga/IO": true, "SELECT": {"args": Array [], "selector": [Function anonymous]}}

Difference:

Compared values have no visual difference.
@antoinejaussoin
Copy link
Owner

antoinejaussoin commented Jul 5, 2017

You shouldn't call selectors directly within select: instead of

const selectVar = yield select(state => exampleSelector(state, { id: action.id }))

use

const selectVar = yield select(exampleSelector, { id: action.id })

This will have the same result (the second parameter will be passed by redux-saga to your selector), but you can test test easily that the select function was called using the exampleSelector function and the other parameter.

@billjohnston
Copy link
Author

This works, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants