Sub topic 12
Reviewing and Testing Every State
Prerequisites: You should know basic UI design terms, how an app fetches data asynchronously, and how to read HTML and TypeScript. This chapter assumes you already know the main loading patterns, empty-state types, error categories, and accessibility tools such as live regions.
A data-driven screen is not one design. It is a set of states and transitions. A review is complete only when each state has a defined appearance, message, action, and accessibility behavior.
Build a state inventory
Start by listing the states that users can reach. Do not test only the normal success case. Include unusual timing, missing data, and failures that affect only part of the screen.
| State | How it starts | What to verify |
|---|---|---|
| Initial loading | The screen opens without data | Loading feedback appears and the layout does not jump |
| Success with data | The request returns items | Items, actions, and labels are correct |
| Success with no items | The request returns an empty collection | The message explains why the space is empty and offers the right next action |
| Retryable error | The network or service fails temporarily | The error explains recovery and retry works |
| Permanent error | The request is rejected or unavailable | The message does not promise that retry will help |
| Partial failure | One section loads while another fails | Successful content remains usable and failed content has its own recovery |
| Refreshing | Existing content is being updated | Existing content remains understandable while the update runs |
Also test transitions. A screen can look correct in isolation but fail when it moves quickly from loading to success, or from one request to another.
Test behavior, not screenshots alone
A screenshot can show spacing and visual hierarchy. It cannot prove that a retry button works, focus moves correctly, or a stale request cannot overwrite newer data.
For each state, test four layers:
- Content: Is the message accurate, specific, and consistent with the cause?
- Interaction: Can the user perform the next action, such as retrying or creating an item?
- Transition: Does the screen move to the right next state after the action?
- Accessibility: Can a keyboard user and a screen reader user perceive the change?
Use realistic conditions. Test a slow connection, an offline connection, a server error, an empty response, and a response that arrives after the user has navigated away.
Check the state contract
A small state contract test can catch missing cases before a browser test runs. The following script checks that each state produces a meaningful user-facing result.
import assert from "node:assert/strict";
function renderMessage(state) {
switch (state.status) {
case "loading":
return "Loading projects";
case "success":
return state.items.length === 0
? "No projects yet. Create your first project."
: `${state.items.length} project(s)`;
case "error":
return state.retryable
? `${state.message} Try again.`
: state.message;
default:
throw new Error(`Unhandled state: ${state.status}`);
}
}
const states = [
{ status: "loading" },
{ status: "success", items: ["Website"] },
{ status: "success", items: [] },
{ status: "error", message: "Connection lost.", retryable: true },
{ status: "error", message: "You do not have access.", retryable: false }
];
for (const state of states) {
const message = renderMessage(state);
assert.ok(message.trim().length > 0, `Missing message for ${state.status}`);
}
assert.match(renderMessage(states[2]), /No projects yet/);
assert.match(renderMessage(states[3]), /Try again/);
console.log("All state contract checks passed.");In a typed application, use a discriminated union so the compiler can warn when a new state is not handled. The test still matters because a compiler cannot judge whether the wording, focus behavior, or layout is useful.
Test transitions and timing
Test these sequences explicitly:
- Open the screen and confirm loading feedback appears.
- Return data quickly and confirm a delayed spinner does not flash.
- Return data slowly and confirm the user receives feedback.
- Start request A, start request B, then return A after B. Request A must not replace newer data.
- Retry an error and confirm the loading state does not duplicate controls or messages.
- Refresh existing content and confirm users can still understand the old content.
- Leave the screen during a request and confirm the result does not update an unrelated screen.
Review accessibility and recovery
A state change must be perceivable without relying on color or motion. Verify that loading updates are announced politely, the updating region is identified as busy, and important errors are announced promptly. After an error, place focus on the error summary or the retry control when that move helps the user continue. Do not move focus for every background refresh.
Check that disabled controls have an understandable reason, keyboard focus remains visible, and the empty-state action has a useful accessible name. Test at increased text size and with reduced motion enabled.
Use a release checklist
Before release, ask:
- Does every reachable state have a design and a test?
- Can each error state explain what happened and what to do next?
- Can users recover without reloading the whole page?
- Are partial failures isolated instead of hiding usable content?
- Are loading and error changes announced appropriately?
- Have slow, offline, repeated, and out-of-order requests been tested?
- Have keyboard, screen reader, reduced-motion, and zoom conditions been checked?
Exercise
A project list shows a spinner, then displays “No projects found.” The API succeeded with an empty array. What should you test before release?
Show answer
Test that the empty result is distinguished from loading and failure, that the message explains the next action, that the action works, that the state is announced appropriately, and that a later refresh can move the screen to a populated state.