Reputation: 53
I'm trying to implement my first tests in react with react-test-library, but I came across this certain problem where there's warning that my component is not wrapped in act(..)
down below is the test that I'm trying to implement
import { BrowserRouter as Router } from "react-router-dom";
beforeEach(() => {
container = render(
<Router>
<Search />
</Router>
);
});
it("handleClick", async () => {
const button = container.getByText("Search");
const event = fireEvent.click(button);
expect(event).toBeTruthy();
});
and Here is the function that I'm trying to test
const handleClick = async () => {
setLoading(true);
const data = await movieAPI.fetchMovieByTitle(movie);
setLoading(false);
navigate(`/movie/${data.Title}`, { state: data });
};
Upvotes: 2
Views: 536
Reputation: 53
Turns out that before doing the assertions we need to wait for the component update to fully complete with waitFor
it("should render spinner", async () => {
const button = container.getByText("Search");
const event = await fireEvent.click(button);
const spinner = container.getByTestId("spinner");
await waitFor(() => {
expect(spinner).toBeInTheDocument();
});
});
Upvotes: 1