【发布时间】:2020-01-21 09:10:43
【问题描述】:
我在我的 react native 项目中使用 jest,我想测试具有“onPress”的组件。当点击它时,会出现警报。
<ListItem
testID={'Contact'}
onPress={() => alert('hello')}
/>
并对其进行测试;
it('test onPress functionality', () => {
window.alert = jest.fn();
const wrapper = shallow(
<Contact
onPress={window.alert}
/>,
);
wrapper
.findWhere(node => node.prop('testID') === 'Contact')
.props()
.onPress();
expect(window.alert).toHaveBeenCalledWith('hello');
});
但是这个测试给出了警告错误。
ReferenceError: 未定义警报 onPress={() => alert('hello')}
请帮我解决这个错误。
【问题讨论】:
标签: reactjs unit-testing react-native jestjs enzyme