Yeda AI Tips · #071

Español

Assert All Four UI States

You're only testing one of your UI's four states. Almost every screen that talks to a network or a database moves through the same lifecycle: it loads, it may come back empty, it may fail, and — if everything goes right — it renders success. Most widget and component tests assert exactly one of those four: the happy render. That's 25% coverage of the states your users actually see.

Why the happy path isn't the whole app

The success render is the state you look at all day during development, so it's the one that rarely breaks. The bugs users hit live elsewhere:

None of these are exotic. All of them ship constantly, because no test ever put the component into that state.

The four assertions

StateWhat to assertHow to reach it in a test
LoadingSpinner/skeleton is visible; content is notRender before the mocked request resolves
EmptyThe empty-state message and its CTA renderMock the request to return [] / no items
ErrorError message renders; retry control existsMock the request to return a 500 or a network error
SuccessData renders; spinner and error are goneMock the request to return realistic fixture data

Four states, four tests. Each one is a few lines once the request mocking is in place.

What it looks like in code

With React Testing Library and MSW, the error case is a one-handler override:

test("handles server error", async () => {
  server.use(
    http.get("/api/items", () =>
      HttpResponse.json({ error: "boom" }, { status: 500 })
    )
  );
  render(<ItemList />);
  expect(await screen.findByText(/something went wrong/i)).toBeInTheDocument();
  expect(screen.queryByRole("progressbar")).not.toBeInTheDocument();
});

Two details do the heavy lifting:

The same shape works everywhere: in Flutter, tester.pumpWidget() plus find.byType(CircularProgressIndicator) for loading, then pump past the future and assert the error Text; in any stack, the pattern is control the data source, assert the render.

Power-user moves

Resources

Building an AI feature? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog