
ReactJS
Learn to simulate user interactions in tests.
import userEvent from '@testing-library/user-event';
test('increments on click', async () => {
render(<Counter />);
const button = screen.getByRole('button');
await userEvent.click(button);
expect(screen.getByText('Count: 1')).toBeInTheDocument();
});test('types in input', async () => {
render(<SearchBox />);
const input = screen.getByPlaceholderText('Search...');
await userEvent.type(input, 'react');
expect(input.value).toBe('react');
});test('submits form', async () => {
const handleSubmit = jest.fn();
render(<LoginForm onSubmit={handleSubmit} />);
await userEvent.type(
screen.getByLabelText('Email'),
'user@example.com'
);
await userEvent.type(
screen.getByLabelText('Password'),
'password'
);
await userEvent.click(screen.getByRole('button', { name: /submit/i }));
expect(handleSubmit).toHaveBeenCalled();
});test('handles Enter key', async () => {
const handleSubmit = jest.fn();
render(<SearchInput onSearch={handleSubmit} />);
await userEvent.type(
screen.getByPlaceholderText('Search...'),
'react{Enter}'
);
expect(handleSubmit).toHaveBeenCalled();
});Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward