A. You cannot call onPress() directly on the element returned by getByText
B. render should be imported from 'react-native' not '@testing-library/react-native'
C. getByText should be called with a component, not a string
D. MyComponent must be wrapped in a <View> to render
Solution
Step 1: Understand what getByText returns
getByText returns a React Test Instance, not a real component with event handlers.
Step 2: Check how to simulate press events
To simulate press, use fireEvent.press(button) from testing library, not call onPress() directly.
Final Answer:
You cannot call onPress() directly on the element returned by getByText -> Option A
Quick Check:
Use fireEvent.press, not direct onPress call [OK]
Hint: Use fireEvent.press to simulate button press [OK]
Common Mistakes:
Calling onPress() directly on test element
Importing render from wrong package
Misusing getByText argument type
5. You want to test a component that conditionally renders a <Text> with 'Logged In' only if the prop isLoggedIn is true. Which test code correctly checks this behavior?
hard
A. const { getByText } = render();
expect(getByText('Logged In')).toBeNull();
B. const { getByText } = render();
expect(queryByText('Logged In')).not.toBeNull();
C. const { queryByText } = render();
expect(queryByText('Logged In')).toBeNull();
D. const { queryByText } = render();
expect(getByText('Logged In')).toBeNull();
Solution
Step 1: Understand difference between getByText and queryByText
getByText throws error if text not found; queryByText returns null if not found.
Step 2: Check test logic for isLoggedIn false
When isLoggedIn is false, 'Logged In' text should not appear, so queryByText returns null.