// Navigate to profile await userEvent.click(screen.getByRole('link', { name: /profile/i })) expect(screen.getByText(/user profile/i)).toBeInTheDocument()
act(() => { result.current.increment() }) react testing library
// Verify dashboard expect(await screen.findByText(/welcome, user/i)).toBeInTheDocument() // Navigate to profile await userEvent
// Or use waitFor for custom conditions await waitFor(() => { expect(screen.getByRole('alert')).toHaveTextContent('Success') }) // fireEvent - lower level, more immediate fireEvent.click(button) fireEvent.change(input, { target: { value: 'new' } }) // userEvent - higher level, more realistic (recommended) await userEvent.click(button) await userEvent.type(input, 'new text') await userEvent.selectOptions(select, 'option1') await userEvent.tab() // Focus management Testing Form Interactions test('submits form with user input', async () => { const handleSubmit = jest.fn() render(<LoginForm onSubmit={handleSubmit} />) // Fill form fields await userEvent.type(screen.getByLabelText(/email/i), 'test@example.com') await userEvent.type(screen.getByLabelText(/password/i), 'password123') 'new text') await userEvent.selectOptions(select
expect(handleSubmit).toHaveBeenCalledWith({ email: 'test@example.com', password: 'password123' }) }) import { renderHook, act } from '@testing-library/react' test('useCounter hook increments', () => { const { result } = renderHook(() => useCounter(0))
// Wait for loading to complete expect(screen.getByText(/loading/i)).toBeInTheDocument()
React Testing Library encourages writing accessible, user-focused tests that give you real confidence in your application. The key is always asking: "Is this how a user would interact with and observe this component?"