【发布时间】:2018-06-20 23:12:46
【问题描述】:
在尝试编写测试时,如果我从子级调用父函数,它会返回
props.onPress() 不是函数
我假设我需要在测试中手动将道具传递给 on press。但我觉得这可能会破坏测试代码结构中现有内容的目的。
家长
export default class Parent extends React.Component {
constructor(){
super()
}
_method(){
return "Something"
}
render(){
return{
<Child onPress={this._method.bind(this) }
}
}
}
儿童
export default (Child = (props) => {
const _childMethod = () => {
return props.onPress()
}
return{
<View>
<TouchableOpacity onPress={() => { return _childMethod() }}>
</TouchableOpacity>
</View>
}
})
测试
import "react-native"
import React from "react"
import Enzyme, {shallow, render} from "enzyme"
import Parent from "path/to/parent"
import Child from "path/to/child"
Enzyme.configure({adapter: new Adapter()})
const wrapper = shallow(<Child />)
it("Should return something", () => {
console.log(wrapper.props().onPress())
})
【问题讨论】:
标签: react-native jestjs enzyme