Reactjs 如何为单元测试获取嵌套props函数的值
我在获取嵌套道具函数的值时遇到问题Reactjs 如何为单元测试获取嵌套props函数的值,reactjs,unit-testing,jestjs,enzyme,Reactjs,Unit Testing,Jestjs,Enzyme,我在获取嵌套道具函数的值时遇到问题 this.props.user.getPerson().getData() getPerson返回包含人员相关数据的新对象 getData返回与人员相关的数据 它的可读性是分开的,我想让它用于单元测试 测试1: let _wrapper, initialProps beforeEach(() => { initialProps = { user: { getPerson: () => {}
this.props.user.getPerson().getData()
getPerson
返回包含人员相关数据的新对象
getData
返回与人员相关的数据
它的可读性是分开的,我想让它用于单元测试
测试1:
let _wrapper,
initialProps
beforeEach(() => {
initialProps = {
user: {
getPerson: () => {}
}
}
_wrapper = shallow(<Test1 {...initialProps} />)
})
some tests...
})
它返回了TypeError:无法读取未定义的属性“getData”
与Test1相同的错误
我试图通过传递函数来获得道具函数的值,但第二个函数并没有起作用
如何获取嵌套道具函数的值 您需要:
@Aayumi太棒了,很高兴它很有用!
let _wrapper,
initialProps
beforeEach(() => {
initialProps = {
user: {
getPerson: () => { getData: () => {} }
}
}
_wrapper = shallow(<Test2 {...initialProps} />)
})
some tests...
})
initialProps = {
user: {
getPerson: () => ({ getData: () => { } }) // wrap in parentheses
}
}