【问题标题】:testing react component with enzyme测试与酶的反应成分
【发布时间】:2017-06-26 09:45:59
【问题描述】:

假设我有以下组件:

export default class CustomInput extends PureComponent {
  render () {
    return (
      <input type='text' value={this.props.value || ''} onChange={this.props.changeHandler} placeholder={this.props.placeholderValue} />
    )
  }
}

CustomInput.propTypes = {
  value: PropTypes.string,
  placeholderValue: PropTypes.string,
  changeHandler: PropTypes.func.isRequired
}

我尝试如下测试:

test('input renders correctly', () => {
    const handler = jest.fn()
    const display = shallow(<CustomInput value='foo' placeholderValue='bar' changeHandler={handler}/>)
    })

这失败了:

TypeError: Cannot read property 'contextTypes' of undefined

任何帮助将不胜感激!

【问题讨论】:

    标签: reactjs redux react-redux jestjs enzyme


    【解决方案1】:

    所以问题出在我的导入上。具体来说,像这样删除自动导入:

    import CustomInput from './index'

    而不是

    import {CustomInput} from './index'

    首先解释导致问题的原因将非常受欢迎

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    • 2017-05-20
    • 2017-09-24
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    相关资源
    最近更新 更多