react-hook-form and testing

Solution retrospective
Testing
Originally I wanted to use Jest
to do testing, but then it turned out that it was too complicated to setup.
Here, I used screen.getByTestId
to get the element, using userEvent.type
, userEvant.click
from @testing-library/user-event
library to mock the keyboard input and the clicking event.
// ----------- // import { describe, it, expect } from "vitest" import { render, screen, waitfor } from "@testing-library/react" import App from "./App" describe("test form", () => { it("submit wrong email", async () => { render(); const emailInput = screen.getByTestId("email-input"); const submitBtn = screen.getByTestId("submit-btn"); await userEvent.type(emailInput, "asdasd"); await userEvent.click(submitBtn); await waitFor(() => { const errorMsg = screen.getByText("Please provide a valid email"); expect(errorMsg).toBeInTheDocument(); }); }); })
Please log in to post a comment
Log in with GitHubCommunity feedback
No feedback yet. Be the first to give feedback on Zup's solution.
Join our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord