use-swr-laggy.test.tsx

Overview

The use-swr-laggy.test.tsx file contains a suite of automated tests for verifying the behavior of the useSWR and useSWRInfinite React hooks, specifically focusing on the keepPreviousData option. These hooks are part of the SWR (stale-while-revalidate) data fetching library commonly used in React applications.

The primary purpose of these tests is to ensure that when the keepPreviousData option is enabled:

These tests use React Testing Library to simulate user interactions and lifecycle events, and verify the data states over time.


Detailed Explanation of Contents

Imports and Utilities Used


Test Suites and Test Cases

All tests are wrapped in a top-level describe block titled 'useSWR - keep previous data'.


1. should keep previous data when key changes when keepPreviousData is enabled


2. should keep previous data when sharing the cache


3. should keep previous data even if there is fallback data


4. should always return the latest data


5. should keep previous data for the useSWRInfinite hook


6. should support changing the keepPreviousData option


7. should re-render when returned data and fallbackData is the same and keepPreviousData is enabled


8. should work keepPreviousData without changing the key


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Although this is a test file, the patterns exemplify how useSWR can be used with keepPreviousData:

import useSWR from 'swr'

function Example() {
  const { data } = useSWR('/api/data', fetcher, { keepPreviousData: true })

  return <div>{data ?? 'Loading...'}</div>
}

Changing the key (e.g., URL) will keep showing the old data until the new data arrives, improving UI stability.


Mermaid Diagram: Component and Function Flow

The file primarily contains test components inside test cases, each with a similar structure. The main relationships are between:

flowchart TD
    TestSuite["describe('useSWR - keep previous data')"]
    subgraph TestCases
      T1["it('should keep previous data when key changes')"]
      T2["it('should keep previous data when sharing the cache')"]
      T3["it('should keep previous data with fallback data')"]
      T4["it('should always return latest data')"]
      T5["it('should keep previous data for useSWRInfinite')"]
      T6["it('should support changing keepPreviousData option')"]
      T7["it('should re-render when data and fallbackData are same')"]
      T8["it('should work keepPreviousData without key change')"]
    end

    subgraph Components
      App1["App Component (T1)"]
      App2["App Component (T2)"]
      App3["App Component (T3)"]
      App4["App Component (T4)"]
      App5["App Component (T5)"]
      App6["App Component (T6)"]
      App7["App Component (T7)"]
      App8["App Component (T8)"]
    end

    subgraph Hooks
      useSWR1["useSWR with keepPreviousData"]
      useSWR2["useSWR (normal)"]
      useSWRInf["useSWRInfinite"]
    end

    subgraph Utilities
      fetcher["fetcher (createResponse)"]
      createKey["createKey()"]
      render["renderWithConfig()"]
      sleep["sleep()"]
    end

    TestSuite --> TestCases
    T1 --> App1
    T2 --> App2
    T3 --> App3
    T4 --> App4
    T5 --> App5
    T6 --> App6
    T7 --> App7
    T8 --> App8

    App1 --> useSWR1
    App2 --> useSWR1
    App2 --> useSWR2
    App3 --> useSWR1
    App3 --> useSWR2
    App4 --> useSWR1
    App5 --> useSWRInf
    App6 --> useSWR1
    App7 --> useSWR1
    App8 --> useSWR1
    App8 --> useSWR2

    useSWR1 --> fetcher
    useSWR2 --> fetcher
    useSWRInf --> fetcher

    App1 --> createKey
    App2 --> createKey
    App3 --> createKey
    App4 --> createKey
    App5 --> createKey
    App6 --> createKey
    App7 --> -- keys array --
    App8 --> createKey

    TestCases --> render
    TestCases --> sleep

Summary

This test file is critical to ensure the robustness and user experience quality of SWR's laggy data feature during asynchronous data fetching and key transitions.