The Core Principle: Test User-Facing Behavior
What You'll Learn
You'll understand why focusing tests on what users see and do—rather than internal component details—creates more valuable and maintainable tests.
The Philosophy Behind Good Tests
This principle is the culmination of everything React Testing Library teaches: write tests that interact with your application the same way a real user would. Users don't care about your component's state variables, effect dependencies, or prop names—they care about clicking buttons, seeing text, filling forms, and getting results.
When you test user-facing behavior, you're answering: "Does this component work from the user's perspective?" Not: "Is the internal implementation structured the way I expect?"
Why This Matters
Imagine testing a login form. A bad test checks if isLoading state is true after submission. A good test checks if a loading spinner appears on screen. The first breaks when you refactor state management. The second only breaks when users actually experience a problem.
This approach creates:
- Resilience: Tests survive refactors that don't change behavior
- Confidence: Passing tests mean users can accomplish their goals
- Maintainability: You're not constantly fixing tests after internal changes
The Mental Shift
Think of your component as a black box. You provide inputs (props, user interactions) and assert outputs (rendered content, network requests). The "how" inside the box is irrelevant—only the observable behavior matters.
Instead of: "Did this state update?"
Think: "Did the UI change as expected?"
Instead of: "Was this function called?"
Think: "Did the user's action produce the right result?"
Key Takeaway: Test what users experience—rendered output and interaction results—not internal mechanics like state or function calls. This creates tests that serve as reliable specifications of user-facing behavior.