用酵素在笑话测试中输入字母

时间:2018-08-15 16:59:59

标签: javascript reactjs jestjs jsx enzyme

我尝试了多种方法在jest + zyme中将字母输入到我的输入元素中:

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

1 个答案:

答案 0 :(得分:0)

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

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