Passing Props to Rendered Components
You'll learn how to render components with different prop configurations in your tests, enabling you to verify behavior across multiple scenarios.
The Core Idea
When testing real components, you rarely render them in isolation without props. Just like in your application, test components need data, callbacks, and configuration. The render function accepts JSX, which means you pass props exactly as you would when using the component normally.
Think of it like testing a car: you don't just start the engine with default settings. You test with different fuel levels, different weather conditions, different loads. Similarly, you render your component with different props to verify it handles various states correctly.
Passing Props in Practice
import { render, screen } from '@testing-library/react';
import { UserProfile } from './UserProfile';
test('displays user name', () => {
render(<UserProfile name="Alice" age={30} />);
// Now test behavior...
});
test('displays different user', () => {
render(<UserProfile name="Bob" age={25} />);
// Test this configuration...
});
You're simply writing JSX inside render(). Props flow naturally—strings, numbers, booleans, objects, functions—all work exactly as they do in your app.
Testing Multiple Configurations
The power comes from rendering the same component multiple times with different props:
test('handles premium users differently', () => {
render(<UserProfile name="Alice" isPremium={true} />);
expect(screen.getByText(/premium badge/i)).toBeInTheDocument();
});
test('shows standard UI for free users', () => {
render(<UserProfile name="Bob" isPremium={false} />);
expect(screen.queryByText(/premium badge/i)).not.toBeInTheDocument();
});
Each render call creates a fresh instance with its specific props, letting you verify behavior across the full range of inputs your component receives.
Key Takeaway: Pass props to render() using normal JSX syntax—test multiple prop configurations by calling render() separately in each test to verify your component handles different scenarios correctly.