/
hl-leaflet.test.ts
312 lines (280 loc) · 10.7 KB
/
hl-leaflet.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Fix utf-8 encoding issue
const encodings = require('iconv-lite/encodings');
const iconvLite = require('iconv-lite/lib');
iconvLite.getCodec('UTF-8');
const debug = false
import he from "he";
import express from 'express';
import puppeteer from "puppeteer";
// Fix puppeteer warning
process.env.PUPPETEER_DISABLE_HEADLESS_WARNING = "true";
const htmxServer = () => {
const app = express();
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.get("/dist/:file", (req: express.Request, res: express.Response) => {
res.header("Content-Type", "text/javascript")
res.sendFile(`/dist/${req.params.file}`, {root: __dirname + "/../"})
})
const map = (props?: {
center: number[],
zoom: number,
}) => {
const center = props?.center || [-33.8650, 151.2094]
const zoom = props?.zoom || 13
// Generate 100 markers
const markers = []
for (let i = 0; i < 100; i++) {
markers.push(`
<div
class="hl-marker"
data-hl-center="[${Math.random() * 180 - 90}, ${Math.random() * 360 - 180}]"
data-hl-popup="Marker ${i}"
></div>
`)
}
return `
<div
id="map-config"
class="hl-map"
hx-post="/map"
hx-trigger="click from:#map-reload"
hx-swap="outerHTML"
hx-vals=${JSON.stringify({center, zoom})}
data-hl-render="#map-render"
data-hl-center=${JSON.stringify(center)}
data-hl-zoom="${zoom}"
>
<div
class="hl-marker"
data-hl-center="[-33.8650, 151.2094]"
data-hl-popup="${he.encode(`
<button
hx-trigger="click"
hx-swap="outerHTML"
hx-target="#map-config"
hx-vals=${JSON.stringify({center: [-34.58602826472734, -58.388420463492665], zoom: 13})}
hx-post="/map"
>Go To Argentinia</button>
`)}"
></div>
${markers.join("\n")}
</div>
`
}
app.post("/more-markers", (req: express.Request, res: express.Response) => {
const bounds = req.body.bounds as number[][]
// Generate 5 markers within bounds
const markers = []
for (let i = 0; i < 5; i++) {
markers.push(`
<div
class="hl-marker"
data-hl-center="[${Math.random() * (bounds[1][0] - bounds[0][0]) + bounds[0][0]}, ${Math.random() * (bounds[1][1] - bounds[0][1]) + bounds[0][1]}]"
data-hl-popup="Marker ${i}"
></div>
`)
}
res.send(`
${markers.join("\n")}
`)
})
app.post("/map", (req: express.Request, res: express.Response) => {
res.send(map(req.body))
})
const head = `
<head>
<!-- htmx -->
<script
src="https://unpkg.com/htmx.org@1.9.9/dist/htmx.min.js"
crossorigin="anonymous"
></script>
<script
src="https://unpkg.com/htmx.org@1.9.9/dist/ext/json-enc.js"
crossorigin="anonymous"
></script>
<!-- leaflet -->
<script
src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
crossorigin="anonymous"
></script>
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.css"
crossorigin="anonymous"
/>
<!-- hl-leaflet -->
<script src="/dist/hl-leaflet.umd.js"></script>
</head>
`
app.get("/", (req: express.Request, res: express.Response) => {
res.send(`
<html>
${head}
<body
hx-ext="json-enc"
hx-boost
>
<h1>Hello World</h1>
<a href="/goodbye">Goodbye</a>
<button onclick="
const el = event.target
el.innerHTML = 'Loading...'
navigator.geolocation.getCurrentPosition((position) => {
const {latitude, longitude} = position.coords
const map = document.querySelector('#map-config')
map.setAttribute('data-hl-center', JSON.stringify([latitude, longitude]))
el.innerHTML = 'Gps Located'
})
">Gps Locate</button>
<button id="more-markers"
hx-post="/more-markers"
hx-vals="js:{bounds:JSON.parse(htmx.find('#map-config').getAttribute('data-hl-bounds'))}"
hx-trigger="click"
hx-target="#map-config"
hx-swap="beforeend"
>
More Markers
</button>
<button id="map-reload"
>Reload Map</button>
<button
class="america"
hx-trigger="click"
hx-swap="outerHTML"
hx-target="#map-config"
hx-vals=${JSON.stringify({center: [40.7128, -74.0060], zoom: 10})}
hx-post="/map"
>Go To America</button>
<div id="map-render"
style="height: 600px;"
hx-on:hl-leaflet-moveend="
const map = htmx.find('#map-config')
map.setAttribute('hx-vals',JSON.stringify(event.detail))
hlLeaflet.syncMove(map,event.detail)
"
hx-on:hl-leaflet-popupopen="htmx.process(event.detail.popup)"
></div>
${map()}
</body>
</html>
`);
})
app.get("/goodbye", (req: express.Request, res: express.Response) => {
res.send(`
<html>
${head}
<body
hx-ext="json-enc"
hx-boost
>
<h1>Goodbye, Cruel World</h1>
<a href="/">Hello</a>
<button id="map-reload"
>Reload Map</button>
<button
hx-trigger="click"
hx-swap="outerHTML"
hx-target="#map-config"
hx-vals=${JSON.stringify({center: [40.7128, -74.0060], zoom: 10})}
hx-post="/map"
>Go To America</button>
<div id="map-render"
style="height: 600px;"
hx-on:hl-leaflet-moveend="htmx.find('#map-config').setAttribute('hx-vals',JSON.stringify(event.detail))"></div>
${map()}
</body>
</html>
`);
})
const server = app.listen(0);
const addr = server.address();
let port = 0
if (!addr || typeof addr === "string") {
throw new Error("server address not found");
} else {
port = addr.port
}
return {server, url: `http://localhost:${port}/`}
}
describe("index", () => {
it("sanity", () => {
expect(1).toBe(1);
});
it("complex-htmx", async () => {
const htmx = htmxServer()
const browser = await puppeteer.launch(
{
headless: !debug,
devtools: debug,
}
)
const page = await browser.newPage();
await page.setViewport({width: 1000, height: 1000})
await page.goto(htmx.url);
await page.waitForSelector("h1");
const text = await page.evaluate(() => document.querySelector("h1")?.textContent);
expect(text).toBe("Hello World");
// Expect the vals to be the same as the initial vals
let vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [-33.8650, 151.2094], zoom: 13}))
// Click the marker
await page.click(".leaflet-marker-icon")
await page.waitForSelector(".leaflet-popup-content button");
await page.click(".leaflet-popup-content button")
vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [-34.58602826472734, -58.388420463492665], zoom: 13}))
// Click the button to go to America
await page.click("button.america")
// wait for 50ms for the map to load
await page.waitForTimeout(50)
vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [40.7128, -74.0060], zoom: 10}))
// Pan the map
await page.mouse.move(500, 500)
await page.mouse.down()
await page.mouse.move(600, 600)
await page.mouse.up()
vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [40.81692723191517, -74.14398193359376], zoom: 10}))
// Click goodbye
await page.click("a")
await page.waitForSelector("h1");
const text2 = await page.evaluate(() => document.querySelector("h1")?.textContent);
expect(text2).toBe("Goodbye, Cruel World");
// Expect the map to be the same as before
vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [-33.8650, 151.2094], zoom: 13}))
// Directly manage the data-hl-center attribute
await page.evaluate(() => {
const map = document.querySelector("#map-config")
map?.setAttribute("data-hl-center", JSON.stringify([40.7128, -74.0060]))
})
// wait for 50ms for the map to load
await page.waitForTimeout(50)
vals = await page.evaluate(() => {
const map = document.querySelector("#map-config")
return map?.getAttribute("hx-vals")
})
expect(vals).toBe(JSON.stringify({center: [40.7128, -74.0060], zoom: 13}))
if (!debug) {
await browser.close();
htmx.server.close();
}
});
})