javascript - 用酶将字母输入到笑话测试中
问题描述
我尝试了很多方法在 jest+enzyme 中将字母输入到我的输入元素中:
it('should change input text and state with it', () => {
const input = wrap.find('input')
expect(spy).toHaveBeenCalled()
//console.log(input.debug()) <input value="name" onChange={[Function]} />
input.simulate('change', {event: {target: {value: 'namea'}}})
//input.simulate('keydown', { which: 'a'})
expect(wrap.state('value')).toBe('namea')
//expect(input.props().value).toBe('namea')
})
//and the wrap
const wrap = mount(
<AddTodo addTodo={spy} />
) //still occurs with shallow
当我运行代码时,我收到错误:预期:'namea' 收到:'name' 我的组件是这样的: https ://github.com/conradkay/todo-app-jest-flow-router-react-redux /blob/master/src/addTodo/addTodo.jsx
解决方案
这是当您使用流以某种方式处理事件时发生的错误。要更正此错误,请使用:
handleChange = ({target}: SyntheticInputEvent<>) => {
this.setState({value: target.value})
}
and:
input.simulate('change', {target: {value: 'namea'}})
expect(wrap.state('value')).toBe('namea')
推荐阅读
- flutter - 后退按钮通向第一页
- c# - EF6 将连接字符串传递给 DbContext:System.ArgumentException:'不支持关键字:'provider'。
- wordpress - Woocommerce - 主页上的空产品列表
- swift - Vapor 如何通过电子邮件查找用户
- griddb - 尝试安装 Griddb 数据库时出现错误
- c# - 当 ContentPage 具有 SizeChanged 事件侦听器时,Xamarin.Forms Java.Lang.NullPointerException
- discord - 仅允许特定频道中的链接
- c++ - 从 `const char *` 隐式构造 `const std::string` 有效吗?
- c# - SSIS脚本C#脚本任务使用变量将当前日期添加到文件中
- python-3.x - 根据 odoo 中的拣货类型,在继承的交货/收据 qweb 报告上打印产品描述