|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { render, waitFor } from "@testing-library/react"; |
| 4 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +vi.mock("@mui/material/styles", async (importOriginal) => ({ |
| 7 | + ...(await importOriginal<typeof import("@mui/material/styles")>()), |
| 8 | + useColorScheme: () => ({ mode: "light", systemMode: "light" }), |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("@/lib/EnvProvider", () => ({ |
| 12 | + useEnv: () => ({ styleProvider: "openmapx" }), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("@/lib/map", () => ({ |
| 16 | + baseMapCreditsHtml: () => [], |
| 17 | + loadMaptilerStyle: vi.fn().mockResolvedValue({}), |
| 18 | + loadOpenMapXStyle: vi.fn().mockResolvedValue({}), |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("./MapCredits", () => ({ MapCredits: () => null })); |
| 22 | + |
| 23 | +vi.mock("maplibre-gl", () => { |
| 24 | + let workerUrl = ""; |
| 25 | + const workerUrlsAtConstruction: string[] = []; |
| 26 | + class FakeMap { |
| 27 | + flyTo = vi.fn(); |
| 28 | + remove = vi.fn(); |
| 29 | + |
| 30 | + constructor() { |
| 31 | + workerUrlsAtConstruction.push(workerUrl); |
| 32 | + } |
| 33 | + } |
| 34 | + class FakeMarker { |
| 35 | + addTo = vi.fn(() => this); |
| 36 | + setLngLat = vi.fn(() => this); |
| 37 | + } |
| 38 | + return { |
| 39 | + __test: { workerUrlsAtConstruction }, |
| 40 | + getVersion: () => "6.1.0", |
| 41 | + getWorkerUrl: () => workerUrl, |
| 42 | + Map: FakeMap, |
| 43 | + Marker: FakeMarker, |
| 44 | + setWorkerUrl: (url: string) => { |
| 45 | + workerUrl = url; |
| 46 | + }, |
| 47 | + }; |
| 48 | +}); |
| 49 | + |
| 50 | +import * as maplibregl from "maplibre-gl"; |
| 51 | +import { LocationMinimap } from "./LocationMinimap"; |
| 52 | + |
| 53 | +const maplibreTest = (maplibregl as unknown as { __test: { workerUrlsAtConstruction: string[] } }) |
| 54 | + .__test; |
| 55 | + |
| 56 | +afterEach(() => { |
| 57 | + maplibreTest.workerUrlsAtConstruction.length = 0; |
| 58 | +}); |
| 59 | + |
| 60 | +describe("LocationMinimap", () => { |
| 61 | + it("configures the versioned worker before constructing its map", async () => { |
| 62 | + render(<LocationMinimap lng={6.084959} lat={50.780558} />); |
| 63 | + |
| 64 | + await waitFor(() => expect(maplibreTest.workerUrlsAtConstruction).toHaveLength(1)); |
| 65 | + expect(maplibreTest.workerUrlsAtConstruction).toEqual([ |
| 66 | + "/runtime/maplibre-gl/6.1.0/maplibre-gl-worker.mjs", |
| 67 | + ]); |
| 68 | + }); |
| 69 | +}); |
0 commit comments