|
| 1 | +// @vitest-environment jsdom |
| 2 | +import "../../__tests__/setup"; |
| 3 | +import { describe, it, expect, vi } from "vitest"; |
| 4 | +import { destinationPoint, sectorPoints, createFovCone } from "../WindowFovCone"; |
| 5 | +import L from "leaflet"; |
| 6 | + |
| 7 | +describe("destinationPoint", () => { |
| 8 | + it("bearing 0 (north) increases latitude", () => { |
| 9 | + const start = L.latLng(45, -75); |
| 10 | + const result = destinationPoint(start, 1000, 0); |
| 11 | + expect(result.lat).toBeGreaterThan(45); |
| 12 | + expect(result.lng).toBeCloseTo(-75, 4); |
| 13 | + }); |
| 14 | + |
| 15 | + it("bearing 180 (south) decreases latitude", () => { |
| 16 | + const start = L.latLng(45, -75); |
| 17 | + const result = destinationPoint(start, 1000, 180); |
| 18 | + expect(result.lat).toBeLessThan(45); |
| 19 | + expect(result.lng).toBeCloseTo(-75, 4); |
| 20 | + }); |
| 21 | + |
| 22 | + it("bearing 90 (east) increases longitude", () => { |
| 23 | + const start = L.latLng(45, -75); |
| 24 | + const result = destinationPoint(start, 1000, 90); |
| 25 | + expect(result.lat).toBeCloseTo(45, 4); |
| 26 | + expect(result.lng).toBeGreaterThan(-75); |
| 27 | + }); |
| 28 | + |
| 29 | + it("bearing 270 (west) decreases longitude", () => { |
| 30 | + const start = L.latLng(45, -75); |
| 31 | + const result = destinationPoint(start, 1000, 270); |
| 32 | + expect(result.lat).toBeCloseTo(45, 4); |
| 33 | + expect(result.lng).toBeLessThan(-75); |
| 34 | + }); |
| 35 | + |
| 36 | + it("distance 0 returns the same point", () => { |
| 37 | + const start = L.latLng(45, -75); |
| 38 | + const result = destinationPoint(start, 0, 42); |
| 39 | + expect(result.lat).toBeCloseTo(45, 10); |
| 40 | + expect(result.lng).toBeCloseTo(-75, 10); |
| 41 | + }); |
| 42 | + |
| 43 | + it("1000m displacement is approximately correct", () => { |
| 44 | + const start = L.latLng(0, 0); |
| 45 | + const result = destinationPoint(start, 1000, 0); |
| 46 | + // 1000m north at the equator is ~0.009 degrees latitude |
| 47 | + const expectedDeg = (1000 / 6371000) * (180 / Math.PI); |
| 48 | + expect(result.lat).toBeCloseTo(expectedDeg, 4); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("sectorPoints", () => { |
| 53 | + it("returns array starting and ending with center", () => { |
| 54 | + const center = L.latLng(45, -75); |
| 55 | + const points = sectorPoints(center, 500, 0, 90, 10); |
| 56 | + expect(points[0]).toBe(center); |
| 57 | + expect(points[points.length - 1]).toBe(center); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns segments + 3 points (center + segments+1 arc + center)", () => { |
| 61 | + const center = L.latLng(45, -75); |
| 62 | + const segments = 10; |
| 63 | + const points = sectorPoints(center, 500, 0, 90, segments); |
| 64 | + // center + (segments + 1) arc points + center = segments + 3 |
| 65 | + expect(points).toHaveLength(segments + 3); |
| 66 | + }); |
| 67 | + |
| 68 | + it("arc points are all different from center", () => { |
| 69 | + const center = L.latLng(45, -75); |
| 70 | + const points = sectorPoints(center, 500, 0, 90, 4); |
| 71 | + // Points 1 through length-2 are arc points (not center) |
| 72 | + for (let i = 1; i < points.length - 1; i++) { |
| 73 | + expect(points[i]).not.toBe(center); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it("single segment produces 4 points", () => { |
| 78 | + const center = L.latLng(45, -75); |
| 79 | + const points = sectorPoints(center, 500, 0, 90, 1); |
| 80 | + // center + 2 arc endpoints + center |
| 81 | + expect(points).toHaveLength(4); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("createFovCone", () => { |
| 86 | + it("returns an object with update and remove methods", () => { |
| 87 | + const map = L.map(document.createElement("div")); |
| 88 | + const handle = createFovCone(map, [45, -75], 0, 90, vi.fn()); |
| 89 | + expect(typeof handle.update).toBe("function"); |
| 90 | + expect(typeof handle.remove).toBe("function"); |
| 91 | + }); |
| 92 | + |
| 93 | + it("remove() calls map.removeLayer twice (polygon + handle)", () => { |
| 94 | + const map = L.map(document.createElement("div")); |
| 95 | + const handle = createFovCone(map, [45, -75], 0, 90, vi.fn()); |
| 96 | + handle.remove(); |
| 97 | + expect(map.removeLayer).toHaveBeenCalledTimes(2); |
| 98 | + }); |
| 99 | + |
| 100 | + it("remove() calls map.off to clean up event listeners", () => { |
| 101 | + const map = L.map(document.createElement("div")); |
| 102 | + const handle = createFovCone(map, [45, -75], 0, 90, vi.fn()); |
| 103 | + handle.remove(); |
| 104 | + expect(map.off).toHaveBeenCalledWith("mousemove", expect.any(Function)); |
| 105 | + expect(map.off).toHaveBeenCalledWith("mouseup", expect.any(Function)); |
| 106 | + }); |
| 107 | + |
| 108 | + it("creates polygon and circleMarker via Leaflet", () => { |
| 109 | + const map = L.map(document.createElement("div")); |
| 110 | + createFovCone(map, [45, -75], 0, 90, vi.fn()); |
| 111 | + expect(L.polygon).toHaveBeenCalled(); |
| 112 | + expect(L.circleMarker).toHaveBeenCalled(); |
| 113 | + }); |
| 114 | +}); |
0 commit comments