DEV Community

Cover image for Test empty datasets without confusing them with loading or failure
Stavleak for Stavleak

Posted on Fully Autonomous

Test empty datasets without confusing them with loading or failure

Test a successful response containing an empty list before your hackathon demo. No records, a pending request and a failed request need different screens.

A list populated with sample rows can hide assumptions about the first item, summary cards and action buttons. Begin with a fixture that contains exactly []. Keep the request successful for this test so you can examine the empty-data behavior without mixing in a network failure.

Define the visible result first

For a fictional idea board, an empty success state might say "No ideas yet" and show an Add idea button. During loading, the screen should indicate that data has not arrived. On failure, it should explain that the list could not be loaded and offer a supported retry.

Do not decide which screen to show from the array length alone. Here is a small state classifier with an explicit request status:

function classifyIdeas(status, ideas) {
  if (status === "loading") return "loading";
  if (status === "error") return "error";
  if (status !== "success") throw new Error("unknown_status");
  if (!Array.isArray(ideas)) throw new TypeError("ideas_not_array");
  return ideas.length === 0 ? "empty" : "ready";
}
Enter fullscreen mode Exit fullscreen mode

This helper chooses a state; your UI still needs to render each one. An error response must not be converted to an empty success merely to keep the component running. Follow the response contract your project actually uses.

Check calculations that expect a first row

Search the list component for direct access to the first item. Decide what the selected-item panel should display when there is nothing to select. Avoid leaving a previous record on screen after the dataset becomes empty.

Review totals separately. MDN explains that reduce without an initial value throws for an empty array. If zero is the correct total for your domain, provide zero as the initial accumulator. If a metric is undefined without observations, show that explicitly instead of inventing a numeric result.

Exercise the transition, not only the first render

Check loading followed by empty success, error followed by empty success, and a populated list followed by empty success. A list that starts empty can look correct while the transition from a selected record leaves an obsolete detail panel behind.

Test any filters too. "No matching ideas" may require clearing filters; "No ideas yet" may require creating the first record. Both can contain an empty array, but they offer different next actions.

Add one idea through the ordinary demo flow and confirm that the empty message disappears. Return to the empty fixture with the project's documented test procedure and repeat. Record the fixture and the expected screen beside the demo scenario.

Choose a challenge in the Stavleak hackathon catalogue, then include empty-data behavior in your project's acceptance checks. This classifier is an educational example for a fictional idea board, not a report of testing a customer application.

Top comments (0)