The
 Vincent

PEREZ
Archives

testing library/react

testing library/react

 

UK/USA
1997
 115 minutes
 

 

Testing Library/react __link__ Today

import render, screen, waitFor from '@testing-library/react'; import UserProfile from './UserProfile'; test('loads user data', async () => render(<UserProfile userId=1 />);

expect(screen.getByText(/loading/i)).toBeInTheDocument();

export default function Greeting( name ) return <h1>Hello, name!</h1>; testing library/react

import render, screen from '@testing-library/react'; import userEvent from '@testing-library/user-event'; import ToggleButton from './ToggleButton'; test('toggles text on click', async () => const user = userEvent.setup(); render(<ToggleButton />);

Greeting.test.jsx

import render, screen from '@testing-library/react'; import Greeting from './Greeting'; test('displays the correct greeting', () => render(<Greeting name="Alice" />); const heading = screen.getByText(/hello, alice!/i); expect(heading).toBeInTheDocument(); ); Use userEvent (recommended over fireEvent for realistic behavior):

expect(button).toHaveTextContent(/on/i); ); For API calls or async updates: waitFor from '@testing-library/react'

const button = screen.getByRole('button', name: /off/i ); await user.click(button);