[英]React Testing library: TypeError: Cannot read property 'articles' of undefined
I'm trying to test my Container using react-testing-library.我正在尝试使用 react-testing-library 测试我的容器。
const middlewares = [thunk.withExtraArgument({})];
const mockStore = configureMockStore(middlewares);
const storeState = {
articles:
[
{
id: "9b565b11-7311-5b5e-a699-97873dffb364",
title: "jsdhahd",
body: "jsahdjadshajhd",
link: "https://www.google.com",
media: "media.jpg"
},
],
loading: false,
error: ''
};
describe('<Homepage />', () => {
let store;
beforeEach(() => {
store = mockStore(storeState);
});
afterEach(() => {
store.clearActions();
});
it('should render correctly', () => {
const container = render(<Provider store={store}>{<Homepage />}</Provider>);
expect(container).toMatchSnapshot();
});
And I get this error: error我得到这个错误:错误
Can anyone help?任何人都可以帮忙吗? It seems a problem with mocking the redux store, but I don't know exactly how to solve it.
嘲笑 redux 商店似乎是一个问题,但我不知道如何解决它。 I have in the reducer a initial state with the articles, loading, and error.
我在减速器中有文章、加载和错误的初始状态。 And in my App.js file I'm wrapping the
<App/>
with the <Provider store={store}>
在我的 App.js 文件中,我用
<Provider store={store}>
包装了<App/>
<Provider store={store}>
export const store = createStore(
rootReducer,
applyMiddleware(thunk)
)
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>, document.getElementById('root')
);
The error was on the way I was creating the storeState
object.错误发生在我创建
storeState
对象的过程中。
Correct way:正确做法:
const storeState = {
reducer: {
articles:
[
{
id: "9b565b11-7311-5b5e-a699-97873dffb364",
title: "jsdhahd",
body: "jsahdjadshajhd",
link: "https://www.google.com",
media: "media.jpg"
},
],
loading: false,
error: ''
};
我认为它应该是state.articles
而不是state.reducer.articles
中的mapStateToProps
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.