UNPKG

4.19 kBJavaScriptView Raw
1/**
2 * react-router v8.3.0
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import { RouterContextProvider, convertRoutesToDataRoutes } from "../../router/utils.js";
12import { Outlet, RouterProvider, createMemoryRouter, withComponentProps, withErrorBoundaryProps, withHydrateFallbackProps } from "../../components.js";
13import { FrameworkContext } from "./components.js";
14import * as React$1 from "react";
15//#region lib/dom/ssr/routes-test-stub.tsx
16/**
17* Creates a React component that renders the provided routes in a test-friendly
18* React Router context.
19*
20* Use this to unit test components that rely on router context, such as
21* `loaderData`, `actionData`, and route matches.
22*
23* @public
24* @category Utils
25* @mode framework
26* @mode data
27* @param routes The route objects to render in the test router.
28* @param _context An optional {@link RouterContextProvider} for supplying
29* application context values to route middleware, loaders, and actions.
30* @returns A React component that renders the test router.
31*/
32function createRoutesStub(routes, _context) {
33 return function RoutesTestStub({ initialEntries, initialIndex, hydrationData, future }) {
34 let routerRef = React$1.useRef(null);
35 let frameworkContextRef = React$1.useRef(null);
36 if (routerRef.current == null || frameworkContextRef.current == null) {
37 frameworkContextRef.current = {
38 future: {},
39 manifest: {
40 routes: {},
41 entry: {
42 imports: [],
43 module: ""
44 },
45 url: "",
46 version: ""
47 },
48 routeModules: {},
49 ssr: false,
50 isSpaMode: false,
51 routeDiscovery: {
52 mode: "lazy",
53 manifestPath: "/__manifest"
54 }
55 };
56 routerRef.current = createMemoryRouter(processRoutes(convertRoutesToDataRoutes(routes, (r) => r), _context ?? new RouterContextProvider(), frameworkContextRef.current.manifest, frameworkContextRef.current.routeModules), {
57 initialEntries,
58 initialIndex,
59 hydrationData
60 });
61 }
62 return /* @__PURE__ */ React$1.createElement(FrameworkContext.Provider, { value: frameworkContextRef.current }, /* @__PURE__ */ React$1.createElement(RouterProvider, { router: routerRef.current }));
63 };
64}
65function processRoutes(routes, context, manifest, routeModules, parentId) {
66 return routes.map((route) => {
67 if (!route.id) throw new Error("Expected a route.id in react-router processRoutes() function");
68 let newRoute = {
69 id: route.id,
70 path: route.path,
71 index: route.index,
72 Component: route.Component ? withComponentProps(route.Component) : void 0,
73 HydrateFallback: route.HydrateFallback ? withHydrateFallbackProps(route.HydrateFallback) : void 0,
74 ErrorBoundary: route.ErrorBoundary ? withErrorBoundaryProps(route.ErrorBoundary) : void 0,
75 action: route.action ? (args) => route.action({
76 ...args,
77 context
78 }) : void 0,
79 loader: route.loader ? (args) => route.loader({
80 ...args,
81 context
82 }) : void 0,
83 middleware: route.middleware ? route.middleware.map((mw) => (...args) => mw({
84 ...args[0],
85 context
86 }, args[1])) : void 0,
87 handle: route.handle,
88 shouldRevalidate: route.shouldRevalidate
89 };
90 let entryRoute = {
91 id: route.id,
92 path: route.path,
93 index: route.index,
94 parentId,
95 hasAction: route.action != null,
96 hasLoader: route.loader != null,
97 hasClientAction: false,
98 hasClientLoader: false,
99 hasClientMiddleware: false,
100 hasErrorBoundary: route.ErrorBoundary != null,
101 module: "build/stub-path-to-module.js",
102 clientActionModule: void 0,
103 clientLoaderModule: void 0,
104 clientMiddlewareModule: void 0,
105 hydrateFallbackModule: void 0
106 };
107 manifest.routes[newRoute.id] = entryRoute;
108 routeModules[route.id] = {
109 default: newRoute.Component || Outlet,
110 ErrorBoundary: newRoute.ErrorBoundary || void 0,
111 handle: route.handle,
112 links: route.links,
113 meta: route.meta,
114 shouldRevalidate: route.shouldRevalidate
115 };
116 if (route.children) newRoute.children = processRoutes(route.children, context, manifest, routeModules, newRoute.id);
117 return newRoute;
118 });
119}
120//#endregion
121export { createRoutesStub };