-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #2
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { createMemoryHistory } from "history"; | ||
import React, { useEffect, useState } from "react"; | ||
import { useRoutes } from "../hooks/useRoutes"; | ||
import { Path } from "../shorthand"; | ||
import { renderInHistory, screen } from "../test-utils"; | ||
import { Redirect } from "./Redirect"; | ||
|
||
describe("Redirect", () => { | ||
it("changes path on rendering", async () => { | ||
const history = createMemoryHistory({ | ||
initialEntries: [ | ||
{ | ||
pathname: "/foo", | ||
state: null, | ||
}, | ||
], | ||
}); | ||
const Foo: React.FC = () => { | ||
const [redirect, setRedirect] = useState(false); | ||
useEffect(() => { | ||
setTimeout(() => setRedirect(true), 100); | ||
}, []); | ||
return redirect ? ( | ||
<Redirect route={routes._.bar} /> | ||
) : ( | ||
<div>Redirecting...</div> | ||
); | ||
}; | ||
const routes = Path() | ||
.route("foo", (foo) => | ||
foo.action(() => { | ||
return <Foo />; | ||
}) | ||
) | ||
.route("bar", (bar) => bar.action(() => <div>I AM BAR</div>)); | ||
const Component: React.FC = () => { | ||
const contents = useRoutes(routes); | ||
return contents; | ||
}; | ||
|
||
renderInHistory(history, <Component />); | ||
expect(await screen.findByText("I AM BAR")).toBeInTheDocument(); | ||
expect(history.index).toBe(0); | ||
expect(history.location).toMatchObject({ | ||
pathname: "/bar", | ||
state: null, | ||
}); | ||
}); | ||
}); |
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,22 @@ | ||
import { useLayoutEffect } from "react"; | ||
import { useNavigate } from "../hooks/useNavigate"; | ||
import { ReactRouteRecord } from "../types/ReactElement"; | ||
|
||
export type RedirectProps<Match> = { | ||
route: ReactRouteRecord<Match>; | ||
} & ({} extends Match ? { match?: Match } : { match: Match }); | ||
|
||
/** | ||
* Redirects to given route. | ||
*/ | ||
export const Redirect = <Match,>({ | ||
route, | ||
match, | ||
}: RedirectProps<Match>): null => { | ||
const navigate = useNavigate(); | ||
useLayoutEffect(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
(navigate.replace as any)(route, match); | ||
}); | ||
return null; | ||
}; |