Examples and End-to-End Tests

This module provides comprehensive example applications and end-to-end (E2E) tests that illustrate practical use cases of the SWR data fetching and state management library. It serves to demonstrate the library's capabilities in real-world scenarios and to validate core behaviors such as concurrency handling, retry mechanisms, suspense integration, mutation workflows, and subscription-based real-time updates.


Purpose and Core Concepts

The Examples and End-to-End Tests topic exists to:

By walking through these examples and tests, users gain insight into the practical application of SWR's advanced features beyond the core API.


Key Example Applications

1. Infinite Scroll Example

This example demonstrates how to implement paginated data fetching with infinite loading using the useSWRInfinite hook. It manages page sizes, concatenates data from multiple pages, and supports refreshing and clearing the data set.

Interaction with other parts

Code snippet from examples/infinite/pages/index.js:

const { data, error, mutate, size, setSize, isValidating } = useSWRInfinite(
  (index) =>
    `https://api.github.com/repos/${repo}/issues?per_page=${PAGE_SIZE}&page=${index + 1}`,
  fetch
)

const issues = data ? [].concat(...data) : []

2. Subscription Example

This example illustrates how to use useSWRSubscription to subscribe to an external event emitter, receiving real-time data updates and error notifications.

Interaction with other parts

Code snippet from examples/subscription/pages/index.js:

const { data, error } = useSWRSubscription('my-sub', (key, { next }) => {
  event.on("data", (value) => next(undefined, value));
  event.on("error", (err) => next(err));
  start();
  return () => {
    stopped = true;
  };
})

End-to-End (E2E) Testing Suite

The E2E tests are critical to maintaining the reliability of the SWR library across complex scenarios. They cover:

Example E2E workflows

Interaction with other parts

Code snippet from e2e/test/initial-render.test.ts:

test('should be able to retry in suspense with mutate', async ({ page }) => {
  await page.goto('./suspense-retry-mutate', { waitUntil: 'commit' })
  await expect(page.getByText('Something went wrong')).toBeVisible()
  await page.getByRole('button', { name: 'retry' }).click()
  await expect(page.getByText('data: SWR suspense retry works')).toBeVisible()
})

Module Workflow Visualization

The following flowchart illustrates the overall workflow of how example applications interact with SWR's core hooks and how E2E tests validate these interactions:

flowchart TD
  A[Example React Component] --> B[Uses SWR Hook]
  B --> C[Internal SWR Middleware & Cache Management]
  C --> D[Data Fetching / Subscription Setup]
  D --> E[API or External Data Source]
  E -->|Data or Error| D
  D -->|Update| C
  C -->|Trigger Rerender| A

  subgraph E2E Tests
    T1[Test User Interaction]
    T2[Assert UI State]
  end

  A --> T1
  T1 --> B
  T2 --> A

This diagram shows the cyclic nature of data flow from components through SWR internals to data sources and back, with E2E tests simulating user behavior and asserting expected UI changes.


Summary of File Relationships

File Path

Role

examples/infinite/pages/index.js

Infinite scroll example using useSWRInfinite

examples/infinite/libs/fetch.js

Fetch utility used in infinite example

examples/subscription/pages/index.js

Real-time subscription example using useSWRSubscription

e2e/test/initial-render.test.ts

E2E tests covering suspense, retry, preload

e2e/test/mutate-server-action.test.ts

E2E tests verifying mutation workflows

These files illustrate and verify the practical use and robustness of SWR's advanced features.


This documentation explains the design intent, functionality, and interactions of the Examples and End-to-End Tests module, referencing key example code and tests to clarify how users can leverage and validate the SWR data fetching library in real-world scenarios.