use-swr-config.test.tsx


Overview

This file contains unit and integration tests focused on verifying the behavior and configurations of the useSWR hook and SWRConfig context provider from the SWR (stale-while-revalidate) React data fetching library. It validates how SWR's configuration options, such as fallback data, refresh intervals, deduplication intervals, middleware usage, and pause behavior, are correctly applied and propagated through nested contexts.

The tests utilize React Testing Library and React hooks (useState, useEffect) to simulate component behavior under different SWR configurations. The file ensures robustness of the SWR configuration system, including context inheritance, config merging, and dynamic config functions.


Detailed Explanation of the File Contents

Imported Modules


Test Suite: 'useSWR - configs'

This suite tests various aspects of SWR's configuration behavior.


Test Cases

1. should read the config fallback from the context

renderWithConfig(<Page />, {
  fetcher,
  refreshInterval: 100,
  dedupingInterval: 0
})

2. should stop revalidations when config.isPaused returns true

<div onClick={() => setPaused(!paused)}>
  {error ? error : `data: ${data}`}
</div>

3. should expose default config as static property on SWRConfig


4. should expose the default config from useSWRConfig


5. should expose the correctly extended config from useSWRConfig

<SWRConfig value={{ dedupingInterval: 1, use: [middleware1] }}>
  <SWRConfig value={{ dedupingInterval: 2, use: [middleware2] }}>
    <Page />
  </SWRConfig>
</SWRConfig>

6. should ignore parent config when value is functional

<SWRConfig value={parentConfig => ({
  dedupingInterval: 2 + parentConfig.dedupingInterval,
  fallback: { a: 2, c: 2 },
  use: [middleware2]
})}>
  <Page />
</SWRConfig>

7. should not occur error when fallback is undefined


Important Implementation Details


Interaction with Other Parts of the System


Usage Examples Extracted from Tests

Basic useSWR with Config Fallback

function Page() {
  const { data } = useSWR(key)
  return <div>data: {data}</div>
}

renderWithConfig(<Page />, {
  fetcher,
  refreshInterval: 100,
  dedupingInterval: 0
})

Pause Revalidation Based on State

function Page() {
  const [paused, setPaused] = useState(false)
  const { data, error, mutate } = useSWR(key, fetcher, {
    revalidateOnMount: true,
    refreshInterval: 1,
    isPaused: () => paused
  })

  useEffect(() => {
    setPaused(true) // pause on mount
  }, [])

  return (
    <div onClick={() => setPaused(!paused)}>
      {error ? error : `data: ${data}`}
    </div>
  )
}

Mermaid Diagram

flowchart TD
    A[use-swr-config.test.tsx]
    A --> B[Tests "useSWR - configs"]
    B --> C1[Config fallback reading]
    B --> C2[Pause revalidation via isPaused]
    B --> C3[Default config static property]
    B --> C4[useSWRConfig returns default config]
    B --> C5[Nested SWRConfig merges config]
    B --> C6[Functional value config overrides parent]
    B --> C7[Fallback undefined does not error]

    C2 -->|uses| D1[Page component with paused state & mutate]
    C5 -->|uses| D2[Page component with useSWRConfig]
    C6 -->|uses| D3[Page component with functional SWRConfig value]

Summary

This test file thoroughly validates the configuration system of the SWR library, ensuring that:

It is essential for maintaining the reliability and predictability of SWR's behavior in React applications.