![](/img/trans.png)
[英]Trouble adding react-testing-library to a project - TypeError: Cannot read property 'then' of undefined
[英]React Testing library: TypeError: Cannot read property 'articles' of undefined
我正在嘗試使用 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();
});
我得到這個錯誤:錯誤
任何人都可以幫忙嗎? 嘲笑 redux 商店似乎是一個問題,但我不知道如何解決它。 我在減速器中有文章、加載和錯誤的初始狀態。 在我的 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')
);
錯誤發生在我創建storeState
對象的過程中。
正確做法:
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.