use-swr-middlewares.test.tsx


Overview

This file contains a comprehensive test suite for verifying the middleware functionality of the useSWR React hook from the SWR library. SWR (stale-while-revalidate) is a popular data fetching library that provides built-in support for caching, revalidation, and request deduplication using React hooks.

The primary focus of this test file is to ensure that the middleware mechanism within useSWR works correctly in various scenarios. Middleware in SWR allows for extending or customizing the behavior of the hook by wrapping the original hook with additional logic (e.g., logging, modifying keys, caching strategies).

The tests cover:

This file uses React Testing Library (@testing-library/react) to render components and assert on DOM output, along with Jest mocks for tracking middleware effects.


Detailed Explanation of Test Cases and Middleware Usage

Common Utilities Imported


1. Test: should use middleware

const loggerMiddleware: Middleware = useSWRNext => (key, fetcher, config) => {
  console.log(key)
  return useSWRNext(key, fetcher, config)
}

2. Test: should pass original keys to middleware


3. Test: should pass null fetcher to middleware


4. Test: should support 'use' option in context


5. Test: should support extending middleware via context and per-hook config


6. Test: should use the correct middleware order in 'withMiddleware'


7. Test: should support react hooks inside middleware


8. Test: should pass modified keys to the next middleware and useSWR


9. Test: should send the non-serialized key to the middleware


Important Implementation Details


Interaction with Other Modules

This file is primarily a test utility and is not directly part of the production runtime but ensures core middleware functionality correctness.


Usage Examples

Below is a distilled example of how middleware is applied and tested:

const loggerMiddleware: Middleware = useSWRNext => (key, fetcher, config) => {
  console.log('middleware key:', key)
  return useSWRNext(key, fetcher, config)
}

function Page() {
  const { data } = useSWR('my-key', () => fetchData(), {
    use: [loggerMiddleware]
  })
  return <div>{data}</div>
}

The test verifies that loggerMiddleware is called with 'my-key' and that the component renders data as expected.


Mermaid Diagram: Middleware Flowchart

This flowchart illustrates the middleware wrapping and execution sequence in useSWR as tested in this file:

flowchart TD
    A[useSWR hook call] --> B{Middleware Stack}
    B -->|Middleware 1| C1[Logger Middleware]
    B -->|Middleware 2| C2[Decorating Key Middleware]
    B -->|Middleware n| Cn[Lazy Middleware]
    C1 --> D[Modified key / fetcher]
    C2 --> D
    Cn --> D
    D --> E[useSWRNext (original SWR hook)]
    E --> F[Data fetching & caching]
    F --> G[Return data to component]

Summary

The use-swr-middlewares.test.tsx file rigorously tests the middleware extension points of the useSWR React hook. It validates middleware parameters, composition order, React hook compatibility, key transformation, and middleware injection via hook options and context providers. The tests ensure that middleware behaves predictably, enabling developers to customize data fetching behavior reliably in SWR-powered applications.