1

I have a form built using react-final-form, yup, and Material-ui. I am testing using Jest, and @testing-library/react.

TL;DR:

  • Is there a way to mock and test only the onSubmit function while bypassing/ leveraging the validate functionality?
  • Is there a better way to go about it?

The mocked onSubmit function was not called because I leveraged RFF's validate function. It seems the validate function is hanging during the submit process. Besides mocking the validate function, is there a best practice in building the validator function so the tests recognize that the mocked onSubmit has been called?

When I try to test whether the form has been submitted it seems to hang on the validate method.

fireEvent.submit(getByTestId('test-form')) // or
fireEvent.click(getByTestId('submit-button'))

mocking function failure

The mocked onSubmit function is not recognized and seems to hang on the validate method.

const schema = yup.object().shape({
    code: yup
        .string()
        .trim()
        .required('Please provide a Code')
        .max(32, 'The Code is too long')
})

const validate = values =>
    schema
        .validate(values, { abortEarly: false })
        .then(valid => ({}))
        .catch(err => extractError(err))

<Form
  onSubmit={onSubmit}
  validate={validate}
  render={({ handleSubmit }) => (
    <form onSubmit={handleSubmit}>
      {...fields and submit button here}
    </form>
  )
/>

Edit flamboyant-tesla-qozho

5
  • 2
    I'd need to see the code for your entire test, I think. Ideally the smallest version that fails. All of RFF's internal testing is done with @testing-library/react, so maybe you can look into that source for some insights? Commented Jan 24, 2020 at 8:43
  • Thanks for the response! I have been using RFF's internal testing files as a reference point--which makes this feel even more bizarre. Commented Jan 24, 2020 at 17:39
  • I identified that the core issue was not mocking the validate prop. Is there a best practice way to test the full integration of a form without having to mock the validate function? I was looking at Final-Forms validation testing but can't seem to find an example that includes testing the mocked onSubmit @ErikR. Commented Jan 24, 2020 at 21:18
  • 1
    You don't need to provide a validate function. Maybe can you show me a more concrete example that's failing? I'm not understanding. Commented Jan 27, 2020 at 15:45
  • @ErikR. The codesandbox should have the updated example where the test fails. Ultimately, I would like to test the whole behavior of the form, including the validate method and for it to pass. Is there a way to accomplish the whole form passing the test? Or do you recommend to test the behaviors separately? --also thank you for all your work on RFF & FF 🙏🏻 Commented Jan 27, 2020 at 18:17

1 Answer 1

1

I was able to pinpoint the source of the problem. I was leveraging the validate prop without mocking it in my tests. I solved this issue by mocking/ testing the validate function separately. Big thanks to @ErikR for the assistance 🙏🏻

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.