What is React Testing Library?
React Testing Library (RTL) is a library for testing React components. It encourages testing from the user's perspective rather than implementation details.
๐ป Component Test
import { render, screen, fireEvent } from '@testing-library/react';
import Counter from './Counter';
test('renders counter and increments', () => {
render( );
const button = screen.getByText(/increment/i);
const count = screen.getByText(/count: 0/i);
expect(count).toBeInTheDocument();
fireEvent.click(button);
expect(screen.getByText(/count: 1/i)).toBeInTheDocument();
});๐ฏ Key Takeaways
- render(): Render component for testing
- screen: Query rendered output
- fireEvent: Simulate user interactions
- getByText: Find elements by text content
- User-centric: Test like a user would