0
0
React Nativemobile~20 mins

Snapshot testing in React Native - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Snapshot Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What does this snapshot test verify?
Consider this React Native snapshot test code:
import React from 'react';
import renderer from 'react-test-renderer';
import Button from './Button';

test('Button renders correctly', () => {
  const tree = renderer.create(<Button title='Click me' />).toJSON();
  expect(tree).toMatchSnapshot();
});

What is this test checking?
React Native
import React from 'react';
import renderer from 'react-test-renderer';
import Button from './Button';

test('Button renders correctly', () => {
  const tree = renderer.create(<Button title='Click me' />).toJSON();
  expect(tree).toMatchSnapshot();
});
AIt tests that the Button component's title prop changes dynamically.
BIt verifies the Button component's onPress function is called.
CIt checks that the Button component renders the same UI structure as before.
DIt ensures the Button component's styles are applied correctly.
Attempts:
2 left
💡 Hint
Snapshot tests compare the rendered output structure to a saved version.
lifecycle
intermediate
1:30remaining
When should you update a snapshot test?
You run a snapshot test and it fails because the UI changed intentionally. What should you do next?
AUpdate the snapshot to reflect the new UI and commit the change.
BIgnore the failure and keep the old snapshot.
CDelete the snapshot test to avoid future failures.
DChange the component code back to match the old snapshot.
Attempts:
2 left
💡 Hint
Snapshots should match the intended UI state.
🔧 Debug
advanced
2:00remaining
Why does this snapshot test fail with a syntax error?
Look at this snapshot test snippet:
test('renders text', () => {
  const tree = renderer.create(Hello).toJSON()
  expect(tree).toMatchSnapshot()
})

Why might this cause a syntax error?
AMissing semicolons cause syntax errors in JavaScript tests.
BThe JSX <Text> element is not imported or defined in the test file.
Cexpect() is not imported from the testing library.
DtoJSON() is not a function on the renderer object.
Attempts:
2 left
💡 Hint
JSX elements must be imported or defined before use.
🧠 Conceptual
advanced
1:30remaining
What is a limitation of snapshot testing in React Native?
Which of these is a common limitation of snapshot tests?
AThey cannot detect changes in component behavior or logic, only UI structure.
BThey only work on Android devices, not iOS.
CThey require manual user interaction to run.
DThey always fail when styles change, even if the UI looks the same.
Attempts:
2 left
💡 Hint
Snapshot tests compare UI output, not how components behave.
📝 Syntax
expert
2:30remaining
What is the output of this snapshot test code?
Given this test code:
import React from 'react';
import renderer from 'react-test-renderer';

const MyComponent = () => <Text>Hello</Text>

test('MyComponent snapshot', () => {
  const tree = renderer.create(<MyComponent />).toJSON();
  console.log(tree.type);
  expect(tree.type).toBe('Text');
});

What will be printed to the console when this test runs?
AError: Text is not defined
BMyComponent
CText
Dundefined
Attempts:
2 left
💡 Hint
What does toJSON() return for a React Native component without importing Text?