Skip to content

FlashcardNot attempted

Testing async UI

Recall it before you reveal

How do you test a component that fetches data and then shows it?

Space to reveal
Show answer & explanation

1. Mock the network at the boundary (MSW handler returning the fixture). 2. render(<Users />) and assert the loading state. 3. expect(await screen.findByText(/ann/i)).toBeInTheDocument(): findBy retries until it appears. (A <li> gets no accessible name from its text, so findByRole('listitem', { name }) wouldn't match.) 4. Assert that the loading state is gone (queryBy → null). 5. Test the error path by overriding the handler to return a 500.

Avoid arbitrary setTimeout waits; "not wrapped in act()" warnings usually mean an update happened after the test stopped waiting.