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
onSubmitfunction while bypassing/ leveraging thevalidatefunctionality? - 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'))
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>
)
/>

@testing-library/react, so maybe you can look into that source for some insights?validateprop. 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 mockedonSubmit@ErikR.validatefunction. Maybe can you show me a more concrete example that's failing? I'm not understanding.validatemethod 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 onRFF&FF🙏🏻