forked from googlemaps/fleet-debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolylineUtils.test.js
More file actions
111 lines (95 loc) · 3.71 KB
/
PolylineUtils.test.js
File metadata and controls
111 lines (95 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { parsePolylineInput, calculatePolylineDistanceMeters } from "./PolylineUtils";
import { formatDistance } from "./Utils";
describe("PolylineUtils", () => {
const EXPECTED_POINTS = [
{ latitude: 37.42213, longitude: -122.0848 },
{ latitude: 37.4152, longitude: -122.0627 },
{ latitude: 37.427, longitude: -122.0854 },
];
test("decodes a GMP Encoded Polyline", () => {
const encoded = "i_lcF~tchVhj@ciCwhAzlC";
const decoded = parsePolylineInput(encoded);
expect(decoded).toHaveLength(EXPECTED_POINTS.length);
for (let i = 0; i < EXPECTED_POINTS.length; i++) {
expect(decoded[i].latitude).toBeCloseTo(EXPECTED_POINTS[i].latitude, 5);
expect(decoded[i].longitude).toBeCloseTo(EXPECTED_POINTS[i].longitude, 5);
}
});
test("decodes an S2 Polyline", () => {
const s2String =
"AQMAAAA7_tIhjf_avysne_M5iOW__WHh1yJy4z9MIcUK8Pvav7QbiLMRiuW_SWY5Y1lx4z-CIIuaN__av1Eb3-fUh-W_iPpHZ7By4z8=";
const decoded = parsePolylineInput(s2String);
expect(decoded).toHaveLength(EXPECTED_POINTS.length);
for (let i = 0; i < EXPECTED_POINTS.length; i++) {
expect(decoded[i].latitude).toBeCloseTo(EXPECTED_POINTS[i].latitude, 5);
expect(decoded[i].longitude).toBeCloseTo(EXPECTED_POINTS[i].longitude, 5);
}
});
test("decodes a GMP Unencoded Polyline (Plain Text)", () => {
const input =
"{ latitude: 37.42213, longitude: -122.0848 }, { latitude: 37.4152, longitude: -122.0627 }, { latitude: 37.427, longitude: -122.0854 }";
const decoded = parsePolylineInput(input);
expect(decoded).toHaveLength(EXPECTED_POINTS.length);
for (let i = 0; i < EXPECTED_POINTS.length; i++) {
expect(decoded[i].latitude).toBe(EXPECTED_POINTS[i].latitude);
expect(decoded[i].longitude).toBe(EXPECTED_POINTS[i].longitude);
}
});
test("decodes a JSON Polyline", () => {
const input = JSON.stringify(EXPECTED_POINTS);
const decoded = parsePolylineInput(input);
expect(decoded).toHaveLength(EXPECTED_POINTS.length);
for (let i = 0; i < EXPECTED_POINTS.length; i++) {
expect(decoded[i].latitude).toBe(EXPECTED_POINTS[i].latitude);
expect(decoded[i].longitude).toBe(EXPECTED_POINTS[i].longitude);
}
});
test("throws error on invalid input", () => {
expect(() => parsePolylineInput("not a polyline")).toThrow();
});
});
describe("calculatePolylineDistanceMeters", () => {
test("returns 0 for empty or single point polylines", () => {
expect(calculatePolylineDistanceMeters([])).toBe(0);
expect(calculatePolylineDistanceMeters([{ latitude: 0, longitude: 0 }])).toBe(0);
});
test("calculates distance correctly", () => {
// Mock window.google.maps
global.window.google = {
maps: {
LatLng: class {
constructor(lat, lng) {
this.lat = lat;
this.lng = lng;
}
},
geometry: {
spherical: {
computeDistanceBetween: jest.fn().mockImplementation(() => 100), // Mock 100m for any two points
},
},
},
};
const points = [
{ latitude: 0, longitude: 0 },
{ latitude: 1, longitude: 1 },
{ latitude: 2, longitude: 2 },
];
// 2 segments, 100m each = 200m
expect(calculatePolylineDistanceMeters(points)).toBe(200);
// Cleanup
delete global.window.google;
});
});
describe("formatDistance", () => {
test("formats under 1000 meters correctly", () => {
const { metric, imperial } = formatDistance(500);
expect(metric).toBe("500 m");
expect(imperial).toBe("1640 ft");
});
test("formats over 1000 meters into km and miles", () => {
const { metric, imperial } = formatDistance(2500);
expect(metric).toBe("2.50 km");
expect(imperial).toBe("1.55 mi");
});
});