use-swr-refresh.test.tsx


Overview

use-swr-refresh.test.tsx is a comprehensive test suite for verifying the behavior of the useSWR React hook's refresh functionality, particularly focusing on the refreshInterval option and related mechanisms such as deduplication, dynamic interval changes, custom comparison, and key changes during interval-based fetching.

This file uses Jest with React Testing Library to simulate component rendering, user interactions, and timer manipulations (using Jest’s fake timers) to ensure that the useSWR hook refreshes data correctly at specified intervals, handles deduplication properly, and respects dynamic changes to refresh intervals or keys.


Detailed Explanation of Contents

Utilities and Imports


Test Suite: useSWR - refresh

Tests are wrapped inside a describe block named "useSWR - refresh", with Jest's fake timers enabled to control asynchronous timing behavior deterministically.


Individual Tests

1. should rerender automatically on interval


2. should dedupe requests combined with intervals


3. should update data upon interval changes


4. should update data upon interval changes -- changes happened during revalidate


5. should allow use custom compare method


6. custom compare should only be used for comparing data


7. should not let the previous interval timer to set new timer if key changes too fast


8. should not call onSuccess from the previous interval if key has changed


9. should allow using function as an interval


10. should pass updated data to refreshInterval


11. should pass updated data to refreshInterval, when refreshInterval is constant function


12. should disable refresh if function returns 0


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Usage Examples

Basic Auto-Refresh Test Example

// Component that increments count every 200ms
function Page() {
  const { data } = useSWR('key', () => count++, {
    refreshInterval: 200,
    dedupingInterval: 100
  });
  return <div>count: {data}</div>;
}

// Render and advance timers to observe automatic refresh
renderWithConfig(<Page />);
await act(() => advanceTimers(200));
screen.getByText('count: 1');

Using Custom Compare Function

const fetcher = () => ({ timestamp: Date.now(), version: '1.0' });

useSWR('key', fetcher, {
  compare: (a, b) => a?.version === b?.version
});

Dynamic Refresh Interval Based on Data

useSWR('key', fetcher, {
  refreshInterval: (data) => data * 1000 // interval changes as data changes
});

Mermaid Diagram: Test File Structure and Workflow

flowchart TD
    A[useSWR - refresh Test Suite] --> B1[Test: rerender on interval]
    A --> B2[Test: dedupe requests + intervals]
    A --> B3[Test: update on interval changes]
    A --> B4[Test: update on interval changes during revalidate]
    A --> B5[Test: custom compare method]
    A --> B6[Test: custom compare only for data]
    A --> B7[Test: no new timer if key changes too fast]
    A --> B8[Test: no onSuccess from previous interval if key changed]
    A --> B9[Test: function as interval]
    A --> B10[Test: pass updated data to refreshInterval]
    A --> B11[Test: constant function refreshInterval called with updated data]
    A --> B12[Test: disable refresh if interval returns 0]

    B1 --> C1[Component with count state]
    B3 --> C3[Component with refreshInterval state]
    B5 --> C5[Component with custom compare]
    B7 --> C7[Component with key-changing state]

Summary

This test file ensures that the useSWR hook's refresh capabilities are robust, performant, and flexible. It covers automatic interval refresh, deduplication, dynamic intervals, custom data comparison, key changes during refresh, and disabling refresh. The tests simulate realistic use cases and edge cases, using Jest's fake timers to reliably test timing-dependent features.

This thorough testing contributes to the stability of SWR’s data fetching and caching behavior in applications that rely on automatic background refreshes of remote data.