leaflet-components/test/components/latlng-bindables.test.ts
Buddy 0419ab781d feat: type leaflet-* components and their leaflet: events
Two augmentations, so TypeScript actually knows about the custom elements:

- HTMLElementTagNameMap (src/index.ts): createElement/querySelector now
  infer the exact component class for all 24 tags instead of HTMLElement.
- Per-component addEventListener/removeEventListener overrides, typing
  `leaflet:<name>` events against the real Leaflet event payload
  (PopupEvent, DragEndEvent, LeafletMouseEvent, etc.) while still accepting
  ordinary DOM events normally, and rejecting event names that component
  doesn't fire.

src/core/event-types.ts holds reusable event-name -> payload-type
fragments (mirroring shared-props.ts's fragment reuse), composed per
family: MapEvents, MarkerEvents, PathEvents, TileLayerEvents,
DivOverlayLayerEvents, GroupEvents. LeafletAddEventListener<T>/
LeafletRemoveEventListener<T> in with-props.ts are type-only
intersection-of-overloads helpers applied via `declare addEventListener:
...`, the same pattern every component already uses for `declare readonly
leafletObject?: X` -- zero runtime cost. Deliberately no generic `string`
fallback overload: a fallback would silently accept unrecognized
`leaflet:*` names too, defeating the point.

Fixed a real bug found while building this: #forwardEvents was
dispatching the raw pre-merge `data` Leaflet passes to fire(), missing
type/target/sourceTarget that Leaflet's own fire() merges in before
notifying real .on() listeners. Typing `detail` against Leaflet's actual
event interfaces would have been dishonest otherwise, so the merge now
matches Leaflet's own Evented#fire.

Also added 'line-updated' to the existing internal-event HTMLElementEventMap
augmentation in register.ts (needed once addEventListener got overridden
on polygon/polyline, which use it internally) and cleaned up ~35 now-
redundant `as HTMLElement & {...}` casts across the test suite that the
tag name map makes unnecessary.
2026-08-22 13:30:12 -07:00

109 lines
3.9 KiB
TypeScript

import { Marker, Popup, Tooltip } from 'leaflet';
import { describe, expect, it } from 'vitest';
import '../../src/components/leaflet-map.ts';
import '../../src/components/leaflet-marker.ts';
import '../../src/components/leaflet-popup.ts';
import '../../src/components/leaflet-tooltip.ts';
import '../../src/components/leaflet-icon.ts';
describe('leaflet-marker', () => {
it('creates a Marker at lat/lng and keeps lat/lng synced while it moves', () => {
const el = document.createElement('leaflet-marker');
el.setAttribute('lat', '51.5');
el.setAttribute('lng', '-0.09');
document.body.append(el);
expect(el.leafletObject).toBeInstanceOf(Marker);
expect(el.leafletObject?.getLatLng()).toMatchObject({ lat: 51.5, lng: -0.09 });
el.leafletObject?.setLatLng([1, 2]);
expect(el.lat).toBe(1);
expect(el.lng).toBe(2);
// event: 'move' syncs the attribute too
expect(el.getAttribute('lat')).toBe('1');
expect(el.getAttribute('lng')).toBe('2');
el.remove();
});
it('draggable toggles the Leaflet dragging handler', () => {
// The dragging handler is only initialized in onAdd(), so the marker
// needs to actually be added to a map first.
const map = document.createElement('leaflet-map');
document.body.append(map);
const el = document.createElement('leaflet-marker');
el.setAttribute('lat', '0');
el.setAttribute('lng', '0');
el.setAttribute('draggable', '');
map.append(el);
expect(el.leafletObject?.dragging?.enabled()).toBe(true);
el.removeAttribute('draggable');
expect(el.leafletObject?.dragging?.enabled()).toBe(false);
map.remove();
});
it('swaps in a <leaflet-icon> child via the icon-changed handshake', () => {
const el = document.createElement('leaflet-marker');
el.setAttribute('lat', '0');
el.setAttribute('lng', '0');
document.body.append(el);
const icon = document.createElement('leaflet-icon');
icon.setAttribute('icon-url', 'https://example.com/icon.png');
el.append(icon);
expect(el.leafletObject?.options.icon).toBe(icon.leafletObject);
el.remove();
});
});
describe('leaflet-popup', () => {
it('takes its content from innerHTML and stays in sync on mutation', async () => {
const el = document.createElement('leaflet-popup');
el.innerHTML = '<b>hello</b>';
document.body.append(el);
expect(el.leafletObject).toBeInstanceOf(Popup);
expect(el.leafletObject?.getContent()).toBe('<b>hello</b>');
el.innerHTML = '<b>updated</b>';
// MutationObserver callbacks run as a microtask.
await Promise.resolve();
expect(el.leafletObject?.getContent()).toBe('<b>updated</b>');
el.remove();
});
it('only sets its own latlng when lat/lng attributes are present', () => {
const withLatLng = document.createElement('leaflet-popup');
withLatLng.setAttribute('lat', '1');
withLatLng.setAttribute('lng', '2');
document.body.append(withLatLng);
expect(withLatLng.leafletObject?.getLatLng()).toMatchObject({ lat: 1, lng: 2 });
withLatLng.remove();
const withoutLatLng = document.createElement('leaflet-popup');
document.body.append(withoutLatLng);
expect(withoutLatLng.leafletObject?.getLatLng()).toBeUndefined();
withoutLatLng.remove();
});
});
describe('leaflet-tooltip', () => {
it('creates a Tooltip with content and direction/permanent/sticky options', () => {
const el = document.createElement('leaflet-tooltip');
el.innerHTML = 'hover text';
el.setAttribute('direction', 'top');
el.setAttribute('permanent', '');
el.setAttribute('sticky', '');
document.body.append(el);
expect(el.leafletObject).toBeInstanceOf(Tooltip);
expect(el.leafletObject?.getContent()).toBe('hover text');
expect(el.leafletObject?.options.direction).toBe('top');
expect(el.leafletObject?.options.permanent).toBe(true);
expect(el.leafletObject?.options.sticky).toBe(true);
el.remove();
});
});