Hone logo
Hone
Problems

Testing a Simple Counter Component with React Testing Library and Jest

This challenge focuses on writing effective unit tests for a React component using React Testing Library and Jest. Testing components in isolation is crucial for ensuring code quality and preventing regressions, and this exercise will solidify your understanding of how to do so. You'll be testing a simple counter component that increments and resets its value.

Problem Description

You are given a React component called Counter that displays a number and provides buttons to increment and reset the number. Your task is to write a suite of unit tests using React Testing Library and Jest to verify the component's behavior. The tests should cover the initial state, incrementing the counter, and resetting the counter. Focus on testing the component's behavior from the user's perspective, rather than its internal implementation details.

Key Requirements:

  • Initial State: Verify that the counter initially displays the value of 0.
  • Increment Button: Verify that clicking the "Increment" button increases the counter's value by 1.
  • Reset Button: Verify that clicking the "Reset" button sets the counter's value back to 0.
  • User Interaction: Tests should simulate user interactions (clicks) on the buttons.
  • Asynchronous Updates: The component updates asynchronously. Your tests must account for this.

Expected Behavior:

The tests should pass if the component behaves as described above. The tests should be robust and handle potential edge cases gracefully.

Edge Cases to Consider:

  • Repeatedly clicking the increment button.
  • Clicking the reset button after incrementing the counter multiple times.
  • Ensure the component renders correctly initially.

Examples

Example 1:

Input: Counter component rendered
Output: The component displays "0".
Explanation: The component should initialize with a count of 0.

Example 2:

Input: Counter component rendered, then "Increment" button clicked once.
Output: The component displays "1".
Explanation: Clicking the increment button should increase the count by 1.

Example 3:

Input: Counter component rendered, incremented to 5, then "Reset" button clicked.
Output: The component displays "0".
Explanation: Clicking the reset button should set the count back to 0, regardless of the current value.

Constraints

  • Language: TypeScript
  • Libraries: React Testing Library, Jest
  • Component: The Counter component is provided below. Do not modify the component itself.
  • Test Structure: Organize your tests logically within a Jest test file.
  • Asynchronous Handling: Use await or waitFor appropriately to handle asynchronous updates.
  • No Direct DOM Manipulation: Avoid directly manipulating the DOM. Use React Testing Library's query methods.

Notes

  • Focus on testing the behavior of the component, not its internal implementation.
  • React Testing Library encourages testing from the user's perspective.
  • Consider using waitFor or findByText to handle asynchronous updates to the component's display.
  • The Counter component is provided below.
import React, { useState } from 'react';

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

  const increment = () => {
    setCount(count + 1);
  };

  const reset = () => {
    setCount(0);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
      <button onClick={reset}>Reset</button>
    </div>
  );
}

export default Counter;
Loading editor...
typescript