测试反应 - 最终形式提交

时间:2018-06-15 16:58:48

标签: jestjs react-final-form

我已经开始从redux-form迁移到react-final-form以使我的bundle更小。我对表单进行了几次测试,其中一个是测试表单提交时调用了正确的操作。切换到react-final-form后,我的测试中的存储操作永远不会被调用。

当表单作为属性传递时,是否有办法测试提交函数。

我的测试:

  it('submits the form', () => {
    const wrapper = shallowUntilTarget(<LoginFormContainer store={store} />, 'form');
    wrapper.find('form').simulate('submit');

    expect(store.getActions()).toEqual(expect.arrayContaining([{ formObj: {}, type: 'PATIENT_LOGIN_REQUEST' }]));
  });

shallowUntilTarget通过容器

呈现实际表单

经测试的组件:

class LoginForm extends React.Component<Props> {
  submitForm = (values) => {
    this.props.dispatch(actions.loginPatient(values));
  };

  render() {
    return (
      <Form
        onSubmit={this.submitForm}
        render={({ handleSubmit }) => (
          <form onSubmit={handleSubmit} />

1 个答案:

答案 0 :(得分:1)

我无法使用redux形式测试验证,但实际上,以react-final形式进行操作要容易得多。表单未提交,验证失败时失败。我的LoginForm具有电子邮件和密码验证

<Form
 onSubmit={this.submitForm}
 validate={createValidator({
   email: [required, email],
   password: [required, minLength('8')],
 })}
 render={({ handleSubmit }) => (

可能有两个测试。一项测试失败和一项测试成功提交。两者都必须在已安装的组件上发生。

  it('does not submits invalid form ', () => {
    const wrapper = mount(<LoginFormContainer store={store} />);

    wrapper.find('form').simulate('submit');
    expect(store.getState().lastAction).not.toEqual({ payload: {}, type: 'PATIENT_LOGIN_REQUEST' });
  });

  it('submits valid form', () => {
    const wrapper = mount(<LoginFormContainer store={store} />);

    const email = wrapper.find('input[name="email"]');
    email.instance().value = 'cerny@seznam.cz';
    email.simulate('change', email);

    const password = wrapper.find('input[name="password"]');
    password.instance().value = '12345678';
    password.simulate('change', password);

    wrapper.find('form').simulate('submit');

    expect(store.getState().lastAction).toEqual({
      payload: { email: 'cerny@seznam.cz', password: '12345678' },
      type: 'PATIENT_LOGIN_REQUEST',
    });
  });
相关问题