diff --git a/client/src/components/Map/MapView.test.tsx b/client/src/components/Map/MapView.test.tsx index 3a35c46d3..e81ec544f 100644 --- a/client/src/components/Map/MapView.test.tsx +++ b/client/src/components/Map/MapView.test.tsx @@ -56,6 +56,37 @@ vi.mock('../../hooks/useGeolocation', () => ({ // after the map already rendered. const thumbCallbacks = vi.hoisted(() => new Map void>()) +// The cluster group and the Leaflet markers MapView reaches for when a selection lands +// on a stop that shares its coordinates and so has no pin of its own. `stacked` decides +// whether the group answers with a bubble or with the marker itself. +const clusterMock = vi.hoisted(() => { + const spiderfy = vi.fn() + return { + spiderfy, + stacked: false, + asked: [] as unknown[], + markers: new Map(), + /** + * One marker double per pin, not per coordinate. Stops that share a coordinate are + * the whole point here, and a registry keyed by place id only proves anything when + * the two markers on that coordinate can be told apart. + */ + stub(node: Element) { + const existing = clusterMock.markers.get(node) + if (existing) return existing + const fresh = { pin: node } + clusterMock.markers.set(node, fresh) + return fresh + }, + group: { + getVisibleParent(marker: unknown) { + clusterMock.asked.push(marker) + return clusterMock.stacked ? { spiderfy } : marker + }, + }, + } +}) + vi.mock('react-leaflet', () => ({ // center/zoom are surfaced so tests can assert the camera the map is built // with; maxZoom because a cluster refuses to attach to a map without one. @@ -63,9 +94,16 @@ vi.mock('react-leaflet', () => ({
{children}
), TileLayer: () =>
, - Marker: ({ children, eventHandlers, position, icon, zIndexOffset }: any) => ( + Marker: ({ children, eventHandlers, position, icon, zIndexOffset, ref }: any) => (
{ if (node && zIndexOffset === 500) eventHandlers?.add?.({ target: { getElement: () => node } }) }} + ref={node => { + if (node && zIndexOffset === 500) eventHandlers?.add?.({ target: { getElement: () => node } }) + // Stands in for the L.Marker react-leaflet hands back, so MapView's registry has + // something to look a selected stop up by. Only the markers the cluster group + // holds get one: a place pin is all this double stands in for, and the route-via + // markers outside the group expect a Leaflet marker of their own. + ref?.(node?.closest('[data-testid="cluster-group"]') ? clusterMock.stub(node) : null) + }} data-testid="marker" data-lat={position[0]} data-lng={position[1]} @@ -118,8 +156,8 @@ vi.mock('react-leaflet', () => ({ vi.mock('react-leaflet-cluster', () => ({ // The real cluster group calls iconCreateFunction itself; the probe button // lets a test invoke it with a chosen child count. - default: ({ children, iconCreateFunction }: any) => ( -
+ default: ({ children, iconCreateFunction, ref }: any) => ( +
{ ref?.(node ? clusterMock.group : null) }}>