【发布时间】:2019-05-12 18:41:55
【问题描述】:
使用 Jest 和 Enzyme,如何测试 this.props.functionToTest 是否运行?
class TestComponent extends Component {
static propTypes = {
functionToTest: PropTypes.func
}
componentDidMount() {
this.props.functionToTest()
}
}
在 Jest 中,我尝试创建 mockProps 并在安装组件时将它们传入。
let props = {
functionToTest = jest.fn(() => {});
}
beforeEach(() => {
const wrapper = mount(<TestComponent {...props} />
}
componentDidMount 函数中的 console.log 将 functionToTest 显示为未定义。显然在 mount 时传入 props 是行不通的。
问题 1:如何传入将在 componentDidMount 函数中显示的模拟道具?
问题 2:一旦该函数可用,我如何获得对该函数的访问权限,以便我可以使用 spyOn 或类似的东西来测试该函数是否已运行?
【问题讨论】:
标签: javascript reactjs jestjs enzyme