Skip to content

Commit

Permalink
Add additional test case for #10983
Browse files Browse the repository at this point in the history
  • Loading branch information
brophdawg11 committed Nov 20, 2023
1 parent a48c43c commit f320378
Showing 1 changed file with 104 additions and 1 deletion.
105 changes: 104 additions & 1 deletion packages/react-router/__tests__/useResolvedPath-test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import * as React from "react";
import * as TestRenderer from "react-test-renderer";
import type { Path } from "react-router";
import { MemoryRouter, Routes, Route, useResolvedPath } from "react-router";
import {
MemoryRouter,
Routes,
Route,
RouterProvider,
createMemoryRouter,
useResolvedPath,
Outlet,
useParams,
} from "react-router";

function ShowResolvedPath({ path }: { path: string | Path }) {
return <pre>{JSON.stringify(useResolvedPath(path))}</pre>;
Expand Down Expand Up @@ -313,4 +322,98 @@ describe("useResolvedPath", () => {
`);
});
});

// Follow up test to https://github.com/remix-run/react-router/pull/10983 to
// ensure we do this consistently across route types
it("resolves relative paths consistently across route types", async () => {
let router = createMemoryRouter([
{
path: "/",
Component: Outlet,
children: [
{
path: "static/foo",
Component: () => <p>Static:{useResolvedPath("foo").pathname}</p>,
},
{
path: "static/foo/foo",
Component: () => (
<p>Static:{useResolvedPath("foo/foo").pathname}</p>
),
},
{
path: "dynamic/:param",
Component: () => <p>Dynamic:{useResolvedPath("foo").pathname}</p>,
},
{
path: "dynamic/:param1/:param2",
Component: () => (
<p>Dynamic:{useResolvedPath("foo/foo").pathname}</p>
),
},
{
path: "splat/*",
Component: () => (
<p>Splat:{useResolvedPath(useParams()["*"]).pathname}</p>
),
},
],
},
]);

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(<RouterProvider router={router} />);
});
// @ts-expect-error
if (!renderer) throw new Error("renderer not defined");

await TestRenderer.act(() => router.navigate("/static/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Static:
/static/foo/foo
</p>
`);

await TestRenderer.act(() => router.navigate("/dynamic/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Dynamic:
/dynamic/foo/foo
</p>
`);

await TestRenderer.act(() => router.navigate("/splat/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Splat:
/splat/foo/foo
</p>
`);

await TestRenderer.act(() => router.navigate("/static/foo/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Static:
/static/foo/foo/foo/foo
</p>
`);

await TestRenderer.act(() => router.navigate("/dynamic/foo/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Dynamic:
/dynamic/foo/foo/foo/foo
</p>
`);

await TestRenderer.act(() => router.navigate("/splat/foo/foo"));
expect(renderer.toJSON()).toMatchInlineSnapshot(`
<p>
Splat:
/splat/foo/foo/foo/foo
</p>
`);
});
});

0 comments on commit f320378

Please sign in to comment.