HuntExams Academy logo
ReactAdvanced#testing

How do you test React components?

Answer

Render with React Testing Library and assert on what the user sees, querying by role or label rather than implementation details, and use userEvent for interactions.

Example

render(<Login />);
await userEvent.type(screen.getByLabelText('Email'), 'a@b.com');
await userEvent.click(screen.getByRole('button', { name: /log in/i }));
expect(screen.getByText(/welcome/i)).toBeInTheDocument();

Related Interview Questions

1
ReactIntermediate#accessibility

What accessibility basics should a React component follow?

Open
2
ReactAdvanced#context#performance

How do you avoid unnecessary re-renders from context?

Open
3
ReactBeginner#fundamentals

What is React and why use it?

Open