In my React application, I have a button on the screen which after clicking on it, a specific element in the screen will disappear. By Clicking the button, a state will change and after re-rendering the component, the element will disappear.
To test this scenario, I am using the React Testing library. It does not work truly by waitForElementToBeRemoved :
//element to hide
const listContainer = screen.getByRole('list');
//Toggle button
const queryButton = screen.getByRole('button', {
name: 'Query',
})
userEvent.click(queryButton);
//this line get error: Exceeded timeout of 20000 ms for a test.
await waitForElementToBeRemoved(() => listContainer);
but when I change waiting the process it works:
await waitFor(() => {
expect(listContainer).not.toBeInTheDocument();
});
I don't understand why. can anyone help me?