|
| 1 | +import { render } from "solid-js/web"; |
| 2 | +import { vi } from "vitest"; |
| 3 | +import { Router, Route, useNavigate } from "../src/index.js"; |
| 4 | +import type { Navigator } from "../src/index.js"; |
| 5 | + |
| 6 | +// jsdom implements history traversal but not scrolling — stub the primitives |
| 7 | +// so the restoration path (capture on scroll, restore via scrollTo) is |
| 8 | +// observable. |
| 9 | +function stubScrolling() { |
| 10 | + let y = 0; |
| 11 | + Object.defineProperty(window, "scrollY", { configurable: true, get: () => y }); |
| 12 | + const scrollTo = vi.fn((_x: number, newY: number) => { |
| 13 | + y = newY; |
| 14 | + window.dispatchEvent(new Event("scroll")); |
| 15 | + }); |
| 16 | + window.scrollTo = scrollTo as any; |
| 17 | + return { |
| 18 | + scrollTo, |
| 19 | + scrollUserTo(newY: number) { |
| 20 | + y = newY; |
| 21 | + window.dispatchEvent(new Event("scroll")); |
| 22 | + } |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +describe("scroll restoration (#577)", () => { |
| 27 | + beforeEach(() => { |
| 28 | + window.history.replaceState(null, "", "/"); |
| 29 | + sessionStorage.clear(); |
| 30 | + }); |
| 31 | + |
| 32 | + test("is off by default", () => { |
| 33 | + window.history.scrollRestoration = "auto"; |
| 34 | + const dispose = render( |
| 35 | + () => ( |
| 36 | + <Router> |
| 37 | + <Route path="/" component={() => null} /> |
| 38 | + </Router> |
| 39 | + ), |
| 40 | + document.body |
| 41 | + ); |
| 42 | + try { |
| 43 | + expect(window.history.scrollRestoration).toBe("auto"); |
| 44 | + } finally { |
| 45 | + document.body.innerHTML = ""; |
| 46 | + dispose(); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + test("restores the saved position on back navigation", async () => { |
| 51 | + const scrolling = stubScrolling(); |
| 52 | + let navigate!: Navigator; |
| 53 | + |
| 54 | + const Long = () => { |
| 55 | + navigate = useNavigate(); |
| 56 | + return <div data-testid="long">long</div>; |
| 57 | + }; |
| 58 | + |
| 59 | + const dispose = render( |
| 60 | + () => ( |
| 61 | + <Router scrollRestoration> |
| 62 | + <Route path="/" component={Long} /> |
| 63 | + <Route path="/short" component={() => <div data-testid="short">short</div>} /> |
| 64 | + </Router> |
| 65 | + ), |
| 66 | + document.body |
| 67 | + ); |
| 68 | + |
| 69 | + try { |
| 70 | + expect(window.history.scrollRestoration).toBe("manual"); |
| 71 | + |
| 72 | + // user scrolls down the long page, then navigates away |
| 73 | + scrolling.scrollUserTo(3000); |
| 74 | + navigate("/short"); |
| 75 | + await vi.waitFor(() => expect(document.querySelector("[data-testid=short]")).toBeTruthy()); |
| 76 | + // push navigations still scroll to the top |
| 77 | + expect(scrolling.scrollTo).toHaveBeenLastCalledWith(0, 0); |
| 78 | + |
| 79 | + window.history.back(); |
| 80 | + await vi.waitFor(() => expect(document.querySelector("[data-testid=long]")).toBeTruthy()); |
| 81 | + await vi.waitFor(() => expect(scrolling.scrollTo).toHaveBeenLastCalledWith(0, 3000)); |
| 82 | + } finally { |
| 83 | + document.body.innerHTML = ""; |
| 84 | + dispose(); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + test("a push prunes saved positions for truncated forward entries", async () => { |
| 89 | + const scrolling = stubScrolling(); |
| 90 | + let navigate!: Navigator; |
| 91 | + |
| 92 | + const Page = () => { |
| 93 | + navigate = useNavigate(); |
| 94 | + return <div data-testid="page">page</div>; |
| 95 | + }; |
| 96 | + |
| 97 | + const dispose = render( |
| 98 | + () => ( |
| 99 | + <Router scrollRestoration> |
| 100 | + <Route path="/" component={Page} /> |
| 101 | + <Route path="/a" component={Page} /> |
| 102 | + <Route path="/b" component={Page} /> |
| 103 | + </Router> |
| 104 | + ), |
| 105 | + document.body |
| 106 | + ); |
| 107 | + |
| 108 | + try { |
| 109 | + navigate("/a"); |
| 110 | + await vi.waitFor(() => expect(window.location.pathname).toBe("/a")); |
| 111 | + scrolling.scrollUserTo(500); |
| 112 | + |
| 113 | + window.history.back(); |
| 114 | + await vi.waitFor(() => expect(window.location.pathname).toBe("/")); |
| 115 | + |
| 116 | + // pushing /b truncates the forward entry (/a at the same depth) — its |
| 117 | + // saved position must not leak into the fresh /b entry |
| 118 | + navigate("/b"); |
| 119 | + await vi.waitFor(() => expect(window.location.pathname).toBe("/b")); |
| 120 | + scrolling.scrollTo.mockClear(); |
| 121 | + |
| 122 | + window.history.back(); |
| 123 | + await vi.waitFor(() => expect(window.location.pathname).toBe("/")); |
| 124 | + window.history.forward(); |
| 125 | + await vi.waitFor(() => expect(window.location.pathname).toBe("/b")); |
| 126 | + await Promise.resolve(); |
| 127 | + expect(scrolling.scrollTo).not.toHaveBeenCalledWith(0, 500); |
| 128 | + } finally { |
| 129 | + document.body.innerHTML = ""; |
| 130 | + dispose(); |
| 131 | + } |
| 132 | + }); |
| 133 | +}); |
0 commit comments