Reactjs Jest测试失败,未定义警报
我将jest用于我的react原生项目,我想测试具有onPress的组件。当点击它时,警报就会出现Reactjs Jest测试失败,未定义警报,reactjs,unit-testing,react-native,jestjs,enzyme,Reactjs,Unit Testing,React Native,Jestjs,Enzyme,我将jest用于我的react原生项目,我想测试具有onPress的组件。当点击它时,警报就会出现 <ListItem testID={'Contact'} onPress={() => alert('hello')} /> 并对其进行测试 it('test onPress functionality', () => { window.alert = jest.fn(); const wrapper = shallow(
<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:未在Press={=>alert'hello'}上定义警报
请帮助我修复此错误。您的测试不正确,因为您测试的参数是“onPress”
expect(window.alert).toHaveBeenCalledWith('hello');
上述操作应该有效。。请使用react native Alert
import {Alert} from 'react-native';
<ListItem
testID={'Contact'}
onPress={() => Alert.alert('hello')}
/>
检查文档多亏了修复,但错误与此无关,现在我修复了,仍然会出现相同的错误。