Junior — Middle
Have you created automated tests for React components as part of unit testing?
sobes.tech AI
Answer from AI
Yes, for unit testing React components, libraries such as Jest and React Testing Library are commonly used. They allow testing component behavior in isolation, simulating user events and checking rendering.
Example of a simple component test with React Testing Library:
import { render, screen, fireEvent } from '@testing-library/react';
import Button from './Button';
test('button calls handler on click', () => {
const onClick = jest.fn();
render(<Button onClick={onClick}>Click me</Button>);
fireEvent.click(screen.getByText('Click me'));
expect(onClick).toHaveBeenCalledTimes(1);
});
This approach helps ensure that components work correctly when code changes.