测试与酶反应的成分 [英] testing react component with enzyme

查看:83
本文介绍了测试与酶反应的成分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有以下组件:

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

的属性'contextTypes'

Any help would be much appreciated!

推荐答案

因此,事实证明问题出在我的导入上。具体来说,像这样删除自动导入:

So it turns out that the problem was with my import. Specifically, removing the autoimport like so:

从'./index'

而不是

从'./index'导入{CustomInput}

首先要对导致问题的原因进行解释

An explanation of what caused the problem in the first place would be more than welcome

这篇关于测试与酶反应的成分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆