import React from 'react';
import { Text, View } from 'react-native';
export default function Greeting() {
return (
<View>
<Text>Hello, Jest!</Text>
</View>
);
}
// __tests__/Greeting.test.js
import React from 'react';
import { render } from '@testing-library/react-native';
import Greeting from '../Greeting';
test('Greeting component renders correct text', () => {
const { getByText } = render(<Greeting />);
expect(getByText('Hello, Jest!')).toBeTruthy();
});
// package.json scripts section
// "scripts": {
// "test": "jest"
// },
// jest.config.js
// module.exports = {
// preset: 'react-native',
// setupFilesAfterEnv: ['@testing-library/jest-native/extend-expect'],
// transformIgnorePatterns: [
// 'node_modules/(?!(react-native|@react-native|@react-native-community)/)'
// ]
// };We implemented the Greeting component to return a View containing a Text element with the string "Hello, Jest!". This is the UI we want to test.
In the __tests__/Greeting.test.js file, we use @testing-library/react-native to render the component and check if the text "Hello, Jest!" is present. This confirms the component renders correctly.
The package.json script "test" runs Jest, and the jest.config.js file configures Jest for React Native, including necessary presets and transforms.
This setup allows running npm test or yarn test to execute the unit test and verify the component's output.