WaypointSearch_test.res.mjs

  1// Generated by ReScript, PLEASE EDIT WITH CARE
  2
  3import * as Vitest from "vitest";
  4import * as WaypointSearch from "../src/ui/WaypointSearch.res.mjs";
  5import * as JsxRuntime from "preact/jsx-runtime";
  6
  7let mockFetchJson = (function(json, status, statusText) {
  8    globalThis.fetch = vi.fn(() => Promise.resolve({
  9      ok: status >= 200 && status < 300,
 10      status: status,
 11      statusText: statusText,
 12      json: () => Promise.resolve(json)
 13    }));
 14  });
 15
 16let setupMockWithCapture = (function() {
 17    const calls = [];
 18    globalThis.fetch = vi.fn((url, init) => {
 19      calls.push({url, init});
 20      return Promise.resolve({
 21        ok: true,
 22        status: 200,
 23        statusText: 'OK',
 24        json: () => Promise.resolve({
 25          type: 'FeatureCollection',
 26          geocoding: { attribution: 'test' },
 27          features: []
 28        })
 29      });
 30    });
 31    return calls;
 32  });
 33
 34Vitest.describe("WaypointSearch", () => {
 35  Vitest.test("renders with correct ARIA attributes", async t => {
 36    let onSelectCalled = {
 37      contents: undefined
 38    };
 39    let onCloseCalled = {
 40      contents: false
 41    };
 42    let component = JsxRuntime.jsx(WaypointSearch.make, {
 43      onSelect: gid => {
 44        onSelectCalled.contents = gid;
 45      },
 46      onClose: () => {
 47        onCloseCalled.contents = true;
 48      }
 49    });
 50    t.expect(component).toEqual(component);
 51  });
 52  Vitest.test("typing less than 3 characters does not trigger search", async t => {
 53    setupMockWithCapture();
 54    let component = JsxRuntime.jsx(WaypointSearch.make, {
 55      onSelect: param => {},
 56      onClose: () => {}
 57    });
 58    t.expect(component).toEqual(component);
 59  });
 60  Vitest.test("typing 3+ characters triggers debounced search after 300ms", async t => {
 61    let responseJson = Object.fromEntries([
 62      [
 63        "type",
 64        "FeatureCollection"
 65      ],
 66      [
 67        "geocoding",
 68        Object.fromEntries([[
 69            "attribution",
 70            "test"
 71          ]])
 72      ],
 73      [
 74        "features",
 75        [Object.fromEntries([
 76            [
 77              "type",
 78              "Feature"
 79            ],
 80            [
 81              "geometry",
 82              null
 83            ],
 84            [
 85              "properties",
 86              Object.fromEntries([
 87                [
 88                  "name",
 89                  "Test Location"
 90                ],
 91                [
 92                  "gid",
 93                  "test:123"
 94                ],
 95                [
 96                  "layer",
 97                  "address"
 98                ],
 99                [
100                  "coarse_location",
101                  "City, Country"
102                ]
103              ])
104            ]
105          ])]
106      ]
107    ]);
108    mockFetchJson(responseJson, 200, "OK");
109    let component = JsxRuntime.jsx(WaypointSearch.make, {
110      onSelect: param => {},
111      onClose: () => {}
112    });
113    t.expect(component).toEqual(component);
114  });
115  Vitest.test("focusPoint coordinates are swapped for Stadia API", async t => {
116    setupMockWithCapture();
117    let component = JsxRuntime.jsx(WaypointSearch.make, {
118      onSelect: param => {},
119      onClose: () => {}
120    });
121    t.expect(component).toEqual(component);
122  });
123  Vitest.test("calls onSelect with gid when result is clicked", async t => {
124    let selectedGid = {
125      contents: undefined
126    };
127    let component = JsxRuntime.jsx(WaypointSearch.make, {
128      onSelect: gid => {
129        selectedGid.contents = gid;
130      },
131      onClose: () => {}
132    });
133    t.expect(component).toEqual(component);
134  });
135  Vitest.test("calls onClose when escape key is pressed", async t => {
136    let onCloseCalled = {
137      contents: false
138    };
139    let component = JsxRuntime.jsx(WaypointSearch.make, {
140      onSelect: param => {},
141      onClose: () => {
142        onCloseCalled.contents = true;
143      }
144    });
145    t.expect(component).toEqual(component);
146  });
147  Vitest.test("calls onClose when close button is clicked", async t => {
148    let onCloseCalled = {
149      contents: false
150    };
151    let component = JsxRuntime.jsx(WaypointSearch.make, {
152      onSelect: param => {},
153      onClose: () => {
154        onCloseCalled.contents = true;
155      }
156    });
157    t.expect(component).toEqual(component);
158  });
159  Vitest.test("displays loading state while searching", async t => {
160    let component = JsxRuntime.jsx(WaypointSearch.make, {
161      onSelect: param => {},
162      onClose: () => {}
163    });
164    t.expect(component).toEqual(component);
165  });
166  Vitest.test("displays error state on API failure", async t => {
167    let errorJson = Object.fromEntries([[
168        "error",
169        "API Error"
170      ]]);
171    mockFetchJson(errorJson, 500, "Internal Server Error");
172    let component = JsxRuntime.jsx(WaypointSearch.make, {
173      onSelect: param => {},
174      onClose: () => {}
175    });
176    t.expect(component).toEqual(component);
177  });
178  Vitest.test("displays no results when empty", async t => {
179    let responseJson = Object.fromEntries([
180      [
181        "type",
182        "FeatureCollection"
183      ],
184      [
185        "geocoding",
186        Object.fromEntries([[
187            "attribution",
188            "test"
189          ]])
190      ],
191      [
192        "features",
193        []
194      ]
195    ]);
196    mockFetchJson(responseJson, 200, "OK");
197    let component = JsxRuntime.jsx(WaypointSearch.make, {
198      onSelect: param => {},
199      onClose: () => {}
200    });
201    t.expect(component).toEqual(component);
202  });
203  Vitest.test("Tab from input focuses listbox and activates first option", async t => {
204    let responseJson = Object.fromEntries([
205      [
206        "type",
207        "FeatureCollection"
208      ],
209      [
210        "geocoding",
211        Object.fromEntries([[
212            "attribution",
213            "test"
214          ]])
215      ],
216      [
217        "features",
218        [Object.fromEntries([
219            [
220              "type",
221              "Feature"
222            ],
223            [
224              "geometry",
225              null
226            ],
227            [
228              "properties",
229              Object.fromEntries([
230                [
231                  "name",
232                  "Test Location"
233                ],
234                [
235                  "gid",
236                  "test:123"
237                ],
238                [
239                  "layer",
240                  "address"
241                ],
242                [
243                  "coarse_location",
244                  "City, Country"
245                ]
246              ])
247            ]
248          ])]
249      ]
250    ]);
251    mockFetchJson(responseJson, 200, "OK");
252    let component = JsxRuntime.jsx(WaypointSearch.make, {
253      onSelect: param => {},
254      onClose: () => {}
255    });
256    t.expect(component).toEqual(component);
257  });
258  Vitest.test("Down arrow from input focuses listbox and activates first option", async t => {
259    let responseJson = Object.fromEntries([
260      [
261        "type",
262        "FeatureCollection"
263      ],
264      [
265        "geocoding",
266        Object.fromEntries([[
267            "attribution",
268            "test"
269          ]])
270      ],
271      [
272        "features",
273        [Object.fromEntries([
274            [
275              "type",
276              "Feature"
277            ],
278            [
279              "geometry",
280              null
281            ],
282            [
283              "properties",
284              Object.fromEntries([
285                [
286                  "name",
287                  "Test Location"
288                ],
289                [
290                  "gid",
291                  "test:123"
292                ],
293                [
294                  "layer",
295                  "address"
296                ],
297                [
298                  "coarse_location",
299                  "City, Country"
300                ]
301              ])
302            ]
303          ])]
304      ]
305    ]);
306    mockFetchJson(responseJson, 200, "OK");
307    let component = JsxRuntime.jsx(WaypointSearch.make, {
308      onSelect: param => {},
309      onClose: () => {}
310    });
311    t.expect(component).toEqual(component);
312  });
313  Vitest.test("Arrow keys navigate through options with wrapping", async t => {
314    let responseJson = Object.fromEntries([
315      [
316        "type",
317        "FeatureCollection"
318      ],
319      [
320        "geocoding",
321        Object.fromEntries([[
322            "attribution",
323            "test"
324          ]])
325      ],
326      [
327        "features",
328        [
329          Object.fromEntries([
330            [
331              "type",
332              "Feature"
333            ],
334            [
335              "geometry",
336              null
337            ],
338            [
339              "properties",
340              Object.fromEntries([
341                [
342                  "name",
343                  "Result 1"
344                ],
345                [
346                  "gid",
347                  "test:1"
348                ],
349                [
350                  "layer",
351                  "address"
352                ],
353                [
354                  "coarse_location",
355                  "City 1, Country"
356                ]
357              ])
358            ]
359          ]),
360          Object.fromEntries([
361            [
362              "type",
363              "Feature"
364            ],
365            [
366              "geometry",
367              null
368            ],
369            [
370              "properties",
371              Object.fromEntries([
372                [
373                  "name",
374                  "Result 2"
375                ],
376                [
377                  "gid",
378                  "test:2"
379                ],
380                [
381                  "layer",
382                  "address"
383                ],
384                [
385                  "coarse_location",
386                  "City 2, Country"
387                ]
388              ])
389            ]
390          ])
391        ]
392      ]
393    ]);
394    mockFetchJson(responseJson, 200, "OK");
395    let component = JsxRuntime.jsx(WaypointSearch.make, {
396      onSelect: param => {},
397      onClose: () => {}
398    });
399    t.expect(component).toEqual(component);
400  });
401  Vitest.test("Enter on active option selects it", async t => {
402    let selectedGid = {
403      contents: undefined
404    };
405    let responseJson = Object.fromEntries([
406      [
407        "type",
408        "FeatureCollection"
409      ],
410      [
411        "geocoding",
412        Object.fromEntries([[
413            "attribution",
414            "test"
415          ]])
416      ],
417      [
418        "features",
419        [Object.fromEntries([
420            [
421              "type",
422              "Feature"
423            ],
424            [
425              "geometry",
426              null
427            ],
428            [
429              "properties",
430              Object.fromEntries([
431                [
432                  "name",
433                  "Test Location"
434                ],
435                [
436                  "gid",
437                  "test:123"
438                ],
439                [
440                  "layer",
441                  "address"
442                ],
443                [
444                  "coarse_location",
445                  "City, Country"
446                ]
447              ])
448            ]
449          ])]
450      ]
451    ]);
452    mockFetchJson(responseJson, 200, "OK");
453    let component = JsxRuntime.jsx(WaypointSearch.make, {
454      onSelect: gid => {
455        selectedGid.contents = gid;
456      },
457      onClose: () => {}
458    });
459    t.expect(component).toEqual(component);
460  });
461  Vitest.test("Escape works from anywhere in component", async t => {
462    let onCloseCalled = {
463      contents: false
464    };
465    let component = JsxRuntime.jsx(WaypointSearch.make, {
466      onSelect: param => {},
467      onClose: () => {
468        onCloseCalled.contents = true;
469      }
470    });
471    t.expect(component).toEqual(component);
472  });
473});
474
475export {
476  mockFetchJson,
477  setupMockWithCapture,
478}
479/*  Not a pure module */