我想测试以下代码:
<React.Fragment>
<Connect />
<FlatList
data={[
{key: "pp", title: "Privacy Policy"},
{key: "tos", title: "Terms of Service"},
]}
renderItem={({item}) => {
return (
<View
onPress={() => this.handleOnPress(item.key)}
>
<Text>{item.title}</Text>
</View>
);
}}
/>
</React.Fragment>
以下是我的测试:
it("should render a FlatList with 2 items", () => {
const wrapper = shallow(
<Menu
/>
);
expect(wrapper).toMatchSnapshot();
expect(wrapper.props().data).toHaveLength(2);
});
由于某些原因,它失败了,并且显示.data
是未定义的。我主要想测试我的平面列表是否有2个项目。
发布于 2018-10-10 07:18:45
如果我理解这个问题,那么看起来您正在尝试获取<Menu/>
(包装器)的.props()
,而不是包装器的子级的<FlatList/>
。
通过使用.childAt
方法,您应该能够解决以下问题:
it("should render a FlatList with 2 items", () => {
const wrapper = shallow(
<Menu
/>
);
expect(wrapper).toMatchSnapshot();
// expect(wrapper.props().data).toHaveLength(2);
expect(
wrapper // the wrapper of 'Menu'
.childAt(1) // the second child of the 'Menu' which is the 'FlatList'
.props() // the props of 'FlatList'
.data // the data field of 'FlatList' props
).toHaveLength(2);
});
https://stackoverflow.com/questions/52730454
复制相似问题