utils.tsx


Overview

The utils.tsx file provides a collection of utility functions primarily designed to facilitate testing in React applications that use the SWR data fetching library. The utilities focus on asynchronous control (e.g., delaying execution, batching updates), rendering components with SWR configurations, simulating DOM events, generating unique keys, and mocking browser behaviors such as visibility state and console errors.

This file is intended to streamline writing robust and clean tests by abstracting common testing patterns, especially when working with asynchronous data fetching and React's concurrent rendering behavior.


Detailed Explanations

1. sleep(time: number): Promise<void>


2. createResponse<T>(response: T, { delay } = { delay: 10 }): Promise<T>


3. nextTick(): Promise<void>


4. focusOn(element: any): Promise<void>


5. createKey(): string


6. _renderWithConfig(element: React.ReactElement, config: SWRConfig['value']): ReturnType<typeof render>


7. renderWithConfig(element: React.ReactElement, config?: SWRConfig['value']): ReturnType<typeof render>


8. renderWithGlobalCache(element: React.ReactElement, config?: SWRConfig['value']): ReturnType<typeof render>


9. hydrateWithConfig(element: React.ReactElement, container: HTMLElement, config?: SWRConfig['value']): ReturnType<typeof render>


10. mockVisibilityHidden(): () => void


11. executeWithoutBatching(fn: () => any): Promise<void>


12. mockConsoleForHydrationErrors(): () => void


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram: Flowchart of Utility Functions and Their Relationships

flowchart TD
  A[sleep(time)] -->|used by| B[nextTick()]
  B -->|wraps| act
  C[createResponse(response, delay)] -->|returns delayed Promise| D[Promise<T>]
  E[focusOn(element)] -->|wraps| act
  E -->|fires event| F[fireEvent.focus]
  G[createKey()] -->|returns| H[String key]
  I[_renderWithConfig(element, config)] -->|wraps| J[SWRConfig Provider]
  I -->|calls| K[render()]
  L[renderWithConfig(element, config?)] -->|calls| I with new Map() as provider
  M[renderWithGlobalCache(element, config?)] -->|calls| I with default provider
  N[hydrateWithConfig(element, container, config?)] -->|render with hydrate:true| O[render()]
  N -->|wraps| J
  P[mockVisibilityHidden()] -->|mocks| Q[document.visibilityState]
  P -->|returns| R[restore function]
  S[executeWithoutBatching(fn)] -->|disables| T[IS_REACT_ACT_ENVIRONMENT]
  S -->|executes| fn
  S -->|restores| T
  U[mockConsoleForHydrationErrors()] -->|mocks| V[console.error]
  U -->|returns| W[restore function with assertion]

Summary

utils.tsx is a specialized utility module to aid testing React components that use SWR. It provides asynchronous helpers, rendering wrappers with SWR contexts, event simulation, and environment mocking. These utilities help maintain test isolation, simulate real-world scenarios (like network delays and visibility changes), and handle React 18’s updated testing behaviors. The file plays a critical role in ensuring reliable and maintainable tests across the codebase.