0PricingLogin
React Native Academy · Lesson

Firing Events and Testing User Interaction

Use fireEvent.press and fireEvent.changeText to simulate user actions, then assert that state updates and callback functions are called correctly.

Simulating User Events in Tests

Component tests become valuable when they verify how a component responds to user interactions — button presses, text input, form submission. React Native Testing Library provides the fireEvent utility to simulate these interactions in a test environment without a real device.

Simulating events is not the same as real user input — it directly calls the event handler prop of the element. But for most unit and integration tests, this is sufficient to verify that the component logic responds correctly to interactions.

fireEvent.press for Button Taps

fireEvent.press(element) simulates a tap on a TouchableOpacity, Pressable, or Button element. After pressing, assert on the expected side effects — state updates, navigation calls, or callback invocations.

Find the button first with a query like getByText or getByRole('button'), then fire the press event. The combination of query and event simulates the full user journey: find the button, tap it, verify the result.

import { render, fireEvent } from '@testing-library/react-native';
import { Counter } from '../src/components/Counter';

it('increments the count when the plus button is pressed', () => {
  const { getByText } = render(<Counter />);

  // Assert initial state
  expect(getByText('Count: 0')).toBeTruthy();

  // Simulate button press
  fireEvent.press(getByText('+'));

  // Assert state changed
  expect(getByText('Count: 1')).toBeTruthy();

  // Press again
  fireEvent.press(getByText('+'));
  expect(getByText('Count: 2')).toBeTruthy();
});

All lessons in this course

  1. Setting Up Jest in a React Native Project
  2. Rendering Components with React Native Testing Library
  3. Firing Events and Testing User Interaction
  4. Mocking Native Modules and Async Code
← Back to React Native Academy