leaflet-components/test/components/geojson.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

53 lines
1.5 KiB
TypeScript

import { GeoJSON } from 'leaflet';
import { describe, expect, it } from 'vitest';
import '../../src/components/leaflet-geojson.ts';
const FEATURE = {
type: 'Feature',
geometry: {
type: 'Polygon',
coordinates: [
[
[-0.13, 51.5],
[-0.12, 51.5],
[-0.12, 51.51],
[-0.13, 51.51],
[-0.13, 51.5],
],
],
},
properties: {},
};
describe('leaflet-geojson', () => {
it('builds a GeoJSON layer from the data attribute with pathProps nested as style', () => {
const el = document.createElement('leaflet-geojson');
el.setAttribute('data', JSON.stringify(FEATURE));
el.setAttribute('color', '#e67e22');
el.setAttribute('fill-opacity', '0.4');
document.body.append(el);
expect(el.leafletObject).toBeInstanceOf(GeoJSON);
expect(el.leafletObject?.getLayers()).toHaveLength(1);
const style = el.leafletObject?.options.style as { color?: string; fillOpacity?: number };
expect(style.color).toBe('#e67e22');
expect(style.fillOpacity).toBe(0.4);
el.remove();
});
it('replaces its layers when the data attribute changes', () => {
const el = document.createElement('leaflet-geojson');
el.setAttribute('data', JSON.stringify(FEATURE));
document.body.append(el);
expect(el.leafletObject?.getLayers()).toHaveLength(1);
el.setAttribute(
'data',
JSON.stringify({ type: 'FeatureCollection', features: [FEATURE, FEATURE] }),
);
expect(el.leafletObject?.getLayers()).toHaveLength(2);
el.remove();
});
});