我正在尝试在我的 React-Redux 应用程序中测试一个输入组件,并尝试使用“redux-mock-store”创建我的 redux 商店的模拟。
当我尝试运行测试时,我得到“无法读取未定义的属性'getState'”错误,所以我想我没有正确初始化我的模拟存储,但我不知道我做错了什么。
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();
});
模拟定义有问题吗?谢谢!