This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import { screen, render, fireEvent } from "@testing-library/react"; | |
import InputField from "./InputField"; | |
import { Formik } from "formik"; | |
test("should have validation error given input field is touched and error exists on form", async () => { | |
const fieldName = "firstName"; | |
const labelName = "First Name"; | |
render( | |
<Formik | |
validate={(values) => { | |
let errors = {}; | |
if (!values?.firstName) { | |
errors.firstName = "Required."; | |
} | |
return errors; | |
}} | |
> | |
<InputField fieldName={fieldName} labelName={labelName} /> | |
</Formik> | |
); | |
const input = screen.getByLabelText(labelName); | |
// Call blur without inputting anything which should trigger a validation error | |
fireEvent.blur(input); | |
const validationErrors = await screen.findByTestId(`errors-${fieldName}`); | |
expect(validationErrors.innerHTML).toBe("Required."); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment