use-swr-infinite.test.tsx

Overview

This file contains a comprehensive suite of unit tests for the React hook useSWRInfinite from the SWR library. The useSWRInfinite hook is designed for managing infinite loading scenarios, such as paginated data fetching. These tests validate the hook’s behavior under various conditions, including initial rendering, pagination, mutation, caching, error handling, and configuration options.

The tests use React Testing Library for rendering components and simulating user interactions, and rely on utilities from a local ./utils module to create mock responses, keys, and control asynchronous behavior.


Detailed Description of Tests and Components

The file defines many test cases inside a describe('useSWRInfinite', () => { ... }) block. Each test defines a React component (Page, Comp, or Content) that uses the useSWRInfinite hook and renders its state, then asserts expected behaviors.

Key Imports


Core Concepts Tested


Example Test Case Breakdown

Test: Basic rendering of first page

function Page() {
  const { data, error, isValidating } = useSWRInfinite(
    index => `page-${index}-${key}`,
    infiniteKey => createResponse(infiniteKey)
  )

  return (
    <div>
      <div>data:{data}</div>
      <div>isArray:{Array.isArray(data) ? 'true' : 'false'}</div>
      <div>error:{error}</div>
      <div>isValidating:{isValidating.toString()}</div>
    </div>
  )
}

Important Implementation Details


Interaction with Other System Parts

The file ensures useSWRInfinite behaves correctly as a data fetching hook in React apps, particularly focusing on pagination and cache management scenarios.


Functions and Components

Page (various versions)

Comp

Utilities (from ./utils)


Important Algorithms and Behaviors Tested


Visual Diagram

componentDiagram
  component "useSWRInfinite Hook" as Hook {
    +getKey(index, previousPageData)
    +fetcher(key)
    +data: Array
    +error
    +isValidating
    +size
    +setSize(size | fn)
    +mutate(data?, options?)
  }

  component "Page / Comp Components" as PageComp

  component "Testing Utils" as Utils {
    +createKey()
    +createResponse()
    +sleep()
    +renderWithConfig()
    +renderWithGlobalCache()
  }

  component "SWRConfig Provider" as SWRConfig

  PageComp --> Hook : uses
  PageComp --> Utils : uses for mocks and render
  Hook --> SWRConfig : reads config & cache
  Hook --> SWRConfig : mutate function

Summary

This test file helps ensure that useSWRInfinite remains reliable and predictable across a wide range of usage scenarios in React applications.


Usage Example

A minimal example of using useSWRInfinite in a component as tested:

import useSWRInfinite from 'swr/infinite'

function InfiniteList() {
  const { data, error, size, setSize, isValidating } = useSWRInfinite(
    (index, previousPageData) => previousPageData && !previousPageData.length ? null : `api/data?page=${index + 1}`,
    fetcherFunction,
    { initialSize: 1 }
  )

  if (error) return <div>Error: {error.message}</div>
  if (!data) return <div>Loading...</div>

  return (
    <>
      {data.map((page, i) => (
        <div key={i}>{page.items.map(item => <p key={item.id}>{item.name}</p>)}</div>
      ))}
      <button disabled={isValidating} onClick={() => setSize(size + 1)}>
        Load More
      </button>
    </>
  )
}

This matches the usage patterns thoroughly tested in the file.


End of Documentation for use-swr-infinite.test.tsx