Jimmy
Jimmy

Reputation: 3880

Unexpected key found in preloadedState argument passed to createStore

I am trying to write a redux integration test. My test successfully passes, however, I get the message:

console.error node_modules/redux/lib/utils/warning.js:14 Unexpected key "word" found in preloadedState argument passed to createStore. Expected to find one of the known reducer keys instead: "jotto", "router". Unexpected keys will be ignored.

It seems to me that my createStore and root reducer look fine. Is there something I need to change that is messing up this preloaded state? You can find the scripts below. Thanks!

jottoRedux.test.js:

import {createStore, applyMiddleware} from 'redux';
import thunkMiddleware from 'redux-thunk';
import {routerMiddleware} from 'connected-react-router';
import rootReducer from 'reducers/rootReducer';
import {initialState} from './jottoReducer';
import {createBrowserHistory} from 'history';

export const history = createBrowserHistory();
const middleware = applyMiddleware(routerMiddleware(history), thunkMiddleware);

export const storeFactory = () =>
  createStore(rootReducer(createBrowserHistory()), {...initialState}, middleware);


export const setWord = (word) => ({
  type: 'SET_WORD',
  word,
});

describe('testing SET_WORD action', () => {
  let store;
  beforeEach(() => {
    store = storeFactory();
  });

  test('state is updated correctly for an unsuccessful guess', () => {
    store.dispatch(setWord('foo'));
    const expectedState = {
      ...initialState,
      word: 'foo',
    };
    const newState = store.getState().jotto;
    expect(newState).toEqual(expectedState);
  });
});

jottoReducer.js:

export const initialState = {
  word: null,
};

const jotto = (state = initialState, action) => {
  switch (action.type) {
    case 'SET_WORD':
      return {
        ...state,
        word: action.word,
      };
    default:
      return state;
  }
};

export default jotto;

rootReducer:

import {combineReducers} from 'redux';
import {connectRouter} from 'connected-react-router';
import jotto from './jottoReducer';

export default (historyObject) => combineReducers({
  jotto,
  router: connectRouter(historyObject),
});

Upvotes: 1

Views: 1592

Answers (1)

Hend El-Sahli
Hend El-Sahli

Reputation: 6752

Try this:

export const storeFactory = () =>
  createStore(rootReducer(createBrowserHistory()), { jotto: initialState }, middleware);

Upvotes: 1

Related Questions