传递给createStore的preloadedState参数中找到意外的键

时间:2019-04-15 16:15:07

标签: javascript redux jestjs redux-thunk

我正在尝试编写Redux集成测试。我的测试成功通过,但是,我收到消息:

  

console.error node_modules / redux / lib / utils / warning.js:14       在preloadedState参数中找到意外的键“ word”,并将其传递给createStore。期望找到已知的reduce键之一:   “ jotto”,“ router”。意外的键将被忽略。

在我看来,我的createStore和root reducer看起来不错。我需要更改某些东西来弄乱此预加载状态吗?您可以在下面找到脚本。谢谢!

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),
});

1 个答案:

答案 0 :(得分:1)

尝试一下:

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