use-swr-config-callbacks.test.tsx


Overview

This file contains a comprehensive suite of unit tests for verifying the behavior of configuration callbacks in the useSWR React hook. useSWR is a popular data fetching library for React that provides built-in features like caching, revalidation, and request deduplication.

The tests focus on ensuring that various lifecycle callbacks provided via the useSWR configuration options:

are triggered correctly and always use the latest version of the callback when component props or state change.

These tests simulate different data fetching scenarios, including successful responses, errors, retries, slow loading detection, and race conditions with mutations. The file uses React Testing Library to render components, simulate user interactions, and assert on DOM outputs and side effects.


Detailed Description of Tests and Key Functions

Common Utilities Used

These utilities support simulating asynchronous fetches and SWR behaviors.


Test Cases

1. should trigger the onSuccess event with the latest version of the onSuccess callback

Purpose:
Ensure that when the onSuccess callback is updated via new props, the latest callback is called on every successful fetch/revalidation.

Implementation Details:

Parameters:

Return Value:

Example Usage:

<Page text="a" />

2. should trigger the onError event with the latest version of the onError callback

Purpose:
Verify that the onError callback reflects the latest prop values on fetch errors.

Implementation Details:


3. should trigger the onErrorRetry event with the latest version of the onErrorRetry callback

Purpose:
Confirm onErrorRetry callback uses the latest props when scheduling retry logic.

Implementation Details:


4. should trigger the onLoadingSlow and onSuccess event with the lastest version of the callbacks

Purpose:
Test that onLoadingSlow and onSuccess callbacks trigger with updated props.

Implementation Details:


5. should trigger the onDiscarded callback when having a race condition with mutate

Purpose:
Verify that the onDiscarded callback fires when a mutation is discarded due to race conditions.

Implementation Details:


6. should not trigger the onSuccess callback when discarded

Purpose:
Ensure that onSuccess is not called if the fetch is discarded.

Implementation Details:


Important Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

The following flowchart illustrates the main flow and relationships between key functions and tests in this file, focusing on the lifecycle of SWR config callbacks and their triggering conditions.

flowchart TD
    A[Start Test] --> B[Render Page component with useSWR]
    B --> C{useSWR fetcher returns data or error}
    C -->|Success| D[onSuccess callback fired with latest props]
    C -->|Error| E[onError callback fired with latest props]
    E --> F[onErrorRetry triggers revalidation]
    F --> B
    B --> G[User triggers mutate()]
    G --> B
    B --> H{Loading time > loadingTimeout?}
    H -->|Yes| I[onLoadingSlow callback fired]
    H -->|No| J[Continue normal flow]
    B --> K{Race condition detected?}
    K -->|Yes| L[onDiscarded callback fired]
    K -->|No| J
    D --> M[Assertions check callback state]
    E --> M
    F --> M
    I --> M
    L --> M
    M --> N[End Test]

Summary

This test file validates that useSWR configuration callbacks:

It provides critical assurance of the SWR hook’s callback lifecycle integrity, ensuring consumers can rely on accurate and up-to-date side-effect handling in their React components.


End of Documentation for use-swr-config-callbacks.test.tsx