|
1 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { Router } from "./router.js"; |
3 | 3 |
|
4 | 4 | describe("Router.deepUrl", () => { |
@@ -27,3 +27,56 @@ describe("Router.deepUrl", () => { |
27 | 27 | expect(navigate).toHaveBeenCalledWith("/de/graph"); |
28 | 28 | }); |
29 | 29 | }); |
| 30 | + |
| 31 | +describe("Router.customRoute language change", () => { |
| 32 | + const hashSetter = vi.fn<(v: string) => void>(); |
| 33 | + const reload = vi.fn<() => void>(); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + hashSetter.mockReset(); |
| 37 | + reload.mockReset(); |
| 38 | + vi.stubGlobal("location", { |
| 39 | + get hash() { |
| 40 | + return ""; |
| 41 | + }, |
| 42 | + set hash(v: string) { |
| 43 | + hashSetter(v); |
| 44 | + }, |
| 45 | + reload, |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + vi.unstubAllGlobals(); |
| 51 | + }); |
| 52 | + |
| 53 | + it("rebuilds the hash from currentState when navigate omits the # prefix", () => { |
| 54 | + const fakeRouter = { |
| 55 | + state: { lang: "de", view: "map" }, |
| 56 | + currentState: {}, |
| 57 | + language: { getLocale: (l: string) => l }, |
| 58 | + targets: [], |
| 59 | + init: true, |
| 60 | + objects: { nodes: { all: [], lost: [], new: [], offline: [], online: [] }, links: [], nodeDict: {} }, |
| 61 | + view: vi.fn(), |
| 62 | + gotoNode: vi.fn(), |
| 63 | + gotoLink: vi.fn(), |
| 64 | + resetView: vi.fn(), |
| 65 | + getParams: () => ({}), |
| 66 | + paramsToUrl: Router.prototype.paramsToUrl, |
| 67 | + generateLink: Router.prototype.generateLink, |
| 68 | + }; |
| 69 | + |
| 70 | + // Mimic what Navigo passes to the handler after deepUrl({lang: "en"}) |
| 71 | + // navigates to "/en/map" — note hashString is "" because there is no "#". |
| 72 | + const match = { |
| 73 | + data: ["en", "map", undefined, undefined, undefined, undefined, undefined], |
| 74 | + hashString: "", |
| 75 | + }; |
| 76 | + |
| 77 | + Router.prototype.customRoute.call(fakeRouter as never, match as never); |
| 78 | + |
| 79 | + expect(hashSetter).toHaveBeenCalledWith("/en/map"); |
| 80 | + expect(reload).toHaveBeenCalled(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments