我可以模拟alert 来测试它的alert 方法是否被调用,但我真正想要测试的是在alert 中按下ok按钮.
import { Alert } from 'react-native';
it('Mocking Alert', () => {
jest.mock('Alert', () => {
return {
alert: jest.fn()
}
};
});
const spy = jest.spyOn(Alert, 'alert');
const wrapper = shallow(<Search />);
wrapper.findWhere(n => n.props().title == 'Submit').simulate('Press');
expect(spy).toHaveBeenCalled(); //passes
})
但我完全不确定该如何测试.这是一个通用组件,我正试图测试它.
export default class Search extends Component{
state = {
someState: false
}
confirmSubmit(){
this.setState(state => ({someState: !state.someState}))
}
onPress = () => {
Alert.alert(
'Confirm',
'Are you sure?'
[{text: 'Ok', onPress: this.confirmSubmit}] //<-- want to test this
)
}
render(){
return(
<View>
<Button title='Submit' onPress={this.onPress}
</View>
)
}
}
有人试过吗?