I have a component that uses a hook that fetches data from the server, and I have mocked that hook to return my testing data.
Now if the mutate
function (returned by the hook) is called, the normal implementation fetches the data again and causes a re-render (I'm using swr
, here the mutate
reference).
How to I trigger a re-render / setState
on a mocked hook?
What I want to test: simply, if the user creates an item the item list should be re-fetched and displayed.
Code to illustrate the issue:
const existing = [...]
const newlyCreated = [...];
useData.mockReturnValue({ data: [existing] });
const { getByRole, findByText } = render(<MyComponent />);
const form = getByRole("form");
const createButton = within(form).getByText("Create");
useData.mockReturnValue({ data: [existing, newlyCreated] });
// createButton.click();
// Somehow trigger re-render???
for (const { name } of [existing, newlyCreated]) await findByText(name);