首页 > 解决方案 > 用酶将字母输入到笑话测试中

问题描述

我尝试了很多方法在 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

标签: javascriptreactjsjestjsjsxenzyme

解决方案


这是当您使用流以某种方式处理事件时发生的错误。要更正此错误,请使用:

  handleChange = ({target}: SyntheticInputEvent<>) => {
    this.setState({value: target.value})
  }
  and:
  input.simulate('change', {target: {value: 'namea'}})
  expect(wrap.state('value')).toBe('namea')

推荐阅读