gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:38:22 24 4
gpt4 key购买 nike

我正在尝试编写 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com