Introduction
Testing helps find and fix problems early. It makes sure the app works well and users are happy.
Testing helps find and fix problems early. It makes sure the app works well and users are happy.
import { render, fireEvent } from '@testing-library/react-native'; import MyComponent from './MyComponent'; test('button press changes text', () => { const { getByText } = render(<MyComponent />); const button = getByText('Press me'); fireEvent.press(button); expect(getByText('You pressed the button!')).toBeTruthy(); });
render to display the component in a test environment.fireEvent to simulate user actions like taps.import { render } from '@testing-library/react-native'; import App from './App'; test('renders welcome message', () => { const { getByText } = render(<App />); expect(getByText('Welcome to the app!')).toBeTruthy(); });
import { fireEvent, render } from '@testing-library/react-native'; import Counter from './Counter'; test('increments counter on button press', () => { const { getByText } = render(<Counter />); const button = getByText('Increment'); fireEvent.press(button); expect(getByText('Count: 1')).toBeTruthy(); });
This simple app shows a button. When you press it, a message appears. Testing this ensures the button works as expected.
import React, { useState } from 'react'; import { Text, Button, View } from 'react-native'; export default function TestApp() { const [pressed, setPressed] = useState(false); return ( <View style={{ padding: 20 }}> <Button title="Press me" onPress={() => setPressed(true)} /> {pressed && <Text>You pressed the button!</Text>} </View> ); }
Testing saves time by catching bugs early.
Automated tests can run many times without extra work.
Good tests improve confidence when changing code.
Testing finds problems before users do.
It checks that app features work correctly.
Helps keep the app reliable and user-friendly.
test('button press changes text', () => {
const text = 'Hello';
const newText = text + ' World';
expect(newText).toBe('Hello World');
});test('check value', () => {
expect(5 + 5).toBe(11);
});Pass the test and this pattern moves unknown