React Testing Examples

react-testing-library
GitHubAbout
All examples

The component receives a callback prop and renders a button. We test that the callback prop is called when the button is clicked.

test.js
it('calls "onClick" prop on button click', () => {
// Render new instance in every test to prevent leaking state
const onClick = jest.fn();
const { getByText } = render(<Button onClick={onClick} />);
fireEvent.click(getByText(/click me nao/i));
expect(onClick).toHaveBeenCalled();
});
it('calls "onClick" prop on button click', () => {
  // Render new instance in every test to prevent leaking state
  const onClick = jest.fn();
  const { getByText } = render(<Button onClick={onClick} />);

  fireEvent.click(getByText(/click me nao/i));
  expect(onClick).toHaveBeenCalled();
});