question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error on expectSaga.provide selector (state is undefined)

See original GitHub issue

Hey there! I’m implementing redux-saga-test-plan on an existent project, and I’m having some troubles to use expectSaga.provide with a specific kind of selector, and I do believe that this has been caused by some bug on the provide at the moment of recognizing and mocking my selector.

I have something like that:

// Selector
const selectFruit = name => state => state.fruits[name]

// Saga
function* onGetFruit({ payload: { name } }) {
  const myFruit = yield select(selectFruit(name))

  if (myFruit === 'apple') {
    const apple = {
      edible: true, 
      color: 'red',
    }
    yield put(eatTheFuit(apple))
  }
}

// Test
it('should find an apple and dispatch an eat action', () => {
  return expectSaga(onGetFruit, {
    payload: { name: 'apple' },
  })
    .provide([
      [select(selectFruit('apple')), {
        name: 'apple',
      }],
    ])
    .put({
      type: 'EAT_THE_FRUIT', payload: { 
        edible: true, 
        color: 'red',
      },
    })
    .run()
})

So, with this scenario I’m always getting an error like that:

  TypeError: Cannot read property 'fruits' of undefined
  > 2 | const selectFruit = name => state => state.fruits[name]
      |                                            ^

I’ve noticed that this doesn’t happen for some selector similar to that, where I use some kind of memoize, like:

const memoizedSelectFruit = memoize(name => state => state.fruits[name])

I hope someone could help me with that! And thanks @jfairbank for this amazing package 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

3reactions
diegofelipececommented, Jan 15, 2020

I’ve “worked around” this question by changing those selectors to pass the argument as a second parameter of the function, like it’s suggested on redux-saga docs.

// So instead of:
const selectFruit = name => state => state.fruits[name]

// I have now:
const selectFruit = (state, name)  => state.fruits[name]

This way I can pass a second parameter to select, specifying the id, name or whatever. To clarify, I udated the same first example:

// Selector
const selectFruit = (state, name) => state.fruits[name]

// Saga
function* onGetFruit({ payload: { name } }) {
  const myFruit = yield select(selectFruit, name)

  if (myFruit === 'apple') {
    const apple = {
      edible: true, 
      color: 'red',
    }
    yield put(eatTheFruit(apple))
  }
}

// Test
it('should find an apple and dispatch an eat action', () => {
  return expectSaga(onGetFruit, {
    payload: { name: 'apple' },
  })
    .provide([
      [select(selectFruit, 'apple'), {
        name: 'apple',
      }],
    ])
    .put({
      type: 'EAT_THE_FRUIT', payload: { 
        edible: true, 
        color: 'red',
      },
    })
    .run()
})
0reactions
AaronVcommented, Nov 4, 2022

I was able to test a curried selector-function by using matchers instead of select directly. I guess in your example it would be like this…

import * as matchers from 'redux-saga-test-plan/matchers';

...

.provide([
  [matchers.select.like(selectFruit('apple')), { name: 'apple' }],
])
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can not provide/test saga when using a reselect selector
The error I get is: It looks as though the reselect selector function is running and trying to access the global state from...
Read more >
Dynamic Providers · GitBook - Redux Saga Test Plan
You can use completely dynamic providers by passing in an object literal to the provide method. The object literal argument must contain effect...
Read more >
Testing Sagas - Redux-Saga
The redux-saga-test library provides syntactic sugar for your step-by-step tests. ... The testSaga API is used for exact order testing and expectSaga is...
Read more >
Source - GitHub
Bug Fixes * (#183) You can now provide mock values for nested forks and spawns. ... ```js it('tests final store state', () =>...
Read more >
React Redux - Undefined issues when following the Quick start
My problem is that I get an undefined error as follows: TypeError: state.user_display is undefined > 8 | const user = useSelector((state) ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found