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.
74 lines
3.3 KiB
TypeScript
74 lines
3.3 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import '../src/components/leaflet-map.ts';
|
|
import '../src/components/leaflet-tile-layer.ts';
|
|
import '../src/components/leaflet-feature-group.ts';
|
|
import '../src/components/leaflet-marker.ts';
|
|
import '../src/components/leaflet-popup.ts';
|
|
|
|
// Exercises the registration-bubbling protocol end to end across a small real
|
|
// tree, rather than one component at a time: <leaflet-register> events bubble
|
|
// from marker -> feature-group -> map, and popup binds to its parent marker
|
|
// instead of bubbling past it.
|
|
describe('component tree wiring', () => {
|
|
it('wires a map + tile-layer + feature-group + marker + popup tree together', () => {
|
|
const map = document.createElement('leaflet-map');
|
|
map.setAttribute('lat', '51.5');
|
|
map.setAttribute('lng', '-0.09');
|
|
map.setAttribute('zoom', '13');
|
|
|
|
map.zoomSnap = 0.5;
|
|
|
|
const tiles = document.createElement('leaflet-tile-layer');
|
|
tiles.setAttribute('url', 'https://tile.example/{z}/{x}/{y}.png');
|
|
|
|
const group = document.createElement('leaflet-feature-group');
|
|
|
|
const marker = document.createElement('leaflet-marker');
|
|
marker.setAttribute('lat', '51.51');
|
|
marker.setAttribute('lng', '-0.1');
|
|
|
|
const popup = document.createElement('leaflet-popup');
|
|
popup.innerHTML = 'hello';
|
|
|
|
marker.append(popup);
|
|
group.append(marker);
|
|
map.append(tiles, group);
|
|
document.body.append(map);
|
|
|
|
// tile-layer and feature-group both registered directly onto the map.
|
|
expect(map.leafletObject?.hasLayer(tiles.leafletObject!)).toBe(true);
|
|
expect(map.leafletObject?.hasLayer(group.leafletObject!)).toBe(true);
|
|
|
|
// marker registered onto the feature-group (which, per Leaflet's own
|
|
// LayerGroup.onAdd, also adds each child directly onto the map so it
|
|
// renders -- hasLayer is true on both, membership is what distinguishes them).
|
|
expect(group.leafletObject?.hasLayer(marker.leafletObject!)).toBe(true);
|
|
expect(map.leafletObject?.hasLayer(marker.leafletObject!)).toBe(true);
|
|
expect(group.leafletObject?.getLayers()).toEqual([marker.leafletObject]);
|
|
|
|
// popup bound to the marker, not registered as a map layer at all.
|
|
expect(marker.leafletObject?.getPopup()).toBe(popup.leafletObject);
|
|
|
|
// A marker added later goes through the same path.
|
|
const marker2 = document.createElement('leaflet-marker');
|
|
marker2.setAttribute('lat', '1');
|
|
marker2.setAttribute('lng', '2');
|
|
group.append(marker2);
|
|
expect(group.leafletObject?.hasLayer(marker2.leafletObject!)).toBe(true);
|
|
|
|
// Disconnecting a child removes it from the map (WithProps calls the
|
|
// Leaflet object's own .remove(), which detaches from its _map).
|
|
// leafletObject is gone once disconnected, so capture the reference first.
|
|
// Note this is a pre-existing Leaflet quirk, not something WithProps
|
|
// introduces: Layer#remove() only detaches from the map, it does not tell
|
|
// an owning LayerGroup/FeatureGroup to forget it (that needs the group's
|
|
// own removeLayer()) -- so the group's hasLayer() stays stale/true here.
|
|
const removedMarkerObj = marker.leafletObject!;
|
|
marker.remove();
|
|
expect(map.leafletObject?.hasLayer(removedMarkerObj)).toBe(false);
|
|
expect(group.leafletObject?.hasLayer(removedMarkerObj)).toBe(true);
|
|
|
|
map.remove();
|
|
});
|
|
});
|