use-swr-infinite-preload.test.tsx


Overview

This file contains a comprehensive test suite for verifying the behavior of the preload functionality in conjunction with the useSWRInfinite React hook from the SWR library. SWR (stale-while-revalidate) is a React Hooks library for data fetching and caching.

The tests focus on ensuring that preloading data with preload behaves correctly when used with useSWRInfinite, which supports infinite loading of paginated data. The suite covers scenarios including:

By thoroughly testing these cases, the file ensures that the preload mechanism integrates smoothly with infinite loading patterns and other SWR features.


File Structure and Key Functions

This file is a pure test file and does not export any functions or classes. It imports utilities and React testing helpers to simulate and assert different behaviors.

Imports:


Test Suite: useSWRInfinite - preload

The entire file is wrapped inside a describe block named "useSWRInfinite - preload", grouping all test cases related to this behavior.


Helper Function

const getKeyFunction = (key: string) => (index: number) => `page-${index}-${key}`

Test Cases and Their Details:


1. preloading useSWRInfinite should produce the same result

preload(getKey(0), fetcher)
renderWithConfig(<Page />)
await screen.findByText('data:true')

2. preload the fetcher function

preload(getKey(0), fetcher)
expect(fetcher).toBeCalledTimes(1)
renderWithConfig(<Page />)
await screen.findByText('data:foo')
expect(fetcher).toBeCalledTimes(1)

3. should avoid preloading the resource multiple times


4. should be able to preload resources in effects


5. preload the fetcher function with the suspense mode


6. avoid suspense waterfall by prefetching the resources (Skipped)


7. reset the preload result when the preload function gets an error


8. dedupe requests during preloading


9. should pass serialize key to fetcher


10. should not break parallel option


11. should be able to preload multiple page


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples

Basic preload usage with useSWRInfinite

const getKey = (index) => `page-${index}-myKey`
const fetcher = (key) => fetchDataForKey(key)

preload(getKey(0), fetcher)

function Page() {
  const { data } = useSWRInfinite(getKey, fetcher)
  return <div>data: {data ? 'loaded' : 'loading'}</div>
}

Preload in effect

useEffect(() => {
  preload(getKey(0), fetcher)
}, [])

Mermaid Diagram: Flowchart of Test Suite Structure

flowchart TD
    A[useSWRInfinite - preload Tests]

    A --> B1[Basic preload correctness]
    A --> B2[Fetcher call count tests]
    A --> B3[Multiple preload deduplication]
    A --> B4[Preload inside useEffect]
    A --> B5[SWR suspense mode preload]
    A --> B6[Waterfall avoidance (skipped)]
    A --> B7[Error handling during preload]
    A --> B8[Request deduplication during preload]
    A --> B9[Serialized key passed to fetcher]
    A --> B10[Parallel fetching option test]
    A --> B11[Multiple page preloading]

    B1 --> C1[Preload key, render, assert data]
    B2 --> C2[Check fetcher call counts]
    B3 --> C3[Preload same key multiple times]
    B4 --> C4[Preload in useEffect, user interaction]
    B5 --> C5[Suspense + Profiler integration]
    B7 --> C7[Preload error, mutate, revalidate]
    B8 --> C8[Delayed fetcher, multiple renders]
    B9 --> C9[Preload with function key]
    B10 --> C10[Parallel requests with delays]
    B11 --> C11[Preload multiple pages separately]

Summary

This test file rigorously verifies the integration of preload with useSWRInfinite in various realistic scenarios, ensuring fast, deduplicated, and correctly handled data fetching for infinite paginated resources in React applications using SWR. It covers preload correctness, error handling, suspense compatibility, and parallel fetching, providing a robust foundation for SWR infinite loading functionality.


End of Documentation