Reactjs 单击测试组件内部方法
我有一个简单的代码,想看看,若按下按钮,onPress是否会被调用。这一切都不需要酶,因为它对react 16不起作用Reactjs 单击测试组件内部方法,reactjs,react-native,jestjs,Reactjs,React Native,Jestjs,我有一个简单的代码,想看看,若按下按钮,onPress是否会被调用。这一切都不需要酶,因为它对react 16不起作用 export default class SimpleThing extends Component { onPress = () => { console.log('Will it get called?') } render() { return ( <View style={s
export default class SimpleThing extends Component {
onPress = () => {
console.log('Will it get called?')
}
render() {
return (
<View style={styles.container}>
<Button onPress={this.onPress} />
</View>
)
}
}
导出默认类SimpleThing扩展组件{
onPress=()=>{
console.log('会调用它吗?')
}
render(){
返回(
)
}
}
测试是这样的,但它不起作用
describe('SimpleThing', () => {
it('calls onPress when Button is pressed', () => {
const component = renderer.create(
<SimpleThing />,
)
const instance = component.getInstance()
const spy = jest.spyOn(instance, 'onPress')
expect(spy).not.toHaveBeenCalled()
const tree = component.toJSON()
tree.children[0].props.onPress()
expect(spy).toHaveBeenCalled()
})
})
description('SimpleThing',()=>{
它('按下按钮时调用onPress',()=>{
const component=renderer.create(
,
)
const instance=component.getInstance()
const spy=jest.spyOn(实例'onPress')
expect(spy).not.toHaveBeenCalled()
const tree=component.toJSON()
tree.children[0].props.onPress()
expect(spy).tohavebeincalled()
})
})
怎么能开玩笑呢?还尝试用jest.fn()替换onPress,但对component.toJSON()没有影响,调用了错误的函数