-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** @jest-environment jsdom */ | ||
import React from 'react' | ||
|
||
import { render } from '@testing-library/react' | ||
|
||
import { Route, Router } from '../router' | ||
import { Set } from '../Set' | ||
import { useRoutePaths, useRoutePath } from '../useRoutePaths' | ||
|
||
test('useRoutePaths and useRoutePath', async () => { | ||
const HomePage = () => { | ||
const routePaths = useRoutePaths() | ||
// Sorry about the `as never` stuff here. In an actual project we have | ||
// generated types to use, but not here | ||
const homePath = useRoutePath('home' as never) | ||
|
||
return ( | ||
<> | ||
<h1>Home Page</h1> | ||
<p>My path is {homePath}</p> | ||
<p>All paths: {Object.values(routePaths).join(',')}</p> | ||
</> | ||
) | ||
} | ||
|
||
interface LayoutProps { | ||
children: React.ReactNode | ||
} | ||
|
||
const Layout = ({ children }: LayoutProps) => <>{children}</> | ||
|
||
const Page = () => <h1>Page</h1> | ||
|
||
const TestRouter = () => ( | ||
<Router> | ||
<Route path="/" page={HomePage} name="home" /> | ||
<Set wrap={Layout}> | ||
<Route path="/one" page={Page} name="one" /> | ||
</Set> | ||
<Set wrap={Layout}> | ||
<Route path="/two/{id:Int}" page={Page} name="two" /> | ||
</Set> | ||
</Router> | ||
) | ||
|
||
const screen = render(<TestRouter />) | ||
|
||
await screen.findByText('Home Page') | ||
await screen.findByText(/^My path is\s+\/$/) | ||
await screen.findByText(/^All paths:\s+\/,\/one,\/two\/\{id:Int\}$/) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useRouterState } from './router-context' | ||
import type { GeneratedRoutesMap } from './util' | ||
|
||
import type { AvailableRoutes } from '.' | ||
|
||
// This has to be a function, otherwise we're not able to do declaration merging | ||
export function useRoutePaths() { | ||
const routerState = useRouterState() | ||
|
||
const routePaths = Object.values(routerState.routes.pathRouteMap).reduce< | ||
Record<keyof GeneratedRoutesMap, string> | ||
>((routePathsAcc, currRoute) => { | ||
if (currRoute.name) { | ||
routePathsAcc[currRoute.name] = currRoute.path | ||
} | ||
|
||
return routePathsAcc | ||
}, {}) | ||
|
||
return routePaths | ||
} | ||
|
||
export function useRoutePath(routeName: keyof AvailableRoutes) { | ||
const routePaths = useRoutePaths() | ||
|
||
return routePaths[routeName] | ||
} |