Hone logo
Hone
Problems

Testing Custom React Hooks with Jest

Custom React hooks are a powerful way to encapsulate reusable logic. Effectively testing these hooks is crucial for ensuring their reliability and correctness. This challenge focuses on writing comprehensive unit tests for a custom hook using Jest and React Testing Library.

Problem Description

You are tasked with creating a custom hook, useCounter, that manages a simple counter state. This hook should provide functionality to increment, decrement, and reset the counter. You will then write a series of Jest tests to verify that useCounter behaves as expected under various scenarios.

Key Requirements:

  • useCounter Hook:
    • Accepts an optional initialValue (defaults to 0).
    • Returns an object containing:
      • count: The current value of the counter.
      • increment: A function to increase the count by 1.
      • decrement: A function to decrease the count by 1.
      • reset: A function to set the count back to its initialValue.
  • Jest Tests:
    • Test the initial state of the counter.
    • Test the increment functionality.
    • Test the decrement functionality.
    • Test the reset functionality.
    • Test edge cases, such as decrementing below zero (if applicable, though for this simple hook, it's allowed).
    • Test the hook when initialized with a specific initialValue.

Expected Behavior:

The useCounter hook should accurately manage the counter state, and the provided functions (increment, decrement, reset) should modify the state as intended.

Examples

Example 1: Initial State

Input: Calling useCounter() without an initial value.
Output: { count: 0, increment: function, decrement: function, reset: function }
Explanation: When useCounter is called without arguments, the count should initialize to 0.

Example 2: Incrementing the Counter

Input:
1. Call useCounter() to get { count, increment }.
2. Call increment().
Output: count should be 1.
Explanation: After calling increment, the count increases by 1 from its initial value.

Example 3: Decrementing the Counter

Input:
1. Call useCounter() to get { count, decrement }.
2. Call decrement().
Output: count should be -1.
Explanation: After calling decrement, the count decreases by 1 from its initial value.

Example 4: Resetting the Counter

Input:
1. Call useCounter(5) to get { count, increment, reset }.
2. Call increment() twice. (count is now 7)
3. Call reset().
Output: count should be 5.
Explanation: The reset function restores the counter to its initial value of 5.

Constraints

  • Your solution should be written in TypeScript.
  • You will use Jest and React Testing Library for testing.
  • The useCounter hook should not directly interact with the DOM.
  • Tests should be focused on the logic within the custom hook itself.

Notes

  • You'll need to simulate rendering your custom hook within a simple functional component for testing purposes using renderHook from @testing-library/react-hooks.
  • Consider how to access the returned values from your hook within your tests.
  • Think about the order of operations when testing multiple state updates.
Loading editor...
typescript