我对本机项目使用笑话,我想测试具有“ 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')}] >>
请帮助我解决此错误。
我对本机项目使用笑话,我想测试具有“ onPress”的组件。单击它时,会出现警报。
您的测试不正确,因为您正在测试'onPress'作为参数。