"Cannot read property 'getState' of undefined" when creating redux mock store

liooshine

I am trying to test an input component in my React-Redux app and trying to create a mock of my redux store with 'redux-mock-store'.

When I try to run the test I get "Cannot read property 'getState' of undefined" error, so I guess I'm not initializing my mock store correctly but I don't know what I'm doing wrong.

import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import InputField from './InputField';
import configureStore from 'redux-mock-store';

describe("<InputField />", () => {
    const mockStore = configureStore([]);

//these are my two reducers as defined in my real redux store, so I'm passing them to the mock as well
    const chosenCityReducer = (chosenCity = null, action) => {
        if(action.type === 'CITY_CHOSEN') {
            return action.payload;
        }
        return chosenCity
    }
    const chosenCityWeatherReducer = (weather=null, action) => {
        if(action.type === 'WEATHER_FETCHED') {
            return action.payload;
        }
        return weather
    }
    let store;
    let component;

    beforeEach(() => {
        store = mockStore({
           chosenCity: chosenCityReducer,
           weatherForecast: chosenCityWeatherReducer
        });
    });

    let div = document.createElement('div')
    component = ReactDOM.render(
        <Provider store={store}>
            <InputField />
        </Provider>
    ,div);

    it('should render with given state from Redux store', () => {
        expect(component.toJSON()).toMatchSnapshot();
    });

Is there something wrong with the mock definition? Thank you!

ourmaninamsterdam

You're creating your component (<InputField/> wrapped in <Provider />) before the beforeEach hook has been called so mockStore hasn't been called yet so store will be undefined.

Try this:

let component;

beforeEach(() => {
  let div = document.createElement('div');
  const store = mockStore({
    chosenCity: chosenCityReducer,
    weatherForecast: chosenCityWeatherReducer
  });
  component = ReactDOM.render(
    <Provider store={store}>
      <InputField />
    </Provider>
  , div);
});

it('should render with given state from Redux store', () => {
  expect(component.toJSON()).toMatchSnapshot();
});

You can always move the store creation out of the beforeEach, if you like.

I usually have a function called renderSubject (which returns the rendered component) which I call within each test rather than using beforeEach. It reduces unnecessary mutable variables such as component being used between tests.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot read property 'getState' of undefined - Redux

Next redux wrapper arise error of Cannot read property 'getState' of undefined

TypeError: Cannot read property ‘getState’ of undefined in Redux Tookit

Cannot read property 'getState' of undefined error

Duplicate actions in redux-observable stream when used with redux-mock-store

Issue mocking a Redux Store with Jest/redux-mock-store

Testing redux connected components with jest and redux-mock-store

chai-redux-mock-store - ReferenceError: createMockStore is not defined

Jest: How to provide Redux mock store to only the child component?

React-redux rerenders when creating arrays

Redux store changes when reload page

Mocking Redux store when testing React components?

"No store found" when using Redux chrome extension

State is undefined when using the Redux store in React

Component not updating when redux store modified

Component not updating when Redux store is changed

General question - when to use redux store?

Where and when to populate redux store in react component

mapStateToProps not updating when redux store updates

When to switch from props to Redux store?

how to send redux store to the client when routing in redux

Creating strict mock when using @MockBean of spring boot?

Do we lie to ourselves when creating a mock for a unit test

When to add something to the redux store versus the component specific store when using React Redux

Why isn´t mock from __mock__ folder invoked when running test for redux action?

Redux mock store is not being updated on react test environment using Jest/RTL

Unit test with redux-mock-store - How can I make this unit test pass?

Creating a mock for a global

Creating mock instances of GIDGoogleUser