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.
90 lines
3.7 KiB
TypeScript
90 lines
3.7 KiB
TypeScript
import { Control, FeatureGroup, LayerGroup } from 'leaflet';
|
|
import { describe, expect, it } from 'vitest';
|
|
import '../../src/components/leaflet-control-zoom.ts';
|
|
import '../../src/components/leaflet-control-scale.ts';
|
|
import '../../src/components/leaflet-control-attribution.ts';
|
|
import '../../src/components/leaflet-control-layers.ts';
|
|
import '../../src/components/leaflet-tile-layer.ts';
|
|
import '../../src/components/leaflet-layer-group.ts';
|
|
import '../../src/components/leaflet-feature-group.ts';
|
|
import '../../src/components/leaflet-marker.ts';
|
|
|
|
describe('leaflet-control-zoom / scale / attribution', () => {
|
|
it('create their matching Control class with position and options', () => {
|
|
const zoom = document.createElement('leaflet-control-zoom');
|
|
zoom.setAttribute('position', 'bottomleft');
|
|
document.body.append(zoom);
|
|
expect(zoom.leafletObject).toBeInstanceOf(Control.Zoom);
|
|
expect(zoom.leafletObject?.options.position).toBe('bottomleft');
|
|
zoom.remove();
|
|
|
|
const scale = document.createElement('leaflet-control-scale');
|
|
scale.setAttribute('max-width', '150');
|
|
document.body.append(scale);
|
|
expect(scale.leafletObject).toBeInstanceOf(Control.Scale);
|
|
expect(scale.leafletObject?.options.maxWidth).toBe(150);
|
|
scale.remove();
|
|
|
|
const attribution = document.createElement('leaflet-control-attribution');
|
|
attribution.setAttribute('prefix', 'Test');
|
|
document.body.append(attribution);
|
|
expect(attribution.leafletObject).toBeInstanceOf(Control.Attribution);
|
|
expect(attribution.leafletObject?.options.prefix).toBe('Test');
|
|
attribution.remove();
|
|
});
|
|
});
|
|
|
|
describe('leaflet-control-layers', () => {
|
|
it('sorts existing children into base layers vs overlays by the type attribute', () => {
|
|
const el = document.createElement('leaflet-control-layers');
|
|
const base = document.createElement('leaflet-tile-layer');
|
|
base.setAttribute('url', 'https://a.example/{z}/{x}/{y}.png');
|
|
base.setAttribute('name', 'Base');
|
|
base.setAttribute('type', 'base');
|
|
base.setAttribute('active', '');
|
|
|
|
const overlay = document.createElement('leaflet-tile-layer');
|
|
overlay.setAttribute('url', 'https://b.example/{z}/{x}/{y}.png');
|
|
overlay.setAttribute('name', 'Overlay');
|
|
|
|
el.append(base, overlay);
|
|
document.body.append(el);
|
|
|
|
expect(el.leafletObject).toBeInstanceOf(Control.Layers);
|
|
// Control.Layers has no public getter for its layer list, so this reaches
|
|
// into Leaflet's own private `_layers` field -- brittle across Leaflet
|
|
// versions, but it's the only way to verify the base/overlay split.
|
|
const layersInternal = el.leafletObject as unknown as {
|
|
_layers: { name: string; overlay?: boolean }[];
|
|
};
|
|
const byName = Object.fromEntries(
|
|
// oxlint-disable-next-line no-underscore-dangle -- Leaflet's own field name
|
|
layersInternal._layers.map((l) => [l.name, Boolean(l.overlay)]),
|
|
);
|
|
expect(byName['Base']).toBe(false);
|
|
expect(byName['Overlay']).toBe(true);
|
|
el.remove();
|
|
});
|
|
});
|
|
|
|
describe('leaflet-layer-group / leaflet-feature-group', () => {
|
|
it('are passthrough WithProps({}) containers that adopt registering children', () => {
|
|
const group = document.createElement('leaflet-layer-group');
|
|
document.body.append(group);
|
|
expect(group.leafletObject).toBeInstanceOf(LayerGroup);
|
|
|
|
const marker = document.createElement('leaflet-marker');
|
|
marker.setAttribute('lat', '1');
|
|
marker.setAttribute('lng', '2');
|
|
group.append(marker);
|
|
|
|
expect(group.leafletObject?.getLayers()).toHaveLength(1);
|
|
group.remove();
|
|
|
|
const feature = document.createElement('leaflet-feature-group');
|
|
document.body.append(feature);
|
|
expect(feature.leafletObject).toBeInstanceOf(FeatureGroup);
|
|
feature.remove();
|
|
});
|
|
});
|