functionrenderWithRedux(ui,{initialState,store=createStore(reducer,initialState)}={}){return{...render(<Providerstore={store}>{ui}</Provider>),
// adding `store` to the returned utilities to allow us// to reference it in our tests (just try to avoid using// this to test implementation details).store,}}// then, in a test:const{getByTestId,getByText}=renderWithRedux(<Counter/>)
Another dev (way more experienced than me) also mocks useSelector.
I would like to see the "official" way work, but so far I'm still stuck.
In my component, a useSelector that includes the callback definition works. However, any useSelector that requires an imported callback seems to remain undefined.
I am happy to report that my issue was a mistake in my definitions of my selector functions in my slice. Turns out I needed to type as RootState and then include the reducer in the return (i.e., state.system.chosenMappingService).
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
What do you mean by "did not allow that"?
RTL's docs specifically show how to use it with React-Redux:
Well to be honest I did add it to the render method, noticed that I got an error and took the other way out :)
Another dev (way more experienced than me) also mocks
useSelector
.I would like to see the "official" way work, but so far I'm still stuck.
In my component, a
useSelector
that includes the callback definition works. However, anyuseSelector
that requires an imported callback seems to remain undefined.This works:
This returns undefined:
And from my slice:
I remain confused.
I am happy to report that my issue was a mistake in my definitions of my selector functions in my slice. Turns out I needed to type as
RootState
and then include the reducer in the return (i.e.,state.system.chosenMappingService
).