【发布时间】:2017-07-12 04:49:10
【问题描述】:
我使用react-dom/test-utils 来测试我的反应组件。
但是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>
);
【问题讨论】: