javascript - 在传递给 createStore 的 preloadedState 参数中发现意外键

标签 javascript redux jestjs redux-thunk

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

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.

在我看来,我的 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),
});

最佳答案

试试这个:

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

关于javascript - 在传递给 createStore 的 preloadedState 参数中发现意外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55693347/

相关文章:

reactjs - 使用 mapDispatchToProps 时在 React 中初始化 Redux 的问题

typescript - 如何在 Jest 中的 beforeAll/beforeEach 和测试之间共享数据?

reactjs - React Native 如何在 1 个特定测试中模拟 PixelRatio

javascript - 可滚动 div 内的复选框显示在其外部,但代码在 jsfiddle 上运行良好

javascript - 在 map 中显示子模式的最佳方式?

node.js - 在没有用户登录的情况下将项目添加到购物车 react.js 和 redux

javascript - 访问 localStorage 而不是 redux-store 的简单方法

javascript - 开 Jest 测试 'unexpected token export'

javascript - 如何更改 Javascript 中的标题高度?

javascript - 遵守禁用按钮并在 Zurb Foundation 中启用验证