Challenge - 5 Problems
React Native Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the output of this test?
Given this React Native component and test, what will the test output be?
React Native
import React from 'react'; import { Text, Button, View } from 'react-native'; import { render, fireEvent } from '@testing-library/react-native'; function Counter() { const [count, setCount] = React.useState(0); return ( <View> <Text testID="count-text">Count: {count}</Text> <Button title="Increment" onPress={() => setCount(count + 1)} /> </View> ); } const { getByTestId, getByText } = render(<Counter />); fireEvent.press(getByText('Increment')); const text = getByTestId('count-text').props.children.join('');
Attempts:
2 left
💡 Hint
Remember that pressing the button updates the state and the text reflects the new count.
✗ Incorrect
The button press triggers setCount to increase count by 1. The Text component then shows 'Count: 1'. The children prop is an array: ['Count: ', 1], so joining them produces 'Count: 1'.
❓ lifecycle
intermediate2:00remaining
Which lifecycle behavior does this test verify?
Consider this test for a React Native component that fetches data on mount. What lifecycle behavior is being tested?
React Native
import React from 'react'; import { Text } from 'react-native'; import { render, waitFor } from '@testing-library/react-native'; function DataFetcher({ fetchData }) { const [data, setData] = React.useState(null); React.useEffect(() => { fetchData().then(setData); }, [fetchData]); return <Text>{data ?? 'Loading...'}</Text>; } const mockFetch = () => Promise.resolve('Hello'); const { getByText } = render(<DataFetcher fetchData={mockFetch} />); await waitFor(() => getByText('Hello'));
Attempts:
2 left
💡 Hint
Look at when fetchData is called inside useEffect.
✗ Incorrect
The useEffect hook runs once after the component mounts, simulating componentDidMount. It calls fetchData and updates state asynchronously.
📝 Syntax
advanced2:00remaining
What error does this test code produce?
Identify the error when running this React Native Testing Library code snippet.
React Native
import React from 'react'; import { Text } from 'react-native'; import { render } from '@testing-library/react-native'; const { getByText } = render(<Text>Hello</Text>); getByText('Hello').props.onPress();
Attempts:
2 left
💡 Hint
Check if all components are imported correctly.
✗ Incorrect
The Text component is used and imported correctly, so no ReferenceError. However, the Text component does not have an onPress prop, so calling onPress() results in a TypeError.
advanced
2:00remaining
What will this test verify about navigation?
Given this test for a React Navigation screen, what behavior does it check?
React Native
import React from 'react'; import { Button } from 'react-native'; import { render, fireEvent } from '@testing-library/react-native'; function HomeScreen({ navigation }) { return <Button title="Go" onPress={() => navigation.navigate('Details')} />; } const mockNavigate = jest.fn(); const { getByText } = render(<HomeScreen navigation={{ navigate: mockNavigate }} />); fireEvent.press(getByText('Go')); expect(mockNavigate).toHaveBeenCalledWith('Details');
Attempts:
2 left
💡 Hint
Look at what mock function is called when the button is pressed.
✗ Incorrect
The test simulates pressing the button and verifies that navigation.navigate was called with the argument 'Details'.
🔧 Debug
expert2:00remaining
Why does this test fail with a timeout?
This test tries to wait for a text update but fails with a timeout error. Why?
React Native
import React from 'react'; import { Text, Button } from 'react-native'; import { render, fireEvent, waitFor } from '@testing-library/react-native'; function AsyncButton() { const [text, setText] = React.useState('Start'); const onPress = () => { setTimeout(() => setText('Done'), 1000); }; return <><Text>{text}</Text><Button title="Press" onPress={onPress} /></>; } const { getByText } = render(<AsyncButton />); fireEvent.press(getByText('Press')); await waitFor(() => getByText('Done'), { timeout: 500 });
Attempts:
2 left
💡 Hint
Compare the setTimeout delay with the waitFor timeout.
✗ Incorrect
The text changes after 1000ms but waitFor only waits 500ms before timing out, so it never sees the updated text.