Skip to content

Commit bc78105

Browse files
committed
Fix unit tests using the dummy router
1 parent 84a9a1a commit bc78105

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

frontend/src/test-utils/router.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,40 @@ import {
1010
createRootRoute,
1111
createRoute,
1212
createRouter,
13+
matchContext,
14+
useRouterState,
1315
} from "@tanstack/react-router";
1416

1517
const rootRoute = createRootRoute();
16-
const index = createRoute({ getParentRoute: () => rootRoute, path: "/" });
18+
const index = createRoute({
19+
getParentRoute: () => rootRoute,
20+
path: "/",
21+
component: () => null,
22+
});
1723

1824
const router = createRouter({
1925
history: createMemoryHistory(),
2026
routeTree: rootRoute.addChildren([index]),
2127
});
28+
router.load();
29+
30+
const InnerProvider: React.FC<React.PropsWithChildren> = ({ children }) => {
31+
const matchId = useRouterState({
32+
select: (s) => {
33+
return s.matches[0]?.id;
34+
},
35+
});
36+
37+
return (
38+
<matchContext.Provider value={matchId}>{children}</matchContext.Provider>
39+
);
40+
};
2241

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

0 commit comments

Comments
 (0)