Reactjs 测试反应最终形式更改事件

Reactjs 测试反应最终形式更改事件,reactjs,enzyme,react-final-form,Reactjs,Enzyme,React Final Form,有人能告诉我如何测试这个简单的行为吗?我已经尝试解决这个问题很多时间了,我希望我只是遗漏了一些东西,因为一个变更事件应该很容易测试 我尝试调用onChange而不是simulate,wrapper.update(),等待新的承诺(resolve=>setTimeout(resolve))没有结果 import * as React from 'react'; import { Form, Field } from 'react-final-form'; import { mount } from

有人能告诉我如何测试这个简单的行为吗?我已经尝试解决这个问题很多时间了,我希望我只是遗漏了一些东西,因为一个变更事件应该很容易测试

我尝试调用
onChange
而不是
simulate
wrapper.update()
等待新的承诺(resolve=>setTimeout(resolve))
没有结果

import * as React from 'react';
import { Form, Field } from 'react-final-form';
import { mount } from 'enzyme';


it('change event', () => {
  const wrapper = mount(
    <Form onSubmit={jest.fn()}>
    {() => (
      <Field name="name">
      {({input}) => <input value={input.value} onChange={input.onChange} />}
      </Field>
    )}
    </Form>
  )

  expect(wrapper.find('input').prop('value')).toEqual('');
  wrapper.find('input').simulate('change', { target: { value: 'blue cheese' } } );
  expect(wrapper.find('input').prop('value')).toEqual('blue cheese');
});

我认为您可能需要在输入中添加一个ref,并直接在其上模拟事件:

it('change event', () => {
  const wrapper = mount(
    <Form onSubmit={jest.fn()}>
    {() => (
      <Field name="name">
      {({input}) => <input ref='myinput' value={input.value} onChange={input.onChange} />}
      </Field>
    )}
    </Form>
  )
  wrapper.ref('myinput').simulate('change', { target: { value: 'blue cheese' } } );
}
it('change event',()=>{
常量包装器=装入(
{() => (
{({input})=>}
)}
)
ref('myinput').simulate('change',{target:{value:'bluecheese'});
}
编辑:

但是,您需要确保input.onChange函数实际使用事件值并最终更改input.value(可能通过setState)

有关类似问题,请参见此处:


我对酶知之甚少。该库本身是使用(我强烈推荐)进行测试的,而
的变化是这样的。

为什么你要为一个已经成熟的npm模块编写测试,而且,
it('change event', () => {
  const wrapper = mount(
    <Form onSubmit={jest.fn()}>
    {() => (
      <Field name="name">
      {({input}) => <input ref='myinput' value={input.value} onChange={input.onChange} />}
      </Field>
    )}
    </Form>
  )
  wrapper.ref('myinput').simulate('change', { target: { value: 'blue cheese' } } );
}