Skip to content

Commit

Permalink
imports
Browse files Browse the repository at this point in the history
Signed-off-by: Logan McAnsh <logan@mcan.sh>
  • Loading branch information
mcansh committed Nov 10, 2022
1 parent b4d3dd4 commit 89fc01b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 84 deletions.
16 changes: 14 additions & 2 deletions packages/remix-server-runtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,17 @@ export type {
UploadHandler,
} from "./reexport";

export type { MemoryHistory } from "./router";
export { createMemoryHistory } from "./router";
export type {
MemoryHistory,
InitialEntry,
StaticHandler,
Location,
AgnosticRouteObject,
} from "./router";
export {
createMemoryHistory,
unstable_createStaticHandler,
matchRoutes,
} from "./router";
export type { Update } from "./router/history";
export type { AgnosticRouteMatch } from "./router/utils";
106 changes: 29 additions & 77 deletions packages/remix-testing/create-remix-stub.tsx
Original file line number Diff line number Diff line change
@@ -1,77 +1,28 @@
import * as React from "react";
import {
unstable_createStaticHandler as createStaticHandler,
matchRoutes,
} from "@remix-run/router";
import { RemixEntry } from "@remix-run/react";
import type {
AssetsManifest,
CatchBoundaryComponent,
EntryContext,
EntryRoute,
RouteData,
RouteManifest,
RouteModules,
ShouldReloadFunction,
} from "@remix-run/react";
import { RemixEntry } from "@remix-run/react";
import type {
ErrorBoundaryComponent,
LinksFunction,
MetaFunction,
MemoryHistory,
} from "@remix-run/server-runtime";
import { json, createMemoryHistory } from "@remix-run/server-runtime";
import type {
AgnosticRouteMatch,
AgnosticRouteObject,
InitialEntry,
StaticHandler,
LoaderFunction,
ActionFunction,
Location,
} from "@remix-run/router";
import type { AgnosticRouteMatch } from "@remix-run/router/dist/utils";
import type { Update } from "@remix-run/router/dist/history";

/**
* Base RouteObject with common props shared by all types of mock routes
*/
type BaseMockRouteObject = {
id?: string;
caseSensitive?: boolean;
path?: string;
element?: React.ReactNode | null;
loader?: LoaderFunction;
action?: ActionFunction;
links?: LinksFunction;
meta?: MetaFunction;
handle?: any;
CatchBoundary?: CatchBoundaryComponent;
ErrorBoundary?: ErrorBoundaryComponent;
unstable_shouldReload?: ShouldReloadFunction;
};

/**
* Index routes must not have children
*/
export declare type MockIndexRouteObject = BaseMockRouteObject & {
children?: undefined;
index: true;
};

/**
* Non-index routes may have children, but cannot have index
*/
export declare type MockNonIndexRouteObject = BaseMockRouteObject & {
children?: MockRouteObject[];
index?: false;
};

/**
* A route object represents a logical route, with (optionally) its child
* routes organized in a tree-like structure.
*/
export declare type MockRouteObject =
| MockIndexRouteObject
| MockNonIndexRouteObject;
MemoryHistory,
StaticHandler,
Update,
} from "@remix-run/server-runtime";
import {
createMemoryHistory,
json,
matchRoutes,
unstable_createStaticHandler as createStaticHandler,
} from "@remix-run/server-runtime";

type RemixStubOptions = {
/**
Expand Down Expand Up @@ -104,7 +55,7 @@ type RemixStubOptions = {
initialIndex?: number;
};

export function createRemixStub(routes: MockRouteObject[]) {
export function createRemixStub(routes: AgnosticRouteObject[]) {
// Setup request handler to handle requests to the mock routes
let { dataRoutes, queryRoute } = createStaticHandler(routes);
return function RemixStub({
Expand Down Expand Up @@ -159,7 +110,7 @@ export function createRemixStub(routes: MockRouteObject[]) {
}

function createRemixContext(
routes: MockRouteObject[],
routes: AgnosticRouteObject[],
currentLocation: Location,
initialLoaderData?: RouteData,
initialActionData?: RouteData
Expand All @@ -185,7 +136,7 @@ function createRemixContext(
};
}

function createManifest(routes: MockRouteObject[]): AssetsManifest {
function createManifest(routes: AgnosticRouteObject[]): AssetsManifest {
return {
routes: createRouteManifest(routes),
entry: { imports: [], module: "" },
Expand All @@ -195,7 +146,7 @@ function createManifest(routes: MockRouteObject[]): AssetsManifest {
}

function createRouteManifest(
routes: MockRouteObject[],
routes: AgnosticRouteObject[],
manifest?: RouteManifest<EntryRoute>,
parentId?: string
): RouteManifest<EntryRoute> {
Expand All @@ -209,7 +160,7 @@ function createRouteManifest(
}

function createRouteModules(
routes: MockRouteObject[],
routes: AgnosticRouteObject[],
routeModules?: RouteModules
): RouteModules {
return routes.reduce((modules, route) => {
Expand All @@ -222,13 +173,14 @@ function createRouteModules(
}

modules[route.id] = {
CatchBoundary: route.CatchBoundary,
ErrorBoundary: route.ErrorBoundary,
CatchBoundary: undefined,
ErrorBoundary: undefined,
// @ts-ignore
default: () => <>{route.element}</>,
handle: route.handle,
links: route.links,
meta: route.meta,
unstable_shouldReload: route.unstable_shouldReload,
links: undefined,
meta: undefined,
unstable_shouldReload: undefined,
};
return modules;
}, routeModules || {});
Expand Down Expand Up @@ -273,7 +225,7 @@ function monkeyPatchFetch(
}

function convertToEntryRoute(
route: MockRouteObject,
route: AgnosticRouteObject,
parentId?: string
): EntryRoute {
return {
Expand All @@ -285,13 +237,13 @@ function convertToEntryRoute(
hasAction: !!route.action,
hasLoader: !!route.loader,
module: "",
hasCatchBoundary: !!route.CatchBoundary,
hasErrorBoundary: !!route.ErrorBoundary,
hasCatchBoundary: false,
hasErrorBoundary: false,
};
}

function convertToEntryRouteMatch(
routes: AgnosticRouteMatch<string, MockRouteObject>[]
routes: AgnosticRouteMatch<string, AgnosticRouteObject>[]
) {
return routes.map((match) => {
return {
Expand All @@ -305,7 +257,7 @@ function convertToEntryRouteMatch(
// Converts route data from a path based index to a route id index value.
// e.g. { "/post/:postId": post } to { "0": post }
function convertRouteData(
routes: MockRouteObject[],
routes: AgnosticRouteObject[],
initialRouteData?: RouteData,
routeData: RouteData = {}
): RouteData | undefined {
Expand Down
5 changes: 0 additions & 5 deletions packages/remix-testing/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export { createRemixStub } from "./create-remix-stub";
export type {
MockIndexRouteObject,
MockNonIndexRouteObject,
MockRouteObject,
} from "./create-remix-stub";

0 comments on commit 89fc01b

Please sign in to comment.