mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 18:34:09 +00:00
feat(ferratas): keep loaded via ferratas in a tile cache on server and device, group segments, own look and count badge
This commit is contained in:
@@ -1,22 +1,34 @@
|
||||
/**
|
||||
* FE-FERRATA-LEAFLET-001 to FE-FERRATA-LEAFLET-007: the via ferrata layer on
|
||||
* FE-FERRATA-LEAFLET-001 to FE-FERRATA-LEAFLET-011: the via ferrata layer on
|
||||
* the Leaflet renderer.
|
||||
*
|
||||
* Leaflet itself is not under test: the mock reflects each polyline's props
|
||||
* back as data attributes, so the assertions read which lines are drawn, in
|
||||
* which order and with what options. Casing and line stay non-interactive so
|
||||
* they cannot swallow clicks meant for places; only the invisible hit line on
|
||||
* top carries the tooltip and the click.
|
||||
* Leaflet itself is not under test: the mock reflects each polyline's and
|
||||
* marker's props back as data attributes, so the assertions read which lines
|
||||
* are drawn, in which order and with what options. Casing and line stay
|
||||
* non-interactive so they cannot swallow clicks meant for places; only the
|
||||
* invisible hit line on top carries the tooltip, the hover and the click.
|
||||
*/
|
||||
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||||
import type { ReactNode } from 'react';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { Ferrata } from '@trek/shared';
|
||||
|
||||
import FerrataLayerLeaflet from './FerrataLayerLeaflet';
|
||||
import { groupFerratas } from './ferrataGroups';
|
||||
|
||||
vi.mock('../../i18n', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock('../../i18n', () => ({
|
||||
useTranslation: () => ({
|
||||
t: (key: string, params?: Record<string, string | number>) =>
|
||||
params ? `${key}(${Object.values(params).join(',')})` : key,
|
||||
}),
|
||||
}));
|
||||
|
||||
const zoom = { value: 12 };
|
||||
const zoomEnd: { handler: (() => void) | null } = { handler: null };
|
||||
|
||||
type Handlers = Record<string, ((event: { latlng: { lat: number; lng: number } }) => void) | undefined>;
|
||||
const at = { latlng: { lat: 46.505, lng: 11.705 } };
|
||||
|
||||
vi.mock('react-leaflet', () => ({
|
||||
Polyline: ({
|
||||
@@ -28,10 +40,10 @@ vi.mock('react-leaflet', () => ({
|
||||
children,
|
||||
}: {
|
||||
positions: unknown;
|
||||
pathOptions: { color: string; weight: number; opacity: number; dashArray?: string };
|
||||
pathOptions: { color: string; weight: number; opacity: number; dashArray?: string; className?: string };
|
||||
interactive?: boolean;
|
||||
bubblingMouseEvents?: boolean;
|
||||
eventHandlers?: { click?: (event: { latlng: { lat: number; lng: number } }) => void };
|
||||
eventHandlers?: Handlers;
|
||||
children?: ReactNode;
|
||||
}) => (
|
||||
<div
|
||||
@@ -41,103 +53,215 @@ vi.mock('react-leaflet', () => ({
|
||||
data-weight={pathOptions.weight}
|
||||
data-opacity={pathOptions.opacity}
|
||||
data-dash={pathOptions.dashArray ?? ''}
|
||||
data-class={pathOptions.className ?? ''}
|
||||
data-interactive={interactive === false ? 'false' : 'true'}
|
||||
data-bubbles={bubblingMouseEvents === false ? 'false' : 'true'}
|
||||
onClick={() => eventHandlers?.click?.({ latlng: { lat: 46.505, lng: 11.705 } })}
|
||||
onClick={() => eventHandlers?.click?.(at)}
|
||||
onMouseOver={() => eventHandlers?.mouseover?.(at)}
|
||||
onMouseOut={() => eventHandlers?.mouseout?.(at)}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
),
|
||||
Tooltip: ({ children }: { children: ReactNode }) => <span data-testid="tooltip">{children}</span>,
|
||||
Marker: ({ position, eventHandlers }: { position: [number, number]; eventHandlers?: Handlers }) => (
|
||||
<button
|
||||
type="button"
|
||||
data-testid="ferrata-icon"
|
||||
data-position={JSON.stringify(position)}
|
||||
onClick={() => eventHandlers?.click?.(at)}
|
||||
/>
|
||||
),
|
||||
Tooltip: ({ children, className }: { children: ReactNode; className?: string }) => (
|
||||
<span data-testid="tooltip" data-class={className ?? ''}>
|
||||
{children}
|
||||
</span>
|
||||
),
|
||||
// A pixel per 0.00001 degree: enough to tell icons apart that sit a few metres from each other.
|
||||
useMap: () => ({
|
||||
getZoom: () => zoom.value,
|
||||
project: ([lat, lng]: [number, number]) => ({ x: lng * 100_000, y: -lat * 100_000 }),
|
||||
}),
|
||||
useMapEvents: (handlers: { zoomend?: () => void }) => {
|
||||
zoomEnd.handler = handlers.zoomend ?? null;
|
||||
return null;
|
||||
},
|
||||
}));
|
||||
|
||||
const ferrata = (id: string, name: string | null, level: number | null, routeName: string | null = null): Ferrata => ({
|
||||
id,
|
||||
name,
|
||||
grade: { raw: level === null ? null : String(level), level, plus: level === 4 ? '+' : null },
|
||||
routeName,
|
||||
geometry: [
|
||||
[46.5, 11.7],
|
||||
[46.51, 11.71],
|
||||
],
|
||||
osmUrl: `https://www.openstreetmap.org/${id}`,
|
||||
const ferrata = (
|
||||
id: string,
|
||||
name: string | null,
|
||||
level: number | null,
|
||||
options: { routeName?: string | null; lat?: number; plus?: '+' | null } = {}
|
||||
): Ferrata => {
|
||||
const lat = options.lat ?? 46.5;
|
||||
return {
|
||||
id,
|
||||
name,
|
||||
grade: { raw: level === null ? null : String(level), level, plus: options.plus ?? null },
|
||||
routeName: options.routeName ?? null,
|
||||
geometry: [
|
||||
[lat, 11.7],
|
||||
[lat + 0.01, 11.71],
|
||||
],
|
||||
osmUrl: `https://www.openstreetmap.org/${id}`,
|
||||
};
|
||||
};
|
||||
|
||||
const lines = () => screen.getAllByTestId('polyline');
|
||||
|
||||
beforeEach(() => {
|
||||
zoom.value = 12;
|
||||
});
|
||||
|
||||
describe('FerrataLayerLeaflet', () => {
|
||||
it('FE-FERRATA-LEAFLET-001: draws nothing for no ferratas', () => {
|
||||
const { container } = render(<FerrataLayerLeaflet ferratas={[]} onSelect={vi.fn()} />);
|
||||
const { container } = render(<FerrataLayerLeaflet groups={[]} onSelect={vi.fn()} />);
|
||||
expect(container).toBeEmptyDOMElement();
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-002: casing, dashed line, then the invisible hit line, in [lat, lng]', () => {
|
||||
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 3)]} onSelect={vi.fn()} />);
|
||||
const lines = screen.getAllByTestId('polyline');
|
||||
it('FE-FERRATA-LEAFLET-002: a light casing, the dark dashed line, then the invisible hit line, in [lat, lng]', () => {
|
||||
const root = document.documentElement;
|
||||
root.style.setProperty('--text-primary', 'black');
|
||||
root.style.setProperty('--bg-primary', 'white');
|
||||
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
|
||||
|
||||
expect(lines.map((line) => line.dataset.weight)).toEqual(['6', '3', '16']);
|
||||
expect(lines.map((line) => line.dataset.interactive)).toEqual(['false', 'false', 'true']);
|
||||
expect(lines[1]?.dataset.dash).toBe('6 5');
|
||||
expect(lines[2]?.dataset.opacity).toBe('0');
|
||||
expect(JSON.parse(lines[0]?.dataset.positions ?? '[]')).toEqual([
|
||||
expect(lines().map((line) => line.dataset.weight)).toEqual(['6', '2.5', '16']);
|
||||
expect(lines().map((line) => line.dataset.color)).toEqual(['white', 'black', 'white']);
|
||||
expect(lines().map((line) => line.dataset.interactive)).toEqual(['false', 'false', 'true']);
|
||||
expect(lines()[1]?.dataset.dash).toBe('6 5');
|
||||
expect(lines()[2]?.dataset.opacity).toBe('0');
|
||||
expect(JSON.parse(lines()[0]?.dataset.positions ?? '[]')).toEqual([
|
||||
[46.5, 11.7],
|
||||
[46.51, 11.71],
|
||||
]);
|
||||
root.style.removeProperty('--text-primary');
|
||||
root.style.removeProperty('--bg-primary');
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-003: the tooltip names the ferrata and its grade, or falls back to the route and unnamed', () => {
|
||||
render(
|
||||
<FerrataLayerLeaflet
|
||||
ferratas={[
|
||||
ferrata('way/1', 'Pisciadù', 4),
|
||||
ferrata('way/2', null, null, 'Sentiero attrezzato'),
|
||||
ferrata('way/3', null, null),
|
||||
]}
|
||||
onSelect={vi.fn()}
|
||||
/>
|
||||
);
|
||||
it('FE-FERRATA-LEAFLET-003: the tooltip names the ferrata and its grade range, or falls back to the route and unnamed', () => {
|
||||
const groups = groupFerratas([
|
||||
ferrata('way/1', 'Pisciadù', 2),
|
||||
ferrata('way/2', 'Pisciadù', 4, { lat: 46.51, plus: '+' }),
|
||||
ferrata('way/3', null, 3, { routeName: 'Sentiero attrezzato', lat: 47 }),
|
||||
ferrata('way/4', null, null, { lat: 48 }),
|
||||
]);
|
||||
render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
|
||||
expect(screen.getAllByTestId('tooltip').map((tip) => tip.textContent)).toEqual([
|
||||
'PisciadùK4+',
|
||||
'Sentiero attrezzato',
|
||||
'Pisciadùferratas.gradeRange(K2,K4+)',
|
||||
'Pisciadùferratas.gradeRange(K2,K4+)',
|
||||
'Sentiero attrezzatoK3',
|
||||
'ferratas.unnamed',
|
||||
]);
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-004: a click on the hit line hands the ferrata and the point to onSelect', () => {
|
||||
it('FE-FERRATA-LEAFLET-004: a click on the hit line hands the ferrata, the way and the point to onSelect', () => {
|
||||
const onSelect = vi.fn();
|
||||
const target = ferrata('way/9', 'Tridentina', 2);
|
||||
render(<FerrataLayerLeaflet ferratas={[target]} onSelect={onSelect} />);
|
||||
const groups = groupFerratas([target]);
|
||||
render(<FerrataLayerLeaflet groups={groups} onSelect={onSelect} />);
|
||||
|
||||
const [, , hit] = screen.getAllByTestId('polyline');
|
||||
const hit = lines()[2];
|
||||
if (!hit) throw new Error('no hit line');
|
||||
fireEvent.click(hit);
|
||||
expect(onSelect).toHaveBeenCalledWith(target, { lng: 11.705, lat: 46.505 });
|
||||
expect(onSelect).toHaveBeenCalledWith(groups[0], target, { lng: 11.705, lat: 46.505 });
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-006: the hit line keeps its click from reaching the map, so no control point lands under it', () => {
|
||||
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 3)]} onSelect={vi.fn()} />);
|
||||
const [, , hit] = screen.getAllByTestId('polyline');
|
||||
expect(hit?.dataset.bubbles).toBe('false');
|
||||
it('FE-FERRATA-LEAFLET-005: the hit line keeps its click from reaching the map, so no control point lands under it', () => {
|
||||
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
|
||||
expect(lines()[2]?.dataset.bubbles).toBe('false');
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-007: the grade sits beside the name as its own label, without a separator', () => {
|
||||
render(<FerrataLayerLeaflet ferratas={[ferrata('way/1', 'Pisciadù', 4)]} onSelect={vi.fn()} />);
|
||||
it('FE-FERRATA-LEAFLET-006: the grade sits beside the name as its own label, without a separator', () => {
|
||||
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 4)])} onSelect={vi.fn()} />);
|
||||
const tip = screen.getByTestId('tooltip');
|
||||
expect(tip.textContent).not.toContain('·');
|
||||
expect(tip.querySelector('span')?.textContent).toBe('K4+');
|
||||
expect(tip.querySelector('span')?.textContent).toBe('K4');
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-005: a theme change recolours the lines without a refetch', async () => {
|
||||
it('FE-FERRATA-LEAFLET-007: hovering a segment lights up the whole ferrata in the accent, and so does the open card', () => {
|
||||
const root = document.documentElement;
|
||||
root.style.setProperty('--warning', 'orange');
|
||||
const list = [ferrata('way/1', 'Pisciadù', 3)];
|
||||
render(<FerrataLayerLeaflet ferratas={list} onSelect={vi.fn()} />);
|
||||
expect(screen.getAllByTestId('polyline')[1]?.dataset.color).toBe('orange');
|
||||
root.style.setProperty('--accent', 'navy');
|
||||
const groups = groupFerratas([
|
||||
ferrata('way/1', 'Via A', 2),
|
||||
ferrata('way/2', 'Via A', 3, { lat: 46.51 }),
|
||||
ferrata('way/3', 'Via B', 3, { lat: 47 }),
|
||||
]);
|
||||
const { rerender } = render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
|
||||
// Casings 0 to 2, lines 3 to 5, hit lines 6 to 8.
|
||||
const lineColours = () =>
|
||||
lines()
|
||||
.slice(3, 6)
|
||||
.map((line) => line.dataset.color);
|
||||
expect(lineColours()).not.toContain('navy');
|
||||
|
||||
const hit = lines()[7];
|
||||
if (!hit) throw new Error('no hit line');
|
||||
fireEvent.mouseOver(hit);
|
||||
expect(lineColours().map((colour) => colour === 'navy')).toEqual([true, true, false]);
|
||||
fireEvent.mouseOut(hit);
|
||||
expect(lineColours()).not.toContain('navy');
|
||||
|
||||
rerender(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} selectedId={groups[1]?.id ?? null} />);
|
||||
expect(lineColours().map((colour) => colour === 'navy')).toEqual([false, false, true]);
|
||||
root.style.removeProperty('--accent');
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-008: shows a route icon halfway along each ferrata only once zoomed in, and it opens the card', () => {
|
||||
const onSelect = vi.fn();
|
||||
const groups = groupFerratas([ferrata('way/1', 'Via A', 2), ferrata('way/2', 'Via B', 3, { lat: 47 })]);
|
||||
render(<FerrataLayerLeaflet groups={groups} onSelect={onSelect} />);
|
||||
expect(screen.queryAllByTestId('ferrata-icon')).toHaveLength(0);
|
||||
|
||||
zoom.value = 14;
|
||||
act(() => zoomEnd.handler?.());
|
||||
const icons = screen.getAllByTestId('ferrata-icon');
|
||||
expect(icons).toHaveLength(2);
|
||||
const [lat, lng] = JSON.parse(icons[0]?.dataset.position ?? '[]') as [number, number];
|
||||
expect(lat).toBeCloseTo(46.505, 4);
|
||||
expect(lng).toBeCloseTo(11.705, 4);
|
||||
|
||||
fireEvent.click(icons[1] as HTMLElement);
|
||||
expect(onSelect).toHaveBeenCalledWith(groups[1], groups[1]?.segments[0], { lng: 11.705, lat: 46.505 });
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-009: a theme change recolours the lines without a refetch', async () => {
|
||||
const root = document.documentElement;
|
||||
root.style.setProperty('--text-primary', 'black');
|
||||
render(<FerrataLayerLeaflet groups={groupFerratas([ferrata('way/1', 'Pisciadù', 3)])} onSelect={vi.fn()} />);
|
||||
expect(lines()[1]?.dataset.color).toBe('black');
|
||||
|
||||
await act(async () => {
|
||||
root.style.setProperty('--warning', 'gold');
|
||||
root.style.setProperty('--text-primary', 'ivory');
|
||||
await Promise.resolve();
|
||||
});
|
||||
|
||||
await waitFor(() => expect(screen.getAllByTestId('polyline')[1]?.dataset.color).toBe('gold'));
|
||||
root.style.removeProperty('--warning');
|
||||
await waitFor(() => expect(lines()[1]?.dataset.color).toBe('ivory'));
|
||||
root.style.removeProperty('--text-primary');
|
||||
});
|
||||
it('FE-FERRATA-LEAFLET-010: a lit ferrata is drawn solid and wider on a wider casing, the hit line and tooltip carry their classes', () => {
|
||||
const groups = groupFerratas([ferrata('way/1', 'Via A', 2)]);
|
||||
render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} selectedId={groups[0]?.id ?? null} />);
|
||||
expect(lines().map((line) => line.dataset.weight)).toEqual(['9', '4', '16']);
|
||||
expect(lines()[1]?.dataset.dash).toBe('');
|
||||
expect(lines()[2]?.dataset.class).toBe('trek-ferrata-hit');
|
||||
expect(screen.getByTestId('tooltip').dataset.class).toBe('map-tooltip');
|
||||
});
|
||||
|
||||
it('FE-FERRATA-LEAFLET-011: of two icons that would touch only the longer ferrata keeps its own', () => {
|
||||
const short = ferrata('way/1', 'Via A', 2);
|
||||
const long: Ferrata = {
|
||||
...ferrata('way/2', 'Via B', 3, { lat: 46.5001 }),
|
||||
geometry: [
|
||||
[46.4951, 11.695],
|
||||
[46.5151, 11.715],
|
||||
],
|
||||
};
|
||||
const groups = groupFerratas([short, long]);
|
||||
zoom.value = 14;
|
||||
render(<FerrataLayerLeaflet groups={groups} onSelect={vi.fn()} />);
|
||||
const icons = screen.getAllByTestId('ferrata-icon');
|
||||
expect(icons).toHaveLength(1);
|
||||
const [lat] = JSON.parse(icons[0]?.dataset.position ?? '[]') as [number, number];
|
||||
expect(lat).toBeCloseTo(46.5051, 4);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user