0
0
NextJSframework~20 mins

Testing client components in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next.js Client Component Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Next.js client component?

Consider this simple client component in Next.js 14+ that uses a button to toggle text visibility.

"use client";
import { useState } from 'react';

export default function ToggleText() {
  const [visible, setVisible] = useState(false);
  return (
    
{visible &&

Hello, Next.js!

}
); }

What will the component display initially and after clicking the button once?

NextJS
"use client";
import { useState } from 'react';

export default function ToggleText() {
  const [visible, setVisible] = useState(false);
  return (
    <div>
      <button onClick={() => setVisible(!visible)} aria-pressed={visible}>
        Toggle
      </button>
      {visible && <p>Hello, Next.js!</p>}
    </div>
  );
}
AInitially shows the button and the text 'Hello, Next.js!'. After clicking once, hides the text but keeps the button.
BInitially shows only the button labeled 'Toggle'. Clicking the button does not change the display.
CInitially shows nothing. After clicking once, shows the button and the text 'Hello, Next.js!'.
DInitially shows only the button labeled 'Toggle'. After clicking once, shows the button and the text 'Hello, Next.js!'.
Attempts:
2 left
💡 Hint

Remember that useState(false) means the text is hidden initially. The button toggles the state.

📝 Syntax
intermediate
2:00remaining
Which option correctly mocks a client component's useState hook in a Next.js test?

You want to test a client component that uses useState. Which mocking approach is syntactically correct and will not cause errors?

Ajest.mock('react', () => ({ ...jest.requireActual('react'), useState: () => [true, jest.fn()] }));
Bjest.mock('react', () => ({ ...jest.requireActual('react'), useState: [true, jest.fn()] }));
Cjest.mock('react', () => ({ useState: [true, jest.fn()] }));
Djest.mock('react', () => ({ useState: () => [true, jest.fn()] }));
Attempts:
2 left
💡 Hint

Remember to spread the actual React module to keep other hooks intact and provide a function for useState.

🔧 Debug
advanced
2:00remaining
What error does this Next.js client component test produce?

Given this test code for a client component using useEffect:

import { render, screen } from '@testing-library/react';
import MyComponent from './MyComponent';

test('renders message', () => {
  render();
  expect(screen.getByText('Loaded')).toBeInTheDocument();
});

And the component code:

"use client";
import { useState, useEffect } from 'react';

export default function MyComponent() {
  const [loaded, setLoaded] = useState(false);
  useEffect(() => {
    setLoaded(true);
  }, []);
  return 
{loaded ? 'Loaded' : 'Loading...'}
; }

What error will the test produce?

AError: Unable to find an element with the text: Loaded
BTypeError: Cannot read property 'getByText' of undefined
CThe test passes with no errors.
DSyntaxError: Unexpected token in component code
Attempts:
2 left
💡 Hint

Consider that useEffect runs after render and testing-library queries immediately after render.

state_output
advanced
2:00remaining
What is the final state value after this sequence in a Next.js client component?

In a client component, the state is updated as follows:

const [count, setCount] = useState(0);

setCount(count + 1);
setCount(count + 1);
setCount(prev => prev + 1);

What is the final value of count after these updates?

A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Remember that state updates with the same value of count in closures may batch and overwrite.

🧠 Conceptual
expert
3:00remaining
Which statement best describes testing Next.js client components with server actions?

Next.js 14+ introduces server actions callable from client components. When testing client components that invoke server actions, which is true?

AServer actions are automatically executed during client component tests without additional setup.
BServer actions run in the client environment and can be tested directly without mocks.
CServer actions run on the server and must be mocked or stubbed in client component tests to avoid runtime errors.
DServer actions cannot be called from client components and thus are irrelevant to client component tests.
Attempts:
2 left
💡 Hint

Think about where server actions execute and how tests run client components isolated from the server.