React Native Testing Library
React Native Testing Library
React Native Testing Library provides utilities for testing React Native components in a way that closely resembles how users actually interact with your application, focusing on behavior rather than implementation details.
Installing React Native Testing Library
bash
npm install --save-dev @testing-library/react-native
Rendering a Component
javascript
import { render, screen } from '@testing-library/react-native';
import Greeting from '../Greeting';
test('renders greeting message', () => {
render( );
expect(screen.getByText('Hello, John!')).toBeTruthy();
});
Simulating User Interactions
javascript
import { render, screen, fireEvent } from '@testing-library/react-native';
test('increments counter on press', () => {
render( );
fireEvent.press(screen.getByText('Increment'));
expect(screen.getByText('1')).toBeTruthy();
});
Testing Text Input
javascript
test('updates input value', () => {
render( );
const input = screen.getByPlaceholderText('Search');
fireEvent.changeText(input, 'React Native');
expect(input.props.value).toBe('React Native');
});
Querying by Test ID
javascript
Submit
screen.getByTestId('submit-button');
Testing Asynchronous UI Updates
javascript
import { waitFor } from '@testing-library/react-native';
await waitFor(() => expect(screen.getByText('Loaded')).toBeTruthy());
Conclusion
React Native Testing Library encourages writing tests that reflect real user behavior, resulting in more reliable tests that remain valid even as internal component implementation details change.

