-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This hook provides access to the routes params/query/meta if the route is active and returns undefined if it's not active
- Loading branch information
Showing
6 changed files
with
109 additions
and
1 deletion.
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
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,59 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { createMemoryHistory } from "history"; | ||
import React from "react"; | ||
import { z } from "zod"; | ||
|
||
import { buildCreateRoute } from "./createRoute"; | ||
import { RoutingContext } from "./providers"; | ||
import { useRouteArgsIfActive } from "./useRouteArgsIfActive"; | ||
|
||
const createRoute = buildCreateRoute(() => createMemoryHistory<any>(), "/"); | ||
const fooRoute = createRoute.simpleRoute()({ | ||
event: "foo", | ||
url: "/:foo", | ||
paramsSchema: z.object({ foo: z.string() }), | ||
querySchema: z.object({ bar: z.string() }), | ||
}); | ||
describe("useRouteArgsIfActive", () => { | ||
it("returns undefined if the route is not active", () => { | ||
const { result } = renderHook(() => useRouteArgsIfActive(fooRoute), { | ||
wrapper: ({ children }) => ( | ||
<RoutingContext.Provider value={{ activeRouteEvents: { current: [] } }}> | ||
{children} | ||
</RoutingContext.Provider> | ||
), | ||
}); | ||
|
||
expect(result.current).toBe(undefined); | ||
}); | ||
|
||
it("returns the routes arguments if the route is active", () => { | ||
const { result } = renderHook(() => useRouteArgsIfActive(fooRoute), { | ||
wrapper: ({ children }) => ( | ||
<RoutingContext.Provider | ||
value={{ | ||
activeRouteEvents: { | ||
current: [ | ||
{ | ||
type: "foo", | ||
meta: {}, | ||
originalUrl: "", | ||
params: { foo: "bar" }, | ||
query: { bar: "baz" }, | ||
}, | ||
], | ||
}, | ||
}} | ||
> | ||
{children} | ||
</RoutingContext.Provider> | ||
), | ||
}); | ||
|
||
expect(result.current).toEqual({ | ||
params: { foo: "bar" }, | ||
query: { bar: "baz" }, | ||
meta: {}, | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { assertIsDefined } from "../utils"; | ||
|
||
import { AnyRoute, ArgumentsForRoute } from "./createRoute"; | ||
import { useActiveRouteEvents } from "./providers"; | ||
import { useIsRouteActive } from "./useIsRouteActive"; | ||
|
||
/** | ||
* @public | ||
* Returns the arguments for the given route if the route is active. | ||
* Returns undefined if the route is not active. | ||
* | ||
* @param route - the route to get the arguments for | ||
* @returns the arguments for the given route if the route is active, undefined otherwise | ||
* @throws if used outside of an xstate-tree root | ||
*/ | ||
export function useRouteArgsIfActive<TRoute extends AnyRoute>( | ||
route: TRoute | ||
): ArgumentsForRoute<TRoute> | undefined { | ||
const isActive = useIsRouteActive(route); | ||
const activeRoutes = useActiveRouteEvents(); | ||
|
||
if (!isActive) { | ||
return undefined; | ||
} | ||
|
||
const activeRoute = activeRoutes?.find( | ||
(activeRoute) => activeRoute.type === route.event | ||
); | ||
assertIsDefined( | ||
activeRoute, | ||
"active route is not defined, but the route is active??" | ||
); | ||
|
||
return { | ||
params: activeRoute.params, | ||
query: activeRoute.query, | ||
meta: activeRoute.meta, | ||
} as ArgumentsForRoute<TRoute>; | ||
} |
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