|
| 1 | +import { render } from "solid-js/web"; |
| 2 | +import { vi } from "vitest"; |
| 3 | +import { HashRouter, Router, Route, useNavigate } from "../src/index.js"; |
| 4 | +import type { Navigator } from "../src/index.js"; |
| 5 | +import { notifyIfNotBlocked } from "../src/lifecycle.js"; |
| 6 | + |
| 7 | +// #694: browsers cap session history (50 entries in Chromium and Firefox). |
| 8 | +// Once full, a push drops the oldest entry and history.length stops growing, |
| 9 | +// so depths taken from history.length repeat. |
| 10 | +describe("history depth when history.length is capped (#694)", () => { |
| 11 | + afterEach(() => { |
| 12 | + vi.restoreAllMocks(); |
| 13 | + document.body.innerHTML = ""; |
| 14 | + }); |
| 15 | + |
| 16 | + const url = () => window.location.pathname + window.location.search + window.location.hash; |
| 17 | + |
| 18 | + async function pushThree(RouterComponent: typeof Router | typeof HashRouter, hash = false) { |
| 19 | + let navigate!: Navigator; |
| 20 | + const Page = () => { |
| 21 | + navigate = useNavigate(); |
| 22 | + return null; |
| 23 | + }; |
| 24 | + const dispose = render( |
| 25 | + () => ( |
| 26 | + <RouterComponent> |
| 27 | + <Route path="/:n?" component={Page} /> |
| 28 | + </RouterComponent> |
| 29 | + ), |
| 30 | + document.body |
| 31 | + ); |
| 32 | + try { |
| 33 | + const depths: number[] = []; |
| 34 | + for (let i = 1; i <= 3; i++) { |
| 35 | + navigate(`/${i}`); |
| 36 | + await vi.waitFor(() => expect(url()).toBe(hash ? `/#/${i}` : `/${i}`)); |
| 37 | + depths.push(window.history.state._depth); |
| 38 | + } |
| 39 | + navigate("/3?replaced", { replace: true }); |
| 40 | + await vi.waitFor(() => expect(url()).toBe(hash ? "/#/3?replaced" : "/3?replaced")); |
| 41 | + depths.push(window.history.state._depth); |
| 42 | + return depths; |
| 43 | + } finally { |
| 44 | + dispose(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + test("Router stamps increasing depths on push", async () => { |
| 49 | + window.history.replaceState({ _depth: 48 }, "", "/"); |
| 50 | + vi.spyOn(window.history, "length", "get").mockReturnValue(50); |
| 51 | + expect(await pushThree(Router)).toEqual([49, 50, 51, 51]); |
| 52 | + }); |
| 53 | + |
| 54 | + test("HashRouter stamps increasing depths on push", async () => { |
| 55 | + window.history.replaceState({ _depth: 48 }, "", "/"); |
| 56 | + vi.spyOn(window.history, "length", "get").mockReturnValue(50); |
| 57 | + expect(await pushThree(HashRouter, true)).toEqual([49, 50, 51, 51]); |
| 58 | + }); |
| 59 | + |
| 60 | + test("Router keeps user state when stamping the depth on push", async () => { |
| 61 | + window.history.replaceState({ _depth: 3 }, "", "/"); |
| 62 | + let navigate!: Navigator; |
| 63 | + const dispose = render( |
| 64 | + () => ( |
| 65 | + <Router> |
| 66 | + <Route |
| 67 | + path="/*" |
| 68 | + component={() => { |
| 69 | + navigate = useNavigate(); |
| 70 | + return null; |
| 71 | + }} |
| 72 | + /> |
| 73 | + </Router> |
| 74 | + ), |
| 75 | + document.body |
| 76 | + ); |
| 77 | + try { |
| 78 | + navigate("/next", { state: { from: "test" } }); |
| 79 | + await vi.waitFor(() => expect(url()).toBe("/next")); |
| 80 | + expect(window.history.state).toEqual({ from: "test", _depth: 4 }); |
| 81 | + } finally { |
| 82 | + dispose(); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + test("an entry pushed outside the router counts up from the last depth", () => { |
| 87 | + window.history.replaceState({ _depth: 49 }, "", "/"); |
| 88 | + vi.spyOn(window.history, "length", "get").mockReturnValue(50); |
| 89 | + const notify = vi.fn(); |
| 90 | + const handler = notifyIfNotBlocked(notify, () => false); |
| 91 | + handler(); // record the current depth |
| 92 | + notify.mockClear(); |
| 93 | + window.history.pushState(null, "", "/#anchor"); |
| 94 | + handler(); |
| 95 | + expect(window.history.state._depth).toBe(50); |
| 96 | + expect(notify).toHaveBeenCalledTimes(1); |
| 97 | + }); |
| 98 | +}); |
0 commit comments