From f8a9020bc4490059e308378326cefac37188f761 Mon Sep 17 00:00:00 2001 From: Chance Strickland Date: Thu, 15 Jul 2021 16:06:30 -0700 Subject: [PATCH] Refactor tests to TS --- .../{exports-test.js => exports-test.tsx} | 0 ...link-click-test.js => link-click-test.tsx} | 4 +-- .../{link-href-test.js => link-href-test.tsx} | 9 ++--- .../{link-push-test.js => link-push-test.tsx} | 17 ++++++---- ...ctive-test.js => nav-link-active-test.tsx} | 33 ++++++++++--------- ...est.js => navigate-encode-params-test.tsx} | 2 +- ...-params-test.js => search-params-test.tsx} | 8 ++--- ...atic-link-test.js => static-link-test.tsx} | 0 ...ation-test.js => static-location-test.tsx} | 4 +-- ...igate-test.js => static-navigate-test.tsx} | 2 +- ...ndant-routes-splat-matching-test.tsx.snap} | 0 ...descendant-routes-splat-matching-test.tsx} | 3 +- ....js => descendant-routes-warning-test.tsx} | 2 +- ...ratePath-test.js => generatePath-test.tsx} | 0 ...ment-test.js => navigate-element-test.tsx} | 2 +- ...atching-test.js => path-matching-test.tsx} | 3 +- packages/react-router/index.tsx | 13 ++++++-- 17 files changed, 59 insertions(+), 43 deletions(-) rename packages/react-router-dom/__tests__/{exports-test.js => exports-test.tsx} (100%) rename packages/react-router-dom/__tests__/{link-click-test.js => link-click-test.tsx} (98%) rename packages/react-router-dom/__tests__/{link-href-test.js => link-href-test.tsx} (92%) rename packages/react-router-dom/__tests__/{link-push-test.js => link-push-test.tsx} (94%) rename packages/react-router-dom/__tests__/{nav-link-active-test.js => nav-link-active-test.tsx} (94%) rename packages/react-router-dom/__tests__/{navigate-encode-params-test.js => navigate-encode-params-test.tsx} (98%) rename packages/react-router-dom/__tests__/{search-params-test.js => search-params-test.tsx} (87%) rename packages/react-router-dom/__tests__/{static-link-test.js => static-link-test.tsx} (100%) rename packages/react-router-dom/__tests__/{static-location-test.js => static-location-test.tsx} (93%) rename packages/react-router-dom/__tests__/{static-navigate-test.js => static-navigate-test.tsx} (94%) rename packages/react-router/__tests__/__snapshots__/{descendant-routes-splat-matching-test.js.snap => descendant-routes-splat-matching-test.tsx.snap} (100%) rename packages/react-router/__tests__/{descendant-routes-splat-matching-test.js => descendant-routes-splat-matching-test.tsx} (92%) rename packages/react-router/__tests__/{descendant-routes-warning-test.js => descendant-routes-warning-test.tsx} (98%) rename packages/react-router/__tests__/{generatePath-test.js => generatePath-test.tsx} (100%) rename packages/react-router/__tests__/{navigate-element-test.js => navigate-element-test.tsx} (98%) rename packages/react-router/__tests__/{path-matching-test.js => path-matching-test.tsx} (98%) diff --git a/packages/react-router-dom/__tests__/exports-test.js b/packages/react-router-dom/__tests__/exports-test.tsx similarity index 100% rename from packages/react-router-dom/__tests__/exports-test.js rename to packages/react-router-dom/__tests__/exports-test.tsx diff --git a/packages/react-router-dom/__tests__/link-click-test.js b/packages/react-router-dom/__tests__/link-click-test.tsx similarity index 98% rename from packages/react-router-dom/__tests__/link-click-test.js rename to packages/react-router-dom/__tests__/link-click-test.tsx index 44d7ac780c..411b89913c 100644 --- a/packages/react-router-dom/__tests__/link-click-test.js +++ b/packages/react-router-dom/__tests__/link-click-test.tsx @@ -4,7 +4,7 @@ import { act } from "react-dom/test-utils"; import { MemoryRouter as Router, Routes, Route, Link } from "react-router-dom"; describe("A click", () => { - let node; + let node: HTMLDivElement; beforeEach(() => { node = document.createElement("div"); document.body.appendChild(node); @@ -62,7 +62,7 @@ describe("A 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) { event.preventDefault(); } diff --git a/packages/react-router-dom/__tests__/link-href-test.js b/packages/react-router-dom/__tests__/link-href-test.tsx similarity index 92% rename from packages/react-router-dom/__tests__/link-href-test.js rename to packages/react-router-dom/__tests__/link-href-test.tsx index 21b856e240..a2a6ce98cd 100644 --- a/packages/react-router-dom/__tests__/link-href-test.js +++ b/packages/react-router-dom/__tests__/link-href-test.tsx @@ -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", () => { @@ -14,7 +15,7 @@ describe("Link href", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -43,7 +44,7 @@ describe("Link href", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -72,7 +73,7 @@ describe("Link href", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -101,7 +102,7 @@ describe("Link href", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( diff --git a/packages/react-router-dom/__tests__/link-push-test.js b/packages/react-router-dom/__tests__/link-push-test.tsx similarity index 94% rename from packages/react-router-dom/__tests__/link-push-test.js rename to packages/react-router-dom/__tests__/link-push-test.tsx index 58df8d6d31..2ab162a196 100644 --- a/packages/react-router-dom/__tests__/link-push-test.js +++ b/packages/react-router-dom/__tests__/link-push-test.tsx @@ -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 = { pathname, search, hash }; return { action: "POP", location, @@ -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); diff --git a/packages/react-router-dom/__tests__/nav-link-active-test.js b/packages/react-router-dom/__tests__/nav-link-active-test.tsx similarity index 94% rename from packages/react-router-dom/__tests__/nav-link-active-test.js rename to packages/react-router-dom/__tests__/nav-link-active-test.tsx index 5c630b90d4..d392c894fa 100644 --- a/packages/react-router-dom/__tests__/nav-link-active-test.js +++ b/packages/react-router-dom/__tests__/nav-link-active-test.tsx @@ -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", () => { @@ -20,7 +21,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -51,7 +52,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -83,7 +84,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -111,7 +112,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -146,7 +147,7 @@ describe("NavLink", () => { return
Child
; } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -180,7 +181,7 @@ describe("NavLink", () => { return
Child
; } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -218,7 +219,7 @@ describe("NavLink", () => { return
Child
; } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -256,7 +257,7 @@ describe("NavLink", () => { return
Child
; } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -292,7 +293,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -320,7 +321,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -352,7 +353,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -384,7 +385,7 @@ describe("NavLink", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -419,7 +420,7 @@ describe("NavLink under a Routes with a basename", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -450,7 +451,7 @@ describe("NavLink under a Routes with a basename", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -482,7 +483,7 @@ describe("NavLink under a Routes with a basename", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( @@ -510,7 +511,7 @@ describe("NavLink under a Routes with a basename", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( diff --git a/packages/react-router-dom/__tests__/navigate-encode-params-test.js b/packages/react-router-dom/__tests__/navigate-encode-params-test.tsx similarity index 98% rename from packages/react-router-dom/__tests__/navigate-encode-params-test.js rename to packages/react-router-dom/__tests__/navigate-encode-params-test.tsx index b46bc5ed48..7545863e77 100644 --- a/packages/react-router-dom/__tests__/navigate-encode-params-test.js +++ b/packages/react-router-dom/__tests__/navigate-encode-params-test.tsx @@ -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); diff --git a/packages/react-router-dom/__tests__/search-params-test.js b/packages/react-router-dom/__tests__/search-params-test.tsx similarity index 87% rename from packages/react-router-dom/__tests__/search-params-test.js rename to packages/react-router-dom/__tests__/search-params-test.tsx index c4569c73b0..d96780720d 100644 --- a/packages/react-router-dom/__tests__/search-params-test.js +++ b/packages/react-router-dom/__tests__/search-params-test.tsx @@ -10,11 +10,11 @@ import { describe("useSearchParams", () => { function SearchPage() { - let queryRef = React.useRef(); + let queryRef = React.useRef(); let [searchParams, setSearchParams] = useSearchParams({ q: "" }); let query = searchParams.get("q"); - function handleSubmit(event) { + function handleSubmit(event: React.FormEvent) { event.preventDefault(); setSearchParams({ q: queryRef.current.value }); } @@ -30,7 +30,7 @@ describe("useSearchParams", () => { ); } - let node; + let node: HTMLDivElement; beforeEach(() => { node = document.createElement("div"); document.body.appendChild(node); @@ -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("input[name=q]"); expect(queryInput).toBeDefined(); expect(node.innerHTML).toMatch(/The current query is "Michael Jackson"/); diff --git a/packages/react-router-dom/__tests__/static-link-test.js b/packages/react-router-dom/__tests__/static-link-test.tsx similarity index 100% rename from packages/react-router-dom/__tests__/static-link-test.js rename to packages/react-router-dom/__tests__/static-link-test.tsx diff --git a/packages/react-router-dom/__tests__/static-location-test.js b/packages/react-router-dom/__tests__/static-location-test.tsx similarity index 93% rename from packages/react-router-dom/__tests__/static-location-test.js rename to packages/react-router-dom/__tests__/static-location-test.tsx index 2cdb1bbfc6..0bf43456b5 100644 --- a/packages/react-router-dom/__tests__/static-location-test.js +++ b/packages/react-router-dom/__tests__/static-location-test.tsx @@ -6,7 +6,7 @@ import { StaticRouter as Router } from "react-router-dom/server"; describe("A ", () => { describe("with a string location prop", () => { it("parses the location into an object", () => { - let location; + let location: ReturnType; function LocationChecker(props) { location = useLocation(); return null; @@ -32,7 +32,7 @@ describe("A ", () => { describe("with an object location prop", () => { it("adds missing properties", () => { - let location; + let location: ReturnType; function LocationChecker(props) { location = useLocation(); return null; diff --git a/packages/react-router-dom/__tests__/static-navigate-test.js b/packages/react-router-dom/__tests__/static-navigate-test.tsx similarity index 94% rename from packages/react-router-dom/__tests__/static-navigate-test.js rename to packages/react-router-dom/__tests__/static-navigate-test.tsx index 2f199bfc2c..850c8238fc 100644 --- a/packages/react-router-dom/__tests__/static-navigate-test.js +++ b/packages/react-router-dom/__tests__/static-navigate-test.tsx @@ -4,7 +4,7 @@ import { Navigate, Routes, Route } from "react-router-dom"; import { StaticRouter as Router } from "react-router-dom/server"; describe("A in a ", () => { - let consoleWarn; + let consoleWarn: jest.SpyInstance; beforeEach(() => { consoleWarn = jest.spyOn(console, "warn").mockImplementation(() => {}); }); diff --git a/packages/react-router/__tests__/__snapshots__/descendant-routes-splat-matching-test.js.snap b/packages/react-router/__tests__/__snapshots__/descendant-routes-splat-matching-test.tsx.snap similarity index 100% rename from packages/react-router/__tests__/__snapshots__/descendant-routes-splat-matching-test.js.snap rename to packages/react-router/__tests__/__snapshots__/descendant-routes-splat-matching-test.tsx.snap diff --git a/packages/react-router/__tests__/descendant-routes-splat-matching-test.js b/packages/react-router/__tests__/descendant-routes-splat-matching-test.tsx similarity index 92% rename from packages/react-router/__tests__/descendant-routes-splat-matching-test.js rename to packages/react-router/__tests__/descendant-routes-splat-matching-test.tsx index 7f9e994630..5108de709e 100644 --- a/packages/react-router/__tests__/descendant-routes-splat-matching-test.js +++ b/packages/react-router/__tests__/descendant-routes-splat-matching-test.tsx @@ -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 splat matching", () => { describe("when the parent route path ends with /*", () => { @@ -32,7 +33,7 @@ describe("Descendant splat matching", () => { ); } - let renderer; + let renderer: ReactTestRenderer; act(() => { renderer = createTestRenderer( diff --git a/packages/react-router/__tests__/descendant-routes-warning-test.js b/packages/react-router/__tests__/descendant-routes-warning-test.tsx similarity index 98% rename from packages/react-router/__tests__/descendant-routes-warning-test.js rename to packages/react-router/__tests__/descendant-routes-warning-test.tsx index 171845d467..02911ab7e0 100644 --- a/packages/react-router/__tests__/descendant-routes-warning-test.js +++ b/packages/react-router/__tests__/descendant-routes-warning-test.tsx @@ -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 ", () => { - let consoleWarn; + let consoleWarn: jest.SpyInstance; beforeEach(() => { consoleWarn = jest.spyOn(console, "warn").mockImplementation(); }); diff --git a/packages/react-router/__tests__/generatePath-test.js b/packages/react-router/__tests__/generatePath-test.tsx similarity index 100% rename from packages/react-router/__tests__/generatePath-test.js rename to packages/react-router/__tests__/generatePath-test.tsx diff --git a/packages/react-router/__tests__/navigate-element-test.js b/packages/react-router/__tests__/navigate-element-test.tsx similarity index 98% rename from packages/react-router/__tests__/navigate-element-test.js rename to packages/react-router/__tests__/navigate-element-test.tsx index 9a838e03d6..402c2b6dfd 100644 --- a/packages/react-router/__tests__/navigate-element-test.js +++ b/packages/react-router/__tests__/navigate-element-test.tsx @@ -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); diff --git a/packages/react-router/__tests__/path-matching-test.js b/packages/react-router/__tests__/path-matching-test.tsx similarity index 98% rename from packages/react-router/__tests__/path-matching-test.js rename to packages/react-router/__tests__/path-matching-test.tsx index e8374edc8c..b2048b325e 100644 --- a/packages/react-router/__tests__/path-matching-test.js +++ b/packages/react-router/__tests__/path-matching-test.tsx @@ -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; } diff --git a/packages/react-router/index.tsx b/packages/react-router/index.tsx index ce36c1f2b3..3141f07e9b 100644 --- a/packages/react-router/index.tsx +++ b/packages/react-router/index.tsx @@ -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 { @@ -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);