reduxjs / redux-mock-store

A mock store for testing Redux async action creators and middleware.
MIT License
2.5k stars 148 forks source link

[Redux-toolkit] [createEntityAdapter] Cannot read property 'ids' of undefined #178

Open daminhtung opened 4 years ago

daminhtung commented 4 years ago

Hi, I got an error when running the test. Any help or suggestions for me?

Error when run testing

TypeError: Cannot read property 'ids' of undefined

  47 |   const items = useSelector((state: MyState) => {
> 48 |     return myEntityAdapter.getSelectors().selectAll(state.mystate)
        |                                         ^
  49 |   })
  50 | 

My test:

const { queryByTestId } = render(
      <Provider store={store}>
        <MenuProvider skipInstanceCheck={true}>
          <MyComponent />
        </MenuProvider>
      </Provider>
    )

mySlice.ts

...
export const myEntityAdapter = createEntityAdapter<Item>()
...
leosuncin commented 2 years ago

Pass an state to mockStore, like this:

import configureStore from 'redux-mock-store';

import type { RootState } from '../../app/store';

const mockStore = configureStore<RootState>();
const store = mockStore({ slice: { ids: [], entities: {} } });

// Rest of the test...