mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
fix(map): make stops that share a spot reachable again
A trip that models one building as several stops — drop the bags, check in, the museum inside the hotel — drew every pin on the same pixel, and only the top one could be clicked, hovered or seen. Clustering is the mechanism for exactly that: the plugin keeps children no zoom level can separate in one cluster and fans them apart on click. disableClusteringAtZoom pinned the group's own maximum zoom to 8, which both removed every cluster above it and moved the only fan trigger into a zoom band a trip planner never operates in, so the spiderfyOnMaxZoom set beside it could never fire. The cutoff is gone. maxClusterRadius is a function of zoom instead: the same generous radius as before in the overview, a pin's width above it, so stops that are genuinely apart keep their own pins and a pile becomes a counted bubble that fans open. Selecting a buried stop from the places rail now opens the bubble it is hiding in rather than doing nothing visible, and it does that without moving the camera, which stays the selection's own concern. The GL renderer has no fan to open, so folding a pin there removes the only thing that was left rather than providing a way in. It folds at a far tighter radius — genuine coincidence, where nothing was reachable to begin with — and draws the selected member of a pile, so the rail reaches it there too. A map object without a projector now skips folding instead of dropping every marker. The public share page spreads the same options and has no rail to fall back on, which made it the worse case; it is fixed by the same change. Closes #2344
This commit is contained in:
@@ -56,6 +56,37 @@ vi.mock('../../hooks/useGeolocation', () => ({
|
||||
// after the map already rendered.
|
||||
const thumbCallbacks = vi.hoisted(() => new Map<string, (thumb: string) => 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<Element, object>(),
|
||||
/**
|
||||
* 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', () => ({
|
||||
<div data-testid="map-container" data-center={JSON.stringify(center)} data-zoom={zoom} data-maxzoom={maxZoom}>{children}</div>
|
||||
),
|
||||
TileLayer: () => <div data-testid="tile-layer" />,
|
||||
Marker: ({ children, eventHandlers, position, icon, zIndexOffset }: any) => (
|
||||
Marker: ({ children, eventHandlers, position, icon, zIndexOffset, ref }: any) => (
|
||||
<div
|
||||
ref={node => { 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) => (
|
||||
<div data-testid="cluster-group">
|
||||
default: ({ children, iconCreateFunction, ref }: any) => (
|
||||
<div data-testid="cluster-group" ref={node => { ref?.(node ? clusterMock.group : null) }}>
|
||||
<button
|
||||
data-testid="cluster-icon-probe"
|
||||
onClick={(e: any) => {
|
||||
@@ -174,6 +212,9 @@ const ORIGINAL_WIDTH = window.innerWidth
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
resetAllStores()
|
||||
clusterMock.markers.clear()
|
||||
clusterMock.asked.length = 0
|
||||
clusterMock.stacked = false
|
||||
mapMock.panes.clear()
|
||||
thumbCallbacks.clear()
|
||||
geoMock.position = null
|
||||
@@ -1242,3 +1283,54 @@ describe('MapView — untrusted values in the marker HTML', () => {
|
||||
expect(iconHtml()).toContain('#00ff00')
|
||||
})
|
||||
})
|
||||
|
||||
// Stops modelling one building — drop the bags, check in, the museum inside it — all
|
||||
// carry the same coordinates, so only the top pin of the pile is ever on screen. The
|
||||
// places rail used to raise the chosen one with a z-index; inside a bubble there is no
|
||||
// pin to raise, so the bubble has to open instead (#2344).
|
||||
describe('MapView — picking a stop that shares its coordinates', () => {
|
||||
const hotel = { lat: 48.8584, lng: 2.2945 }
|
||||
const stacked = [
|
||||
buildMapPlace({ id: 1, name: 'Drop the bags', ...hotel }),
|
||||
buildMapPlace({ id: 2, name: 'Check in', ...hotel }),
|
||||
buildMapPlace({ id: 3, name: 'Louvre', lat: 48.8606, lng: 2.3376 }),
|
||||
]
|
||||
/** The place pins, in the order the places were handed over. */
|
||||
const pins = () => screen.getAllByTestId('marker').filter(node => node.closest('[data-testid="cluster-group"]'))
|
||||
const markerOf = (index: number) => clusterMock.markers.get(pins()[index])
|
||||
|
||||
it('FE-COMP-MAPVIEW-077: the bubble it is hiding in is fanned open', () => {
|
||||
clusterMock.stacked = true
|
||||
const { rerender } = render(<MapView places={stacked} />)
|
||||
expect(clusterMock.spiderfy).not.toHaveBeenCalled()
|
||||
|
||||
rerender(<MapView places={stacked} selectedPlaceId={2} />)
|
||||
expect(clusterMock.spiderfy).toHaveBeenCalled()
|
||||
// And it asked about the stop that was picked — not about the one stacked under it,
|
||||
// which carries the same coordinates, and not about whichever registered first.
|
||||
expect(clusterMock.asked).toContain(markerOf(1))
|
||||
expect(clusterMock.asked).not.toContain(markerOf(0))
|
||||
expect(clusterMock.asked).not.toContain(markerOf(2))
|
||||
})
|
||||
|
||||
it('FE-COMP-MAPVIEW-079: a stop in another pile is looked up by its own id', () => {
|
||||
clusterMock.stacked = true
|
||||
const { rerender } = render(<MapView places={stacked} />)
|
||||
rerender(<MapView places={stacked} selectedPlaceId={3} />)
|
||||
|
||||
expect(clusterMock.asked).toContain(markerOf(2))
|
||||
expect(clusterMock.asked).not.toContain(markerOf(0))
|
||||
expect(clusterMock.asked).not.toContain(markerOf(1))
|
||||
})
|
||||
|
||||
it('FE-COMP-MAPVIEW-078: a stop with a pin of its own is left alone, camera included', () => {
|
||||
clusterMock.stacked = false
|
||||
const { rerender } = render(<MapView places={stacked} />)
|
||||
rerender(<MapView places={stacked} selectedPlaceId={1} />)
|
||||
|
||||
expect(clusterMock.spiderfy).not.toHaveBeenCalled()
|
||||
// The selection still only pans: the zoom belongs to the day fit.
|
||||
expect(mapMock.panTo).toHaveBeenCalled()
|
||||
expect(mapMock.setView).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user