【发布时间】:2019-11-23 06:50:41
【问题描述】:
我有一个这样的组件:
export const MyList = props => {
const myCallbackFunction = () => {
// do stuff
};
const ListEmptyComponent = (
<MyCustomComponent
text="sample text"
onButtonPress={myCallbackFunction}
/>
);
return (
<FlatList
data={DATA}
renderItem={({ item }) => (<Item title={item.title} />)}
ListEmptyComponent={ListEmptyComponent} />
);
};
我想测试 ListEmptyComponent。在我的测试中,我尝试模拟 myCallbackFunction 并确保 ListEmptyComponent 等于 MyCustomComponent:
it("should display the MyCustomComponent", () => {
const myCallbackFunction = jest.fn();
const component = renderComponent();
expect(
component
.find(FlatList)
.prop("ListEmptyComponent")
).toEqual(
<MyCustomComponent
text="sample text"
onButtonPress={myCallbackFunction}
/>
);
});
测试失败,因为这是它所期望的:
onButtonPress={[Function mockConstructor]}
这就是它收到的内容:onButtonPress={[Function myCallbackFunction]}
我做错了什么?
【问题讨论】:
-
好像
myCallbackFunction组件使用不是你从props传递过来的,所以你可以考虑把组件改成使用onButtonPress={props.myCallbackFunction}
标签: javascript unit-testing react-native jestjs enzyme