0
0
NextJSframework~20 mins

React Testing Library integration in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
React Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1: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');
});
AThe test checks if the button has a specific CSS class.
BThe test checks if the button is disabled by default.
CThe test checks if clicking the button triggers an alert.
DThe test checks if the button renders with the label 'Click me'.
Attempts:
2 left
💡 Hint
Look at what the expect statement is verifying.
state_output
intermediate
1: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');
});
AThe test fails because the button text remains 'Count: 0' after click.
BThe test passes because the button text updates to 'Count: 1' after click.
CThe test throws an error because fireEvent.click is not defined.
DThe test fails because the button is disabled and cannot be clicked.
Attempts:
2 left
💡 Hint
Check how the state changes on click and what the test expects.
📝 Syntax
advanced
2: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?
Aawait screen.findByText('Data loaded');
Bscreen.getByText('Data loaded');
Cawait screen.queryByText('Data loaded');
Dscreen.findByText('Data loaded');
Attempts:
2 left
💡 Hint
Remember which query returns a promise and waits for the element.
🔧 Debug
advanced
2: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();
});
AThe button's visible text is 'Send', but the test looks for name 'Submit'.
BThe button element is not rendered because render is missing a wrapper.
CgetByRole cannot find buttons with type 'submit'.
DThe test is missing an async wait for the button to appear.
Attempts:
2 left
💡 Hint
Check the button's label text and the name option in getByRole.
🧠 Conceptual
expert
2: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.
AReact Testing Library requires less setup because it mocks all network requests automatically.
BReact Testing Library allows shallow rendering which Enzyme does not support.
CReact Testing Library encourages testing components like users interact with them, focusing on accessibility and behavior rather than implementation details.
DReact Testing Library uses snapshot testing exclusively, which Enzyme cannot do.
Attempts:
2 left
💡 Hint
Think about how tests should reflect user experience.