Skip to content

Commit 916ed30

Browse files
fix: correct coordinate validation order (#289)
1 parent 30a35b1 commit 916ed30

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

src/components/directions/directions.spec.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,89 @@ describe('DirectionsControl', () => {
285285
expect(result.wps).toBe('13.4,52.5,10,48');
286286
});
287287
});
288+
289+
describe('DirectionsControl URL parsing', () => {
290+
beforeEach(() => {
291+
vi.clearAllMocks();
292+
mockResults.data = null;
293+
mockWaypoints.length = 0;
294+
mockWaypoints.push(
295+
{ id: '0', geocodeResults: [], userInput: '' },
296+
{ id: '1', geocodeResults: [], userInput: '' }
297+
);
298+
});
299+
300+
it('should process URL params with valid coordinates (Berlin)', async () => {
301+
const parseUrlParams = await import('@/utils/parse-url-params');
302+
vi.mocked(parseUrlParams.parseUrlParams).mockReturnValue({
303+
wps: '13.365016850476763,52.483706198952575,13.422421655040836,52.49336042169804',
304+
});
305+
306+
render(<DirectionsControl />);
307+
308+
expect(mockReverseGeocode).toHaveBeenCalledTimes(2);
309+
expect(mockReverseGeocode).toHaveBeenCalledWith(
310+
13.365016850476763,
311+
52.483706198952575,
312+
0,
313+
{ isPermalink: true }
314+
);
315+
expect(mockReverseGeocode).toHaveBeenCalledWith(
316+
13.422421655040836,
317+
52.49336042169804,
318+
1,
319+
{ isPermalink: true }
320+
);
321+
});
322+
323+
it('should process URL params with valid coordinates where lng > 90 (Singapore)', async () => {
324+
const parseUrlParams = await import('@/utils/parse-url-params');
325+
vi.mocked(parseUrlParams.parseUrlParams).mockReturnValue({
326+
wps: '103.66492937866911,1.4827280571964963,103.66421854954496,1.4840285187178779',
327+
});
328+
329+
render(<DirectionsControl />);
330+
331+
expect(mockReverseGeocode).toHaveBeenCalledTimes(2);
332+
expect(mockReverseGeocode).toHaveBeenCalledWith(
333+
103.66492937866911,
334+
1.4827280571964963,
335+
0,
336+
{ isPermalink: true }
337+
);
338+
expect(mockReverseGeocode).toHaveBeenCalledWith(
339+
103.66421854954496,
340+
1.4840285187178779,
341+
1,
342+
{ isPermalink: true }
343+
);
344+
});
345+
346+
it('should skip truly invalid coordinates from URL', async () => {
347+
const parseUrlParams = await import('@/utils/parse-url-params');
348+
vi.mocked(parseUrlParams.parseUrlParams).mockReturnValue({
349+
wps: '999,999',
350+
});
351+
352+
render(<DirectionsControl />);
353+
354+
expect(mockReverseGeocode).not.toHaveBeenCalled();
355+
});
356+
357+
it('should handle coordinates near edge of valid range', async () => {
358+
const parseUrlParams = await import('@/utils/parse-url-params');
359+
vi.mocked(parseUrlParams.parseUrlParams).mockReturnValue({
360+
wps: '179.9,89,-179.9,-89',
361+
});
362+
363+
render(<DirectionsControl />);
364+
365+
expect(mockReverseGeocode).toHaveBeenCalledTimes(2);
366+
expect(mockReverseGeocode).toHaveBeenCalledWith(179.9, 89, 0, {
367+
isPermalink: true,
368+
});
369+
expect(mockReverseGeocode).toHaveBeenCalledWith(-179.9, -89, 1, {
370+
isPermalink: true,
371+
});
372+
});
373+
});

src/components/directions/directions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const DirectionsControl = () => {
5151
const lng = coordinates[i]!;
5252
const lat = coordinates[i + 1]!;
5353

54-
if (!isValidCoordinates(lng, lat) || isNaN(lng) || isNaN(lat)) continue;
54+
if (!isValidCoordinates(lat, lng) || isNaN(lng) || isNaN(lat)) continue;
5555

5656
const index = i / 2;
5757
reverseGeocode(lng, lat, index, { isPermalink: true });

src/components/isochrones/isochrones.spec.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,22 @@ describe('IsochronesControl URL parsing', () => {
219219

220220
expect(mockReverseGeocode).not.toHaveBeenCalled();
221221
});
222+
223+
it('should process URL params with valid coordinates where lng > 90 (Singapore)', async () => {
224+
const parseUrlParams = await import('@/utils/parse-url-params');
225+
vi.mocked(parseUrlParams.parseUrlParams).mockReturnValue({
226+
wps: '103.66492937866911,1.4827280571964963',
227+
});
228+
229+
render(<IsochronesControl />);
230+
231+
expect(mockReverseGeocode).toHaveBeenCalledWith(
232+
103.66492937866911,
233+
1.4827280571964963
234+
);
235+
expect(mockFlyTo).toHaveBeenCalledWith({
236+
center: [103.66492937866911, 1.4827280571964963],
237+
zoom: 12,
238+
});
239+
});
222240
});

src/components/isochrones/isochrones.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const IsochronesControl = () => {
3535
const lng = coordinates[i]!;
3636
const lat = coordinates[i + 1]!;
3737

38-
if (!isValidCoordinates(lng, lat) || isNaN(lng) || isNaN(lat)) continue;
38+
if (!isValidCoordinates(lat, lng) || isNaN(lng) || isNaN(lat)) continue;
3939

4040
reverseGeocode(lng, lat).then(() => {
4141
refetchIsochrones();

0 commit comments

Comments
 (0)