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

57 lines
2.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { DivIcon, Icon } from 'leaflet';
import { describe, expect, it, vi } from 'vitest';
import '../../src/components/leaflet-icon.ts';
import '../../src/components/leaflet-div-icon.ts';
describe('leaflet-icon', () => {
it('creates nothing until icon-url is set, then an Icon once it is', () => {
const el = document.createElement('leaflet-icon');
document.body.append(el);
expect(el.leafletObject).toBeUndefined();
el.setAttribute('icon-url', 'https://example.com/icon.png');
expect(el.leafletObject).toBeInstanceOf(Icon);
expect(el.leafletObject?.options.iconUrl).toBe('https://example.com/icon.png');
el.remove();
});
it('rebuilds (not mutates) the Icon on every attribute change and re-emits icon-changed', () => {
const el = document.createElement('leaflet-icon');
el.setAttribute('icon-url', 'https://example.com/a.png');
const onChanged = vi.fn();
el.addEventListener('icon-changed', onChanged);
document.body.append(el);
const first = el.leafletObject;
onChanged.mockClear();
el.setAttribute('icon-size', JSON.stringify([32, 32]));
expect(el.leafletObject).not.toBe(first);
expect(onChanged).toHaveBeenCalledOnce();
el.remove();
});
});
describe('leaflet-div-icon', () => {
it('defaults className to Leaflet’s own default and renders markup as html', () => {
const el = document.createElement('leaflet-div-icon');
el.innerHTML = '<span>*</span>';
document.body.append(el);
expect(el.leafletObject).toBeInstanceOf(DivIcon);
expect(el.leafletObject?.options.className).toBe('leaflet-div-icon');
expect(el.leafletObject?.options.html).toBe('<span>*</span>');
el.remove();
});
it('emits icon-changed(null) on disconnect', () => {
const el = document.createElement('leaflet-div-icon');
document.body.append(el);
const onChanged = vi.fn();
el.addEventListener('icon-changed', onChanged);
el.remove();
expect(onChanged).toHaveBeenCalledOnce();
expect((onChanged.mock.calls[0][0] as CustomEvent).detail.icon).toBeNull();
});
});