I have a form submit event in React, then it redirect to a url using history
const handleFormSubmit = (e) => {
e.preventDefault();
if (query) history.push(query); //here is uncovered
};
<Form onSubmit={handleFormSubmit}>
I'm testing it using react-testing-library
it("should type input and redirect if submitted", async () => {
fireEvent.change(screen.getByPlaceholderText(/your name/i), {
target: { value: query },
});
await screen.findByDisplayValue(query);
});
it("should submit the search form", async () => {
fireEvent.click(screen.getByText(/submit/i));
expect(await history.location.pathname).toBe("/" + query);
});
The test is passing, but the coverage report in jest show history.pushState(query) is uncovered.
I guess it's because of the if statement?