我要测试ErrorBoundry,我有这段代码
import React, { Component } from 'react'
class ErrorBoundary extends Component {
constructor (props) {
super(props)
this.state = { hasError: false }
}
componentDidCatch (error, info) {
this.setState({ hasError: true })
}
render () {
if (this.state.hasError) {
return <h1>Something went wrong.</h1>
}
return this.props.children
}
}
export default ErrorBoundary
我想测试,现在我已经测试了何时有错误,并且一切正常,但是我该如何在测试文件上写代码,我希望返回this.props.children()或模拟类似的东西?
我用酶
答案 0 :(得分:1)
您尝试过吗? .simulateError(error) => Self
https://airbnb.io/enzyme/docs/api/ShallowWrapper/simulateError.html
答案 1 :(得分:0)
要测试非错误情况,只需测试true
呈现其子代即可:
ErrorBoundary