我使用react-dom/test-utils
來測試我的反應組件。react-dom/test-utils,TypeError:無法讀取未定義的屬性'children'
但scryRenderedDOMComponentsWithClass
返回值沒有props
和props.children
財產。我做錯了什麼?
這裏是我的測試代碼:
it('renders item count correctly',() => {
const $$searchList = TestUtils.renderIntoDocument(<SearchList items={datas}/>)
const list = TestUtils.scryRenderedDOMComponentsWithClass($$searchList, 'list');
expect(list.props.children).toHaveLength(4);
expect(items).toHaveLength(4);
});
這裏是測試結果:
● component - SearchList test suites › renders item count correctly
TypeError: Cannot read property 'children' of undefined
at Object.<anonymous> (src/components/List/__tests__/SearchList.test.jsx:17:26)
at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
at process._tickCallback (internal/process/next_tick.js:109:7)
component - SearchList test suites
✓ loads without error (43ms)
✕ renders item count correctly (18ms)
- 更新 -
這裏是我的組件渲染:
return (
<div className="list">
{
items.map((item: Book, idx: number): React.ReactElement<IListProps<Book>> => {
return (
<ListItem onClick={() => this.onItemClick(item, idx)} key={item.id} item={item}/>
);
})
}
</div>
);
我更新我的問題,仍然不知道該怎麼辦。 – novaline