|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 5 | +import { render, screen, act } from '@testing-library/react'; |
| 6 | +import type { MeasurePoint } from '../utils/measureDistance'; |
| 7 | + |
| 8 | +// Capture the click handler registered via useMapEvents so the test can fire |
| 9 | +// synthetic map clicks. Stub the leaflet primitives to plain DOM so we can |
| 10 | +// assert what the controller renders without a real map. |
| 11 | +let clickHandler: ((e: { latlng: { lat: number; lng: number } }) => void) | null = null; |
| 12 | +const containerStyle: { cursor: string } = { cursor: '' }; |
| 13 | + |
| 14 | +vi.mock('react-leaflet', () => ({ |
| 15 | + useMap: () => ({ getContainer: () => ({ style: containerStyle }) }), |
| 16 | + useMapEvents: (handlers: { click: (e: { latlng: { lat: number; lng: number } }) => void }) => { |
| 17 | + clickHandler = handlers.click; |
| 18 | + return {}; |
| 19 | + }, |
| 20 | + CircleMarker: ({ center }: { center: [number, number] }) => ( |
| 21 | + <div data-testid="measure-ring" data-lat={center[0]} data-lng={center[1]} /> |
| 22 | + ), |
| 23 | + Polyline: ({ positions, children }: { positions: [number, number][]; children?: React.ReactNode }) => ( |
| 24 | + <div data-testid="measure-line" data-positions={JSON.stringify(positions)}>{children}</div> |
| 25 | + ), |
| 26 | + Tooltip: ({ children }: { children?: React.ReactNode }) => ( |
| 27 | + <div data-testid="measure-label">{children}</div> |
| 28 | + ), |
| 29 | +})); |
| 30 | + |
| 31 | +let unit: 'km' | 'mi' = 'km'; |
| 32 | +vi.mock('../contexts/SettingsContext', () => ({ |
| 33 | + useSettings: () => ({ distanceUnit: unit }), |
| 34 | +})); |
| 35 | + |
| 36 | +import MeasureDistanceController from './MeasureDistanceController'; |
| 37 | + |
| 38 | +const POINTS: MeasurePoint[] = [ |
| 39 | + { id: 'a', lat: 40.0, lng: -105.0, label: 'A' }, |
| 40 | + { id: 'b', lat: 40.5, lng: -105.0, label: 'B' }, |
| 41 | + { id: 'c', lat: 41.0, lng: -105.0, label: 'C' }, |
| 42 | +]; |
| 43 | + |
| 44 | +function click(lat: number, lng: number) { |
| 45 | + act(() => clickHandler?.({ latlng: { lat, lng } })); |
| 46 | +} |
| 47 | + |
| 48 | +describe('MeasureDistanceController', () => { |
| 49 | + beforeEach(() => { |
| 50 | + clickHandler = null; |
| 51 | + containerStyle.cursor = ''; |
| 52 | + unit = 'km'; |
| 53 | + }); |
| 54 | + |
| 55 | + it('renders nothing when inactive', () => { |
| 56 | + render(<MeasureDistanceController active={false} points={POINTS} />); |
| 57 | + expect(screen.queryByTestId('measure-ring')).toBeNull(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders nothing with fewer than two points', () => { |
| 61 | + render(<MeasureDistanceController active points={[POINTS[0]]} />); |
| 62 | + expect(screen.queryByTestId('measure-ring')).toBeNull(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('snaps two clicks to nearest nodes and draws a labeled line', () => { |
| 66 | + render(<MeasureDistanceController active points={POINTS} />); |
| 67 | + // Click near A, then near C -> line A..C, ~111 km. |
| 68 | + click(39.9, -105.0); |
| 69 | + click(41.1, -105.0); |
| 70 | + |
| 71 | + const line = screen.getByTestId('measure-line'); |
| 72 | + expect(JSON.parse(line.getAttribute('data-positions')!)).toEqual([ |
| 73 | + [40.0, -105.0], |
| 74 | + [41.0, -105.0], |
| 75 | + ]); |
| 76 | + const label = screen.getByTestId('measure-label').textContent ?? ''; |
| 77 | + expect(label).toMatch(/km$/); |
| 78 | + expect(parseFloat(label)).toBeCloseTo(111.2, 0); |
| 79 | + }); |
| 80 | + |
| 81 | + it('honors the miles preference', () => { |
| 82 | + unit = 'mi'; |
| 83 | + render(<MeasureDistanceController active points={POINTS} />); |
| 84 | + click(39.9, -105.0); |
| 85 | + click(40.6, -105.0); // nearest to B |
| 86 | + expect(screen.getByTestId('measure-label').textContent).toMatch(/mi$/); |
| 87 | + }); |
| 88 | + |
| 89 | + it('a third click restarts the measurement from a new anchor', () => { |
| 90 | + render(<MeasureDistanceController active points={POINTS} />); |
| 91 | + click(39.9, -105.0); // A |
| 92 | + click(41.1, -105.0); // C -> completed pair |
| 93 | + expect(screen.queryByTestId('measure-line')).not.toBeNull(); |
| 94 | + |
| 95 | + click(40.6, -105.0); // restart with B as the new anchor A |
| 96 | + expect(screen.queryByTestId('measure-line')).toBeNull(); |
| 97 | + // one ring for the new anchor |
| 98 | + expect(screen.getAllByTestId('measure-ring')).toHaveLength(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it('sets a crosshair cursor while active and restores it on exit', () => { |
| 102 | + const { rerender } = render(<MeasureDistanceController active points={POINTS} />); |
| 103 | + expect(containerStyle.cursor).toBe('crosshair'); |
| 104 | + rerender(<MeasureDistanceController active={false} points={POINTS} />); |
| 105 | + expect(containerStyle.cursor).toBe(''); |
| 106 | + }); |
| 107 | + |
| 108 | + it('Escape clears the measurement and calls onExit', () => { |
| 109 | + const onExit = vi.fn(); |
| 110 | + render(<MeasureDistanceController active points={POINTS} onExit={onExit} />); |
| 111 | + click(39.9, -105.0); |
| 112 | + click(41.1, -105.0); |
| 113 | + expect(screen.queryByTestId('measure-line')).not.toBeNull(); |
| 114 | + |
| 115 | + act(() => { |
| 116 | + window.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape' })); |
| 117 | + }); |
| 118 | + expect(onExit).toHaveBeenCalledTimes(1); |
| 119 | + expect(screen.queryByTestId('measure-line')).toBeNull(); |
| 120 | + }); |
| 121 | +}); |
0 commit comments