Created
September 17, 2019 03:38
-
-
Save pratik14/0d2287d3dd4dc8ee81e0da2e38c107f9 to your computer and use it in GitHub Desktop.
Testing react Formik forms with react-testing-library
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 { Formik, Form, Field, ErrorMessage } from "formik"; | |
import DatePicker from "react-datepicker"; | |
import "react-datepicker/dist/react-datepicker.css"; | |
const App = () => ( | |
<div> | |
<Formik | |
initialValues={{ email: "", date: "" }} | |
validate={values => { | |
let errors = {}; | |
if (!values.date) { | |
errors.date = "Required"; | |
} | |
if (!values.email) { | |
errors.email = "Required"; | |
} | |
return errors; | |
}} | |
> | |
{({ setFieldValue, values }) => ( | |
<Form> | |
<label htmlFor="email">Email</label> | |
<Field id="email" type="email" name="email" /> | |
<ErrorMessage data-testid="emailError" name="email" component="div" /> | |
<br /> | |
<DatePicker | |
selected={values.date} | |
onChange={event => { | |
setFieldValue("date", event); | |
}} | |
/> | |
<ErrorMessage data-testid="dateError" name="date" component="div" /> | |
<br /> | |
<button type="submit">Submit</button> | |
</Form> | |
)} | |
</Formik> | |
</div> | |
); | |
export default App; |
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 "@testing-library/jest-dom/extend-expect"; | |
import { render, fireEvent, wait } from "@testing-library/react"; | |
import App from "./App"; | |
it("should show validation on blur", async () => { | |
const { getByLabelText, getByTestId } = render(<App />); | |
const input = getByLabelText("Email"); | |
fireEvent.blur(input); | |
await wait(() => { | |
expect(getByTestId("emailError")).not.toBe(null); | |
expect(getByTestId("emailError")).toHaveTextContent("Required"); | |
}); | |
}); | |
it("should show date validation on click on submit", async () => { | |
const { getByText, getByTestId } = render(<App />); | |
const button = getByText("Submit"); | |
fireEvent.click(button); | |
await wait(() => { | |
expect(getByTestId("dateError")).toHaveTextContent("Required"); | |
}); | |
}); | |
jest.mock("react-datepicker", () => props => ( | |
<input | |
data-testid="mockedDateField" | |
onChange={() => { | |
console.log(props.onChange); | |
props.onChange("asdfasd"); | |
}} | |
/> | |
)); | |
test("should remove date error id we select date", async () => { | |
const { getByText, getByTestId, queryByTestId } = render(<App />); | |
const button = getByText("Submit"); | |
fireEvent.click(button); | |
const mockedDateField = getByTestId("mockedDateField"); | |
fireEvent.change(mockedDateField, { target: { value: new Date() } }); | |
await wait(() => { | |
expect(queryByTestId("dateError")).toBe(null); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice, though I need more help how can I test a component with a formik component e.g :