Skip to content

Commit

Permalink
Fix failing tests, and new test cases to expose regression issues wit…
Browse files Browse the repository at this point in the history
…h useNavigate
  • Loading branch information
42shadow42 committed Jun 3, 2022
1 parent 258b614 commit d644b50
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/react-router-dom-v5-compat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export type {
Pathname,
Search,
RoutesProps,
} from "./react-router-dom";
} from "../react-router-dom";
export {
BrowserRouter,
HashRouter,
Expand Down Expand Up @@ -108,7 +108,7 @@ export {
useResolvedPath,
useRoutes,
useSearchParams,
} from "./react-router-dom";
} from "../react-router-dom";

export type { StaticRouterProps } from "./lib/components";
export { CompatRouter, CompatRoute, StaticRouter } from "./lib/components";
2 changes: 1 addition & 1 deletion packages/react-router-dom-v5-compat/lib/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { useHistory, Route as RouteV5 } from "react-router-dom";
// We are a wrapper around react-router-dom v6, so bring it in
// and bundle it because an app can't have two versions of
// react-router-dom in its package.json.
import { Router, Routes, Route } from "../react-router-dom";
import { Router, Routes, Route } from "../../react-router-dom";

// v5 isn't in TypeScript, they'll also lose the @types/react-router with this
// but not worried about that for now.
Expand Down
73 changes: 73 additions & 0 deletions packages/react-router/__tests__/useNavigate-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,77 @@ describe("useNavigate", () => {
`);
});

it("transitions to the new location when called immediately", () => {
const Home = React.forwardRef(function Home(_props, ref) {
let navigate = useNavigate();

React.useImperativeHandle(ref, () => ({
navigate: () => navigate("/about")
}))

return null
})

let homeRef;

let renderer: TestRenderer.ReactTestRenderer;
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route path="home" element={<Home ref={(ref) => homeRef = ref} />} />
<Route path="about" element={<h1>About</h1>} />
</Routes>
</MemoryRouter>
);

TestRenderer.act(() => {
homeRef.navigate();
})

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<h1>
About
</h1>
`);
});

it("allows navigation in child useEffects", () => {
function Child({ onChildRendered }) {

React.useEffect(() => {
onChildRendered();
});

return null;
}

function Parent() {
let navigate = useNavigate();

let onChildRendered = React.useCallback(() => navigate("/about"), []);

return <Child onChildRendered={onChildRendered} />;
}

let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route path="home" element={<Parent />} />
<Route path="about" element={<h1>About</h1>} />
</Routes>
</MemoryRouter>
);
});

expect(renderer.toJSON()).toMatchInlineSnapshot(`
<h1>
About
</h1>
`);
});

describe("with state", () => {
it("adds the state to location.state", () => {
function Home() {
Expand Down Expand Up @@ -97,4 +168,6 @@ describe("useNavigate", () => {
`);
});
});


});

0 comments on commit d644b50

Please sign in to comment.