Hone logo
Hone
Problems

Testing React Hooks with Asynchronous Results in Jest

Testing React hooks that return promises or asynchronous data can be tricky. This challenge focuses on writing Jest tests for a custom hook that fetches data and manages loading and error states, ensuring you can reliably verify its behavior in different scenarios. Successfully completing this challenge demonstrates proficiency in testing asynchronous operations within React components.

Problem Description

You are tasked with creating and testing a custom React hook called useFetch. This hook will:

  1. Accept a URL as an argument.
  2. Fetch data from the provided URL using fetch.
  3. Return an object with the following properties:
    • data: The fetched data (initially null).
    • loading: A boolean indicating whether the data is being fetched (initially true).
    • error: An error object if an error occurred during fetching (initially null).

You need to write Jest tests to verify the following:

  • Successful Fetch: The hook correctly fetches data and updates the data, loading, and error states when the fetch is successful.
  • Error Handling: The hook correctly handles fetch errors and updates the error state.
  • Loading State: The hook correctly sets loading to true initially and false after the fetch completes (either successfully or with an error).
  • URL Dependency: The hook fetches data from the provided URL.

Examples

Example 1: Successful Fetch

Input: URL = 'https://jsonplaceholder.typicode.com/todos/1'
Output: { data: { userId: 1, id: 1, title: 'delectus aut autem', completed: false }, loading: false, error: null }
Explanation: The hook successfully fetches data from the URL, sets the data property to the fetched object, sets loading to false, and error to null.

Example 2: Error Handling

Input: URL = 'https://invalid-url.com'
Output: { data: null, loading: false, error: Error { message: 'Failed to fetch', name: 'TypeError' } }
Explanation: The hook attempts to fetch data from an invalid URL, resulting in a fetch error. The hook sets loading to false and the error property to the error object.

Example 3: Initial State

Input: URL = 'https://jsonplaceholder.typicode.com/todos/1' (before fetch)
Output: { data: null, loading: true, error: null }
Explanation: Before the fetch operation begins, the hook initializes the data to null, loading to true, and error to null.

Constraints

  • The useFetch hook must be implemented using the fetch API.
  • The tests should use jest.mock to mock the fetch function to control the responses and simulate errors.
  • The tests should use async/await for cleaner asynchronous testing.
  • The hook should be written in TypeScript.
  • The tests should cover all the scenarios described in the Problem Description.

Notes

  • Consider using mockResolvedValue and mockRejectedValue from jest.fn() to simulate successful and error responses from the fetch API.
  • Think about how to properly assert the values of data, loading, and error within your tests.
  • Remember that the loading state should be true before the fetch starts and false after it completes, regardless of success or failure.
  • Focus on isolating the hook's logic by mocking external dependencies like fetch. This allows you to test the hook's internal state management effectively.
Loading editor...
typescript