Skip to main content

Testing visual mental model

The strongest React tests exercise the interface through the same observable behavior users and assistive technologies experience.

Behavior-focused React test loop
1Render UI
2Find semantic elements
3Interact like a user
4Wait for async outcomes
5Assert visible behavior

Test the public surface​

What should a component test observe?
Semantics
roles · labels · accessible names
Interaction
click · type · focus · submit · keyboard
Visible result
content · state · errors · navigation feedback
Async behavior
pending · resolved · rejected · retries
Accessibility behavior
focus movement · dialog relationships · form feedback
Integration contract
providers · router · server/cache boundaries when genuinely relevant

Avoid implementation-detail coupling​

Weak assertion vs resilient assertion
FragileInspect internals
Private state values, Hook call counts, component internals, or DOM structure that users do not care about.
ResilientObserve behavior
A button is disabled, a dialog appears, an error is announced, or saved data becomes visible.

Query by meaning​

1Role + accessible name
2Label text
3Visible text/value/alt text when appropriate
4Test ID only when no semantic query fits

Choose the test boundary deliberately​

What kind of confidence do you need?
Pure domain calculationUnit test the function
One component interactionBehavior-focused component test
Provider/router/cache coordinationIntegration test the boundary
Critical browser workflowEnd-to-end test in a real browser
Visual styling regressionUse visual regression tooling where justified

Async React testing​

Async UI should be asserted as a user-visible lifecycle
User starts work
Submit, navigation, lazy load, Action, or mutation.
Pending UI
Spinner, disabled control, fallback, optimistic projection, or status message.
Success
Expected result becomes observable.
Failure
Recoverable error UI becomes observable.

Keep this mental model​

Testing in one picture
Render
Create the UI state.
Interact
Use the public interface.
Wait
Respect async transitions.
Assert
Check observable outcomes.

Continue with Testing React Through User Behavior for detailed Testing Library patterns, async APIs, and production testing strategy.