Avoiding Direct State and Props Assertions
You'll learn why testing internal state or props is a testing anti-pattern and what to test instead.
The Core Problem
When you test that a component's state changed to true or that a prop was passed with a specific value, you're testing implementation details—internal wiring that users never see. If you refactor from useState to a reducer, or rename a prop, these tests break even though the user experience hasn't changed.
This violates the black-box testing principle you've been building toward.
Test the Effect, Not the Cause
Instead of asserting "the isOpen state is now true", assert what the user sees when that state changes: "a modal with role='dialog' is now visible in the document."
Instead of checking "the onClick prop was called", verify the behavior the user experiences: "the button is no longer disabled" or "a success message appears."
Analogy: When you test drive a car, you don't pop the hood to verify the engine temperature increased—you check that the speedometer says you're moving. The engine is an implementation detail.
Why This Matters
This approach gives you:
- Refactor confidence: Change internal logic freely without breaking tests
- Real-world validation: If the test passes but users can't see the change, the test would catch it
- Maintainable tests: Fewer false failures from harmless refactors
When you query by role, text content, or accessibility attributes (as you've learned), you're naturally testing user-visible outcomes. State and props are just the mechanism—the UI change is the contract.
Key Takeaway: Test what users see and experience, not the internal state or props that produce those effects—implementation can change, but user expectations shouldn't.