Skip to content

Instantly share code, notes, and snippets.

@scottsauber
Last active January 1, 2022 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottsauber/02fd249d31aa7f00ce779d36da5cdb2a to your computer and use it in GitHub Desktop.
Save scottsauber/02fd249d31aa7f00ce779d36da5cdb2a to your computer and use it in GitHub Desktop.
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