0
0
React Nativemobile~20 mins

Component testing in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Component 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 React Native component test?
Consider this test for a simple button component. What will the test output be?
React Native
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { Text, TouchableOpacity } from 'react-native';

function MyButton({ onPress }) {
  return <TouchableOpacity onPress={onPress} accessibilityLabel="my-button">
    <Text>Press me</Text>
  </TouchableOpacity>;
}

test('button press triggers handler once', () => {
  const handler = jest.fn();
  const { getByA11yLabel } = render(<MyButton onPress={handler} />);
  fireEvent.press(getByA11yLabel('my-button'));
  expect(handler).toHaveBeenCalledTimes(1);
});
ATest fails because the handler is never called.
BTest passes because the button press calls the handler exactly once.
CTest fails due to syntax error in the component code.
DTest fails because the accessibilityLabel is missing.
Attempts:
2 left
💡 Hint
Check if the button has the correct accessibility label and if the press event triggers the handler.
lifecycle
intermediate
2:00remaining
What happens to the component state after this test?
Given this React Native component and test, what is the final value of the displayed count after the test runs?
React Native
import React, { useState } from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import { Text, Button, View } from 'react-native';

function Counter() {
  const [count, setCount] = useState(0);
  return <View>
    <Text testID="count-text">Count: {count}</Text>
    <Button title="Increment" onPress={() => setCount(count + 1)} />
  </View>;
}

test('increments count on button press', () => {
  const { getByText, getByTestId } = render(<Counter />);
  fireEvent.press(getByText('Increment'));
  fireEvent.press(getByText('Increment'));
  expect(getByTestId('count-text').props.children.join('')).toBe('Count: 2');
});
AThe displayed count is 'Count: 1' because only one press is registered.
BThe displayed count remains 'Count: 0' because state does not update in tests.
CThe test throws an error because children is not an array.
DThe displayed count is 'Count: 2' after two presses.
Attempts:
2 left
💡 Hint
Check how many times the increment button is pressed and how state updates.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in this React Native test?
Identify which test code snippet will cause a syntax error when testing a component.
Atest('missing closing parenthesis', () => { const { getByText } = render(<Text>Hello</Text>); expect(getByText('Hello')).toBeTruthy();
Btest('renders text', () => { const { getByText } = render(<Text>Hello</Text>); expect(getByText('Hello')).toBeTruthy(); });
Ctest('correct async test', async () => { const { findByText } = render(<Text>Hi</Text>); expect(await findByText('Hi')).toBeTruthy(); });
Dtest('renders button', () => { const { getByRole } = render(<Button title='Click' onPress={() => {}} />); expect(getByRole('button')).toBeTruthy(); });
Attempts:
2 left
💡 Hint
Look for missing brackets or parentheses in the test code.
🔧 Debug
advanced
2:00remaining
Why does this test fail to find the button?
This test tries to find a button by accessibility label but fails. Why?
React Native
import React from 'react';
import { render } from '@testing-library/react-native';
import { TouchableOpacity, Text } from 'react-native';

function MyButton() {
  return <TouchableOpacity>
    <Text>Click me</Text>
  </TouchableOpacity>;
}

test('finds button by accessibility label', () => {
  const { getByA11yLabel } = render(<MyButton />);
  getByA11yLabel('my-button');
});
AThe TouchableOpacity lacks the accessibilityLabel prop, so getByA11yLabel cannot find it.
BThe test fails because Text components cannot have accessibility labels.
CThe test fails because getByA11yLabel requires a testID instead.
DThe test fails because TouchableOpacity is not a valid React Native component.
Attempts:
2 left
💡 Hint
Check if the component has the accessibilityLabel property set.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using React Native Testing Library over Enzyme for component testing?
Choose the best reason why React Native Testing Library is preferred for testing React Native components.
AIt automatically generates UI tests without writing any test code.
BIt provides faster snapshot testing and shallow rendering capabilities than Enzyme.
CIt encourages testing components the way users interact with them, focusing on accessibility and behavior rather than implementation details.
DIt requires less setup and supports testing native modules directly without mocks.
Attempts:
2 left
💡 Hint
Think about how users experience the app and what testing approach matches that.