簡體   English   中英

React 測試庫:TypeError:無法讀取未定義的屬性“文章”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM