Skip to content

Commit

Permalink
Refactor tests to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
chaance authored and mjackson committed Jul 15, 2021
1 parent 199fd08 commit f8a9020
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { act } from "react-dom/test-utils";
import { MemoryRouter as Router, Routes, Route, Link } from "react-router-dom";

describe("A <Link> click", () => {
let node;
let node: HTMLDivElement;
beforeEach(() => {
node = document.createElement("div");
document.body.appendChild(node);
Expand Down Expand Up @@ -62,7 +62,7 @@ describe("A <Link> click", () => {
describe("when preventDefault is used on the click handler", () => {
it("stays on the same page", () => {
function Home() {
function handleClick(event) {
function handleClick(event: React.MouseEvent<HTMLAnchorElement>) {
event.preventDefault();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";
import { act, create as createTestRenderer } from "react-test-renderer";
import { MemoryRouter as Router, Routes, Route, Link } from "react-router-dom";
import type { ReactTestRenderer } from "react-test-renderer";

describe("Link href", () => {
describe("absolute", () => {
Expand All @@ -14,7 +15,7 @@ describe("Link href", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -43,7 +44,7 @@ describe("Link href", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -72,7 +73,7 @@ describe("Link href", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -101,7 +102,7 @@ describe("Link href", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import * as React from "react";
import * as ReactDOM from "react-dom";
import { act } from "react-dom/test-utils";
import { Router, Routes, Route, Link } from "react-router-dom";
import type { History } from "history";

function createHref({ pathname = "/", search = "", hash = "" }) {
function createHref({ pathname = "/", search = "", hash = "" }): string {
return pathname + search + hash;
}

function createMockHistory({ pathname = "/", search = "", hash = "" }) {
let location = { pathname, search, hash };
let location: Partial<History["location"]> = { pathname, search, hash };
return {
action: "POP",
location,
Expand All @@ -18,13 +19,17 @@ function createMockHistory({ pathname = "/", search = "", hash = "" }) {
go() {},
back() {},
forward() {},
listen() {},
block() {}
};
listen() {
return () => {};
},
block() {
return () => {};
}
} as History;
}

describe("Link push and replace", () => {
let node;
let node: HTMLDivElement;
beforeEach(() => {
node = document.createElement("div");
document.body.appendChild(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Route,
NavLink
} from "react-router-dom";
import type { ReactTestRenderer } from "react-test-renderer";

describe("NavLink", () => {
describe("when it does not match", () => {
Expand All @@ -20,7 +21,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -51,7 +52,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -83,7 +84,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -111,7 +112,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home"]}>
Expand Down Expand Up @@ -146,7 +147,7 @@ describe("NavLink", () => {
return <div>Child</div>;
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home/child"]}>
Expand Down Expand Up @@ -180,7 +181,7 @@ describe("NavLink", () => {
return <div>Child</div>;
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home/child"]}>
Expand Down Expand Up @@ -218,7 +219,7 @@ describe("NavLink", () => {
return <div>Child</div>;
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home/child"]}>
Expand Down Expand Up @@ -256,7 +257,7 @@ describe("NavLink", () => {
return <div>Child</div>;
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/home/child"]}>
Expand Down Expand Up @@ -292,7 +293,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/Home"]}>
Expand Down Expand Up @@ -320,7 +321,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/Home"]}>
Expand Down Expand Up @@ -352,7 +353,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/Home"]}>
Expand Down Expand Up @@ -384,7 +385,7 @@ describe("NavLink", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/Home"]}>
Expand Down Expand Up @@ -419,7 +420,7 @@ describe("NavLink under a Routes with a basename", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/app/home"]}>
Expand Down Expand Up @@ -450,7 +451,7 @@ describe("NavLink under a Routes with a basename", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/app/home"]}>
Expand Down Expand Up @@ -482,7 +483,7 @@ describe("NavLink under a Routes with a basename", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/app/home"]}>
Expand Down Expand Up @@ -510,7 +511,7 @@ describe("NavLink under a Routes with a basename", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/app/home"]}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "react-router-dom";

describe("navigate with params", () => {
let node;
let node: HTMLDivElement;
beforeEach(() => {
node = document.createElement("div");
document.body.appendChild(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import {

describe("useSearchParams", () => {
function SearchPage() {
let queryRef = React.useRef();
let queryRef = React.useRef<HTMLInputElement>();
let [searchParams, setSearchParams] = useSearchParams({ q: "" });
let query = searchParams.get("q");

function handleSubmit(event) {
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setSearchParams({ q: queryRef.current.value });
}
Expand All @@ -30,7 +30,7 @@ describe("useSearchParams", () => {
);
}

let node;
let node: HTMLDivElement;
beforeEach(() => {
node = document.createElement("div");
document.body.appendChild(node);
Expand All @@ -56,7 +56,7 @@ describe("useSearchParams", () => {
let form = node.querySelector("form");
expect(form).toBeDefined();

let queryInput = node.querySelector("input[name=q]");
let queryInput = node.querySelector<HTMLInputElement>("input[name=q]");
expect(queryInput).toBeDefined();

expect(node.innerHTML).toMatch(/The current query is "Michael Jackson"/);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { StaticRouter as Router } from "react-router-dom/server";
describe("A <StaticRouter>", () => {
describe("with a string location prop", () => {
it("parses the location into an object", () => {
let location;
let location: ReturnType<typeof useLocation>;
function LocationChecker(props) {
location = useLocation();
return null;
Expand All @@ -32,7 +32,7 @@ describe("A <StaticRouter>", () => {

describe("with an object location prop", () => {
it("adds missing properties", () => {
let location;
let location: ReturnType<typeof useLocation>;
function LocationChecker(props) {
location = useLocation();
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Navigate, Routes, Route } from "react-router-dom";
import { StaticRouter as Router } from "react-router-dom/server";

describe("A <Navigate> in a <StaticRouter>", () => {
let consoleWarn;
let consoleWarn: jest.SpyInstance<void, any>;
beforeEach(() => {
consoleWarn = jest.spyOn(console, "warn").mockImplementation(() => {});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from "react";
import { act, create as createTestRenderer } from "react-test-renderer";
import { MemoryRouter as Router, Outlet, Routes, Route } from "react-router";
import type { ReactTestRenderer } from "react-test-renderer";

describe("Descendant <Routes> splat matching", () => {
describe("when the parent route path ends with /*", () => {
Expand Down Expand Up @@ -32,7 +33,7 @@ describe("Descendant <Routes> splat matching", () => {
);
}

let renderer;
let renderer: ReactTestRenderer;
act(() => {
renderer = createTestRenderer(
<Router initialEntries={["/courses/react/react-fundamentals"]}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { act, create as createTestRenderer } from "react-test-renderer";
import { MemoryRouter as Router, Outlet, Routes, Route } from "react-router";

describe("Descendant <Routes>", () => {
let consoleWarn;
let consoleWarn: jest.SpyInstance<void, any>;
beforeEach(() => {
consoleWarn = jest.spyOn(console, "warn").mockImplementation();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { act } from "react-dom/test-utils";
import { MemoryRouter as Router, Navigate, Routes, Route } from "react-router";

describe("navigate using an element", () => {
let node;
let node: HTMLDivElement;
beforeEach(() => {
node = document.createElement("div");
document.body.appendChild(node);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { matchRoutes } from "react-router";
import type { PartialRouteObject } from "react-router";

describe("path matching", () => {
function pickPaths(routes, pathname) {
function pickPaths(routes: PartialRouteObject[], pathname: string) {
let matches = matchRoutes(routes, { pathname });
return matches ? matches.map(match => match.route.path) : null;
}
Expand Down
13 changes: 10 additions & 3 deletions packages/react-router/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ export function generatePath(path: string, params: Params = {}): string {
* @see https://reactrouter.com/api/matchRoutes
*/
export function matchRoutes(
routes: RouteObject[],
routes: PartialRouteObject[],
location: string | PartialLocation,
basename = ""
): RouteMatch[] | null {
Expand Down Expand Up @@ -725,13 +725,20 @@ export interface RouteMatch {
}

function flattenRoutes(
routes: RouteObject[],
routes: PartialRouteObject[],
branches: RouteBranch[] = [],
parentPath = "",
parentRoutes: RouteObject[] = [],
parentIndexes: number[] = []
): RouteBranch[] {
routes.forEach((route, index) => {
(routes as RouteObject[]).forEach((route, index) => {
route = {
...route,
path: route.path || "/",
caseSensitive: !!route.caseSensitive,
element: route.element
};

let path = joinPaths([parentPath, route.path]);
let routes = parentRoutes.concat(route);
let indexes = parentIndexes.concat(index);
Expand Down

0 comments on commit f8a9020

Please sign in to comment.