1

I having this component

const Testcomponent = (props:any) => {    const {
    name,
    age   } = useSelector(
    (state: any) => ({
      name: state.user.name,
      age: state.user.age
    }),
    shallowEqual   );
return (<div>{name}-{age}</div>)
}

I tried to test , import * as redux from 'react-redux'

const useSelectorState =  {
    state:{
        user:{
            name:"test",
            age:11
        }}}

const spy = jest.spyOn(redux, 'useSelector')
spy.mockImplementation(cb => cb(useSelectorState))

This is not covering the useselector in Coverage report .

Arun Lazer
  • 33
  • 5

1 Answers1

0

I fixed the issue the thing is I missed to create a mockstore using the state cause selector will take state from the store so we need to set store with the same state which we passing in useselector

const useSelectorState =  {
    state:{
        user:{
            name:"test",
            age:11
        }}}
const mockStore = configureStore(useSelectorState)

const spy = jest.spyOn(redux, 'useSelector')
spy.mockImplementation(cb => cb(useSelectorState))
Arun Lazer
  • 33
  • 5