|
| 1 | +import { expect, type Page, test } from "@playwright/test"; |
| 2 | + |
| 3 | +const BASE = process.env.SQLPAGE_TEST_BASE ?? "http://localhost:8080/"; |
| 4 | + |
| 5 | +declare global { |
| 6 | + function sqlpage_map(): void; |
| 7 | +} |
| 8 | + |
| 9 | +type Marker = { coords?: string; title: string }; |
| 10 | + |
| 11 | +const PARIS = "48.85,2.35"; |
| 12 | +const PARIS_WITHOUT_ITS_LONGITUDE = "48.85,"; |
| 13 | +const NOT_COORDINATES = "somewhere nice"; |
| 14 | + |
| 15 | +async function renderMap( |
| 16 | + page: Page, |
| 17 | + center: string | null, |
| 18 | + markers: Marker[] = [], |
| 19 | +) { |
| 20 | + return page.evaluate( |
| 21 | + async ({ center, markers }) => { |
| 22 | + document.getElementById("test-map")?.remove(); |
| 23 | + const container = document.createElement("div"); |
| 24 | + container.id = "test-map"; |
| 25 | + container.className = "leaflet"; |
| 26 | + container.style.height = "200px"; |
| 27 | + container.dataset.zoom = "5"; |
| 28 | + container.dataset.max_zoom = "18"; |
| 29 | + if (center !== null) container.dataset.center = center; |
| 30 | + container.innerHTML = markers |
| 31 | + .map( |
| 32 | + (m) => |
| 33 | + `<div class="marker"${m.coords === undefined ? "" : ` data-coords="${m.coords}"`}><h3>${m.title}</h3></div>`, |
| 34 | + ) |
| 35 | + .join(""); |
| 36 | + container.dataset.preInit = "map"; |
| 37 | + document.body.appendChild(container); |
| 38 | + |
| 39 | + const errors: string[] = []; |
| 40 | + const record = (e: ErrorEvent) => errors.push(e.message); |
| 41 | + window.addEventListener("error", record); |
| 42 | + |
| 43 | + const logged: string[] = []; |
| 44 | + const console_error = console.error; |
| 45 | + console.error = (...args) => logged.push(args.join(" ")); |
| 46 | + |
| 47 | + sqlpage_map(); |
| 48 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 49 | + |
| 50 | + console.error = console_error; |
| 51 | + window.removeEventListener("error", record); |
| 52 | + |
| 53 | + return { |
| 54 | + errors, |
| 55 | + logged, |
| 56 | + markers: container.querySelectorAll(".leaflet-marker-icon").length, |
| 57 | + initialized: !!container.querySelector(".leaflet-map-pane"), |
| 58 | + }; |
| 59 | + }, |
| 60 | + { center, markers }, |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +test.beforeEach(async ({ page }) => { |
| 65 | + await page.goto(`${BASE}/documentation.sql?component=map#component`); |
| 66 | + await page.waitForFunction(() => "L" in window); |
| 67 | +}); |
| 68 | + |
| 69 | +test("centers the map on a pair of coordinates", async ({ page }) => { |
| 70 | + const map = await renderMap(page, PARIS); |
| 71 | + |
| 72 | + expect(map.errors).toEqual([]); |
| 73 | + expect(map.logged).toEqual([]); |
| 74 | + expect(map.initialized).toBe(true); |
| 75 | +}); |
| 76 | + |
| 77 | +test("reports a center whose longitude is missing", async ({ page }) => { |
| 78 | + const map = await renderMap(page, PARIS_WITHOUT_ITS_LONGITUDE); |
| 79 | + |
| 80 | + expect(map.errors).toEqual([]); |
| 81 | + expect(map.logged).toEqual([ |
| 82 | + expect.stringContaining(PARIS_WITHOUT_ITS_LONGITUDE), |
| 83 | + ]); |
| 84 | + expect(map.initialized).toBe(true); |
| 85 | +}); |
| 86 | + |
| 87 | +test("reports a center that is not a pair of numbers", async ({ page }) => { |
| 88 | + const map = await renderMap(page, NOT_COORDINATES); |
| 89 | + |
| 90 | + expect(map.errors).toEqual([]); |
| 91 | + expect(map.logged).toEqual([expect.stringContaining(NOT_COORDINATES)]); |
| 92 | + expect(map.initialized).toBe(true); |
| 93 | +}); |
| 94 | + |
| 95 | +test("draws a marker at a pair of coordinates", async ({ page }) => { |
| 96 | + const map = await renderMap(page, PARIS, [{ coords: PARIS, title: "Paris" }]); |
| 97 | + |
| 98 | + expect(map.errors).toEqual([]); |
| 99 | + expect(map.logged).toEqual([]); |
| 100 | + expect(map.markers).toBe(1); |
| 101 | +}); |
| 102 | + |
| 103 | +test("reports a marker whose longitude is missing, keeping the others", async ({ |
| 104 | + page, |
| 105 | +}) => { |
| 106 | + const map = await renderMap(page, PARIS, [ |
| 107 | + { coords: PARIS_WITHOUT_ITS_LONGITUDE, title: "Half of Paris" }, |
| 108 | + { coords: PARIS, title: "Paris" }, |
| 109 | + ]); |
| 110 | + |
| 111 | + expect(map.errors).toEqual([]); |
| 112 | + expect(map.logged).toEqual([ |
| 113 | + expect.stringContaining(PARIS_WITHOUT_ITS_LONGITUDE), |
| 114 | + ]); |
| 115 | + expect(map.markers).toBe(1); |
| 116 | +}); |
0 commit comments