简体   繁体   English

在反应中模拟提交最终形式

[英]Mocking Submit of final-form in react

I am attempting to test that a callback function is being called by submitting a form.我正在尝试通过提交表单来测试是否正在调用回调函数。 I have mocked an onSubmit function which is passed into react-final-form.我模拟了一个传递到 react-final-form 的 onSubmit 函数。 As shown in the codesandbox and below, I've got a simple form with an onSubmit callback.如代码和框所示,我有一个带有 onSubmit 回调的简单表单。

export const MyForm = ({ onSubmit }) => (
  <Form
    onSubmit={onSubmit}
    render={({ handleSubmit }) => (
      <form onSubmit={handleSubmit} autoComplete="off">
        <Field
          label="Email"
          component={Input}
          name="email"
          type="email"
          autoComplete="off"
        />
        <button>Submit</button>
      </form>
    )}
  />
);

When I simulate a click event on the button, I expect it to call the mocked function.当我在按钮上模拟点击事件时,我希望它调用模拟函数。


  it("should call onSubmit when the button is click", () => {
    const button = wrapper.find("button");
    expect(button.length).toBeGreaterThan(0);
    button.at(0).simulate("click");
    expect(mockSubmit).toHaveBeenCalled();
  });

Any assistance would be greatly appreciated.任何帮助将不胜感激。

Codesandbox 代码沙盒

You will need to simulate submit in order to submit the form.您需要模拟submit才能提交表单。

As for the Warning: An update to ReactFinalForm inside a test was not wrapped in act(...).至于Warning: An update to ReactFinalForm inside a test was not wrapped in act(...). , you are using a promise in your submit handler in the test which causes the form validation, submit, and state updates to be async. ,您在测试中的提交处理程序中使用了 promise,这会导致表单验证、提交和状态更新异步。

act() provides a scope around expected component updates, and you will get this warning when an component does something outside of this scope. act()提供了围绕预期组件更新的范围,当组件执行此范围之外的操作时,您将收到此警告。 Since in the test the submit handler is async, the updates will happen outside of the act() function and will give you this error.由于在测试中提交处理程序是异步的,更新将发生在act()函数之外,并会给你这个错误。

There are two ways to fix this, make the submit handler sync via jest.fn() .有两种方法可以解决这个问题,通过jest.fn()使提交处理程序同步。

const mockSubmit = jest.fn();

If you need to keep this async, you will need to act/await over a the submit promise.如果您需要保持异步,则需要对提交承诺采取行动/等待。 This would mean you would need to create a resolved promise value and have a mock function resolve it.这意味着您需要创建一个已解析的承诺值并让模拟函数解析它。

  const promise = Promise.resolve();
  const mockSubmit = jest.fn(() => promise);

  beforeEach(() => {
    wrapper = mount(<MyForm onSubmit={mockSubmit} />);
  });

  it("should call onSubmit when the button is click 2", async () => {
    const button = wrapper.find("form");
    expect(button.length).toBeGreaterThan(0);
    button.at(0).simulate("submit");
    expect(mockSubmit).toHaveBeenCalled();

    await act(() => promise);
  });

我的首选方法是使用<button type="submit">Submit</button>然后fireEvent.click(getByText('Submit'))就像这样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM