Skip to content

Commit

Permalink
Incorporate PR feedback, run yarn format
Browse files Browse the repository at this point in the history
  • Loading branch information
david-bezero committed Sep 13, 2023
1 parent b4ad330 commit 80c2f60
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
37 changes: 28 additions & 9 deletions packages/react-router-dom/__tests__/scroll-restoration-test.tsx
Expand Up @@ -14,7 +14,9 @@ import getHtml from "../../react-router/__tests__/utils/getHtml";

describe(`ScrollRestoration`, () => {
it("restores the scroll position for a page when re-visited", () => {
const consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {});
const consoleWarnMock = jest
.spyOn(console, "warn")
.mockImplementation(() => {});

let testWindow = getWindowImpl("/base");
const mockScroll = jest.fn();
Expand All @@ -28,7 +30,9 @@ describe(`ScrollRestoration`, () => {
return (
<>
<Outlet />
<ScrollRestoration getKey={(location) => "test1-" + location.pathname} />
<ScrollRestoration
getKey={(location) => "test1-" + location.pathname}
/>
</>
);
},
Expand Down Expand Up @@ -119,15 +123,17 @@ describe(`ScrollRestoration`, () => {
});

it("fails gracefully if sessionStorage is not available", () => {
const consoleWarnMock = jest.spyOn(console, "warn").mockImplementation(() => {});
const consoleWarnMock = jest
.spyOn(console, "warn")
.mockImplementation(() => {});

let testWindow = getWindowImpl("/base");
const mockScroll = jest.fn();
window.scrollTo = mockScroll;

jest.spyOn(window, "sessionStorage", "get").mockImplementation(() => {
throw new Error("denied");
})
});

let router = createBrowserRouter(
[
Expand All @@ -137,7 +143,9 @@ describe(`ScrollRestoration`, () => {
return (
<>
<Outlet />
<ScrollRestoration getKey={(location) => "test2-" + location.pathname} />
<ScrollRestoration
getKey={(location) => "test2-" + location.pathname}
/>
</>
);
},
Expand Down Expand Up @@ -172,8 +180,9 @@ describe(`ScrollRestoration`, () => {
]);

expect(consoleWarnMock).toHaveBeenCalledWith(
expect.stringContaining("Scroll restoration will not work"),
expect.anything(),
expect.stringContaining(
"Failed to save scroll positions in sessionStorage"
)
);

consoleWarnMock.mockRestore();
Expand All @@ -184,13 +193,23 @@ const testPages = [
{
index: true,
Component() {
return <p>On page 1<br /><Link to="/page">Go to page 2</Link></p>;
return (
<p>
On page 1<br />
<Link to="/page">Go to page 2</Link>
</p>
);
},
},
{
path: "page",
Component() {
return <p>On page 2<br /><Link to="/">Go to page 1</Link></p>;
return (
<p>
On page 2<br />
<Link to="/">Go to page 1</Link>
</p>
);
},
},
];
Expand Down
8 changes: 6 additions & 2 deletions packages/react-router-dom/index.tsx
Expand Up @@ -1325,10 +1325,14 @@ function useScrollRestoration({
storageKey || SCROLL_RESTORATION_STORAGE_KEY,
JSON.stringify(savedScrollPositions)
);
window.history.scrollRestoration = "auto";
} catch (error) {
console.warn("Failed to record scroll position in session storage. Scroll restoration will not work.", error);
warning(
false,
"Failed to save scroll positions in sessionStorage, <ScrollRestoration /> will not work properly."
);
console.warn(error);
}
window.history.scrollRestoration = "auto";
}, [storageKey, getKey, navigation.state, location, matches])
);

Expand Down

0 comments on commit 80c2f60

Please sign in to comment.