Challenge - 5 Problems
React Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What does this test check for?
Given this React component and test, what behavior does the test verify?
NextJS
import { render, screen } from '@testing-library/react'; import Button from './Button'; test('renders button with correct label', () => { render(<Button label="Click me" />); const buttonElement = screen.getByRole('button'); expect(buttonElement).toHaveTextContent('Click me'); });
Attempts:
2 left
💡 Hint
Look at what the expect statement is verifying.
✗ Incorrect
The test renders the Button component with label 'Click me' and verifies the button's text content matches that label.
❓ state_output
intermediate1:30remaining
What is the output of this test after clicking?
Consider this component and test. What will the test output be after simulating a click?
NextJS
import { render, screen, fireEvent } from '@testing-library/react'; import { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return <button onClick={() => setCount(count + 1)}>Count: {count}</button>; } test('increments count on click', () => { render(<Counter />); const button = screen.getByRole('button'); fireEvent.click(button); expect(button).toHaveTextContent('Count: 1'); });
Attempts:
2 left
💡 Hint
Check how the state changes on click and what the test expects.
✗ Incorrect
The Counter component increments count state on click, so the button text updates to 'Count: 1'. The test expects this updated text and passes.
📝 Syntax
advanced2:00remaining
Which test code snippet correctly waits for async content?
You want to test a component that fetches data and shows it after loading. Which test snippet correctly waits for the data to appear?
Attempts:
2 left
💡 Hint
Remember which query returns a promise and waits for the element.
✗ Incorrect
findByText returns a promise and waits for the element to appear, so awaiting it correctly handles async content.
🔧 Debug
advanced2:00remaining
Why does this test fail with 'Unable to find role' error?
This test fails with an error saying it cannot find a button role. What is the cause?
NextJS
import { render, screen } from '@testing-library/react'; test('finds submit button', () => { render(<button type="submit">Send</button>); const btn = screen.getByRole('button', { name: 'Submit' }); expect(btn).toBeInTheDocument(); });
Attempts:
2 left
💡 Hint
Check the button's label text and the name option in getByRole.
✗ Incorrect
getByRole with name option matches accessible name, which is the button's visible text. The button text is 'Send', but the test looks for 'Submit', so it fails.
🧠 Conceptual
expert2:30remaining
What is the main benefit of using React Testing Library over Enzyme?
Choose the best explanation for why React Testing Library is preferred for testing React components compared to Enzyme.
Attempts:
2 left
💡 Hint
Think about how tests should reflect user experience.
✗ Incorrect
React Testing Library promotes testing from the user's perspective by querying DOM elements as users would, improving test reliability and accessibility focus. Enzyme focuses more on component internals.