Skip to content

Commit

Permalink
Fix unit tests using the dummy router
Browse files Browse the repository at this point in the history
  • Loading branch information
sandhose committed Jan 6, 2025
1 parent 6f67d27 commit 1d0b0ab
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions frontend/src/test-utils/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,40 @@ import {
createRootRoute,
createRoute,
createRouter,
matchContext,
useRouterState,
} from "@tanstack/react-router";

const rootRoute = createRootRoute();
const index = createRoute({ getParentRoute: () => rootRoute, path: "/" });
const index = createRoute({
getParentRoute: () => rootRoute,
path: "/",
component: () => null,
});

const router = createRouter({
history: createMemoryHistory(),
routeTree: rootRoute.addChildren([index]),
});
router.load();

const InnerProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
const matchId = useRouterState({
select: (s) => {
return s.matches[0]?.id;
},
});

return (
<matchContext.Provider value={matchId}>{children}</matchContext.Provider>
);
};

export const DummyRouter: React.FC<React.PropsWithChildren> = ({
children,
}) => (
/** @ts-expect-error: The router we pass doesn't match the "real" router, which is fine for tests */
<RouterContextProvider router={router}>{children}</RouterContextProvider>
<RouterContextProvider router={router}>
<InnerProvider>{children}</InnerProvider>
</RouterContextProvider>
);

0 comments on commit 1d0b0ab

Please sign in to comment.