Course contentsShow
← React
Lesson 2036 of 2,39646. Testing React with RTLFree lesson

The Mental Model: Black Box Testing

Treat your component as a black box—test inputs (props, user actions) and outputs (rendered DOM).

The Mental Model: Black Box Testing

You'll learn how to approach testing React components like a user, not a developer peeking under the hood.

What Black Box Testing Means

Imagine testing a toaster. You don't crack it open and verify every wire and resistor inside. Instead, you put bread in, press the lever, and check if toast comes out. Black box testing treats your React component the same way—you only test what goes in (inputs) and what comes out (outputs), ignoring the internal mechanics.

The Three Parts You Care About

  1. Props: The data you pass into your component
  2. User Actions: Clicks, typing, focus events—anything a real user does
  3. Rendered DOM: What actually appears on screen (text, buttons, images)

You don't test:

  • Internal state variable names (useState internals)
  • Helper function implementations
  • Component re-render counts
  • Private methods or hooks

Why This Mental Model Matters

Your users can't see your component's internal state. They don't know if you used useState or useReducer. They only know:

  • "Does this button appear?"
  • "When I click it, does something happen?"
  • "Can I type in this input field?"

Testing from the outside means your tests survive refactoring. You can completely rewrite a component's internals without breaking tests—as long as the user experience stays the same.

Example Thinking

Instead of: "Does my component set isOpen state to true?"
Think: "Does the modal appear on screen when I click the button?"

Key Takeaway: Test your components like a user interacts with them—through props, user events, and visible output. Treat everything else as an implementation detail that can change.