0
0
React Nativemobile~20 mins

React Native Testing Library - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Native Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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('');
A"Count: 1"
BTypeError: Cannot read property 'join' of undefined
C"Count: 0"
D"Count: undefined"
Attempts:
2 left
💡 Hint
Remember that pressing the button updates the state and the text reflects the new count.
lifecycle
intermediate
2: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'));
AComponentWillUnmount cleans up fetchData call
BComponentDidMount triggers fetchData and updates state asynchronously
CComponentDidUpdate triggers fetchData on every render
DComponentDidCatch handles fetchData errors
Attempts:
2 left
💡 Hint
Look at when fetchData is called inside useEffect.
📝 Syntax
advanced
2: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();
ATypeError: getByText(...).props.onPress is not a function
BReferenceError: Text is not defined
CSyntaxError: Unexpected token <
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check if all components are imported correctly.
navigation
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');
AThe test ensures navigation.goBack is called on button press
BThe test verifies the Details screen is rendered after button press
CThe test checks if pressing the button calls navigation.navigate with 'Details'
DThe test confirms navigation.reset is called with empty routes
Attempts:
2 left
💡 Hint
Look at what mock function is called when the button is pressed.
🔧 Debug
expert
2: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 });
AThe getByText selector is case sensitive and misses 'Done'
BThe setTimeout callback never runs because of missing cleanup
CThe fireEvent.press does not trigger onPress handler
DThe waitFor timeout is too short to catch the delayed text update
Attempts:
2 left
💡 Hint
Compare the setTimeout delay with the waitFor timeout.