React testing use mockstore with custom store to test it

I would like to test components that are using redux store in my project. Here is the function that I use fo getting some store elements:

const variable = store.getState().serverConfiguration.testValue;

When I am testing this component I use mockStore to do that so my store is empty right? That is why I get error saying that TypeError: Cannot read property 'testValue' of undefined

Is there a way to pass my custom store to mockStore so that it will be possible to test it correctly? I tried to do that like this but it doesn't work:

const store = configureMockStore()({
    serverConfiguration: {
       testValue: "Admin"
    }
});

1 answer

  • answered 2021-01-16 12:27 Stijn2404

    Try this:

    import configureStore from "redux-mock-store"
    
    const mockStore = configureStore([]);
    
    const store = mockStore({
        serverConfiguration: {
            testValue: "Admin"
        }
    })