Skip to content

Commit

Permalink
Fix test types and re-enable DataHashRouter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Oct 11, 2022
1 parent 892238b commit 7ea0fcb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ testDomRouter("<DataBrowserRouter>", createBrowserRouter, (url) =>
getWindowImpl(url, false)
);

// testDomRouter("<DataHashRouter>", createHashRouter, (url) =>
// getWindowImpl(url, true)
// );
testDomRouter("<DataHashRouter>", createHashRouter, (url) =>
getWindowImpl(url, true)
);

let router: Router | null = null;

Expand Down
1 change: 0 additions & 1 deletion packages/react-router/__tests__/matchRoutes-test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AgnosticRouteObject } from "@remix-run/router";
import * as React from "react";
import type { RouteObject } from "react-router";
import { matchRoutes } from "react-router";
Expand Down
54 changes: 33 additions & 21 deletions packages/router/__tests__/router-test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
/* eslint-disable jest/valid-title */
import type {
ActionFunction,
AgnosticDataRouteObject,
AgnosticRouteMatch,
Fetcher,
RouterFetchOptions,
HydrationState,
InitialEntry,
LoaderFunction,
Router,
RouterNavigateOptions,
StaticHandler,
Expand All @@ -28,36 +26,42 @@ import {
} from "../index";

// Private API
import type { AgnosticRouteObject, TrackedPromise } from "../utils";
import type {
AgnosticIndexRouteObject,
AgnosticNonIndexRouteObject,
AgnosticRouteObject,
TrackedPromise,
} from "../utils";
import { AbortedDeferredError } from "../utils";

///////////////////////////////////////////////////////////////////////////////
//#region Types and Utils
///////////////////////////////////////////////////////////////////////////////

// Routes passed into setup() should just have a boolean for loader/action
// indicating they want a stub
type TestRouteObject = Pick<
AgnosticDataRouteObject,
// indicating they want a stub. They get enhanced back to AgnosticRouteObjects
// by our test harness
type TestIndexRouteObject = Pick<
AgnosticIndexRouteObject,
"id" | "index" | "path" | "shouldRevalidate"
> & {
loader?: boolean;
action?: boolean;
hasErrorBoundary?: boolean;
children?: TestRouteObject[];
};

// Enhanced route objects are what is passed to the router for testing, as they
// have been enhanced with stubbed loaders and actions
type EnhancedRouteObject = Omit<
TestRouteObject,
"loader" | "action" | "children"
type TestNonIndexRouteObject = Pick<
AgnosticNonIndexRouteObject,
"id" | "index" | "path" | "shouldRevalidate"
> & {
loader?: LoaderFunction;
action?: ActionFunction;
children?: EnhancedRouteObject[];
loader?: boolean;
action?: boolean;
hasErrorBoundary?: boolean;
children?: TestRouteObject[];
};

type TestRouteObject = TestIndexRouteObject | TestNonIndexRouteObject;

// A helper that includes the Deferred and stubs for any loaders/actions for the
// route allowing fine-grained test execution
type InternalHelpers = {
Expand Down Expand Up @@ -251,7 +255,7 @@ function setup({
// active navigation loader/action
function enhanceRoutes(_routes: TestRouteObject[]) {
return _routes.map((r) => {
let enhancedRoute: EnhancedRouteObject = {
let enhancedRoute: AgnosticRouteObject = {
...r,
loader: undefined,
action: undefined,
Expand Down Expand Up @@ -313,7 +317,7 @@ function setup({
);
};
}
if (r.children) {
if (!r.index && r.children) {
enhancedRoute.children = enhanceRoutes(r.children);
}
return enhancedRoute;
Expand Down Expand Up @@ -438,7 +442,11 @@ function setup({
href: string,
navigationId: number
): NavigationHelpers {
let matches = matchRoutes(enhancedRoutes, href);
invariant(
currentRouter?.routes,
"No currentRouter.routes available in getNavigationHelpers"
);
let matches = matchRoutes(currentRouter.routes, href);

// Generate helpers for all route matches that contain loaders
let loaderHelpers = getHelpers(
Expand Down Expand Up @@ -473,7 +481,11 @@ function setup({
navigationId: number,
opts?: RouterNavigateOptions
): FetcherHelpers {
let matches = matchRoutes(enhancedRoutes, href);
invariant(
currentRouter?.routes,
"No currentRouter.routes available in getFetcherHelpers"
);
let matches = matchRoutes(currentRouter.routes, href);
invariant(currentRouter, "No currentRouter available");
let search = parsePath(href).search || "";
let hasNakedIndexQuery = new URLSearchParams(search)
Expand Down Expand Up @@ -506,7 +518,7 @@ function setup({
if (opts?.formMethod === "post") {
if (currentRouter.state.navigation?.location) {
let matches = matchRoutes(
enhancedRoutes,
currentRouter.routes,
currentRouter.state.navigation.location
);
invariant(matches, "No matches found for fetcher");
Expand Down Expand Up @@ -1003,7 +1015,7 @@ describe("a router", () => {
});

it("throws if it finds index routes with children", async () => {
let routes = [
let routes: AgnosticRouteObject[] = [
// @ts-expect-error
{
index: true,
Expand Down

0 comments on commit 7ea0fcb

Please sign in to comment.