import React, { useMemo } from "react"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; import { createMemoryHistory } from "@nano-router/history"; import "@testing-library/jest-dom"; import { NestedRouter, Route, Router, Routes, useLink, useLocation, useParams, } from "./index.js"; const routes = new Routes( new Route("posts", "/posts"), new Route("comments", "/comments/:id"), ); const nestedRoutes = new Routes( new Route("posts/new", "/new"), new Route("posts/edit", "/edit/:id"), new Route("posts/show", "/:id"), ); type LocationProps = { name: string; }; const Location: React.FC = ({ name }) => { const location = useLocation(); return
{location.pathname}
; }; const PostViews = () => { const { id } = useParams(); const showNewPost = useLink("posts/new"); const showEditPost = useLink({ route: "posts/edit", params: { id } }); const showComments = useLink({ route: "comments", params: { id } }); return ( <> New post Edit post Show comments ); }; const NestedApp = () => { const history = useMemo( () => createMemoryHistory({ initialEntries: ["/42"] }), [], ); return (
); }; const App = () => { const history = useMemo( () => createMemoryHistory({ initialEntries: ["/posts"] }), [], ); return (
); }; describe("NestedRouter", () => { beforeEach(() => { render(); }); it("returns the router from the context", () => { expect(screen.getByTestId("location-app")).toHaveTextContent("/posts"); expect(screen.getByTestId("location-nested-app")).toHaveTextContent("/42"); }); describe("when navigating in the nested router", () => { describe("and the route exist", () => { beforeEach(async () => { await userEvent.click(screen.getByTestId("edit")); }); it("updates the routing information in the nested router", () => { expect(screen.getByTestId("location-app")).toHaveTextContent("/posts"); expect(screen.getByTestId("location-nested-app")).toHaveTextContent( "/edit/42", ); }); }); describe("and the route does't exist", () => { beforeEach(async () => { await userEvent.click(screen.getByTestId("comments")); }); it("forwards the navigation to the parent router", () => { expect(screen.getByTestId("location-app")).toHaveTextContent( "/comments/42", ); expect(screen.getByTestId("location-nested-app")).toHaveTextContent( "/42", ); }); }); }); });