All files / src/routes lazyPage.tsx

100% Statements 4/4
100% Branches 0/0
100% Functions 1/1
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32                                            4x     261x 261x 261x        
import React, { lazy } from "react";
 
/**
 * A routed page, loaded on demand.
 *
 * Wraps `React.lazy` and keeps the loader on the component so a test can await
 * the chunk *before* rendering. That matters: with a bare `lazy`, the first
 * render suspends on a real module load, and a test then has to race that
 * resolution against `act()` — which produced either a page that never mounted
 * (83 smoke tests passing against an empty fallback, and coverage halving) or
 * an "a suspended resource finished loading outside act" warning, depending on
 * timing. Preloading removes the race instead of tuning it.
 *
 * ```tsx
 * const Governance = lazyPage(() => import("@/apps/user/pages/Governance"));
 * ```
 */
export interface LazyPage extends React.LazyExoticComponent<React.ComponentType> {
  /** Resolves the page's module. Idempotent — the module cache does the work. */
  load: () => Promise<unknown>;
}
 
export const lazyPage = (
  load: () => Promise<{ default: React.ComponentType }>,
): LazyPage => {
  const component = lazy(load) as LazyPage;
  component.load = load;
  return component;
};
 
export default lazyPage;