use-swr-promise.test.tsx

Overview

This file contains a suite of automated tests for verifying the behavior of the useSWR React hook when used with promises as fallback data or fallback values. It specifically tests scenarios where promises are passed as fallback values in SWRConfig or as fallbackData in the hook's options, focusing on async data resolution, suspense integration, and error handling.

The tests ensure that useSWR correctly supports promise-based fallbacks by:

These tests validate the integration of React Suspense and Error Boundaries with the SWR cache and data fetching mechanism when promises are used as fallback values.


Detailed Explanations

Imports


Test Suite: describe('useSWR - promise', ...)

This suite contains six test cases that cover different scenarios of using promises as fallback data within useSWR.


Test Case 1: 'should allow passing promises as fallback'

Purpose:
Tests that useSWR supports passing a promise as a fallback value via the fallback prop in SWRConfig.

Implementation details:

Parameters: None (test scope).

Return: None (test assertion).

Usage Example:
Test code shows usage of useSWR with a promise fallback in SWRConfig.


Test Case 2: 'should allow passing promises as fallbackData'

Purpose:
Tests passing a promise as fallbackData option directly in the useSWR hook.

Details:


Test Case 3: 'should suspend when resolving the fallback promise'

Purpose:
Verifies that useSWR suspends rendering while resolving a fallback promise when used with React's Suspense.

Details:


Test Case 4: 'should handle errors with fallback promises'

Purpose:
Tests error handling when fallback promise rejects.

Details:


Test Case 5: 'should handle same fallback promise that is already pending'

Purpose:
Tests that multiple components sharing the same pending fallback promise synchronize correctly.

Details:


Important Implementation Details


Interaction with Other Parts of the System

This file is purely for testing and verification; it does not export components or logic for production use. It ensures the useSWR hook behaves correctly under promise fallback conditions.


Visual Diagram

flowchart TD
    A[useSWR hook] -->|Uses| B[SWRConfig fallback / fallbackData]
    B -->|Can be Promise| C[Promise resolving to data]
    A -->|Returns| D[data]
    A -->|Triggers| E[Component Render]
    E -->|Suspense pending| F[Suspense fallback UI]
    E -->|Error thrown| G[ErrorBoundary fallback UI]
    H[Multiple components] -->|Share| B
    C -->|Resolves or rejects| D

Diagram Explanation:


Summary

This test file rigorously validates that the useSWR hook supports promise-based fallbacks, properly integrates with React Suspense and Error Boundaries, and correctly handles synchronization when multiple components share the same fallback promise. The tests rely on utility functions to simulate delayed async data and ensure UI updates match expected data states. This makes the file crucial for ensuring SWR's robustness in handling asynchronous initial states in React applications.