Skip to content

Commit cb44ad0

Browse files
committed
Enhance GeolocateControl error event to include detailed GeolocationPositionError properties
- Updated the error event to fire with a structured object containing code, message, and constants for PERMISSION_DENIED, POSITION_UNAVAILABLE, and TIMEOUT. - Added a unit test to verify that these properties are correctly passed through when an error occurs. GitOrigin-RevId: [your-rev-id]
1 parent c2227bc commit cb44ad0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/ui/control/geolocate_control.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,13 @@ class GeolocateControl extends Evented<GeolocateControlEvents> implements IContr
428428
this._userLocationDotMarker.addClassName('mapboxgl-user-location-dot-stale');
429429
}
430430

431-
this.fire(new Event('error', error));
431+
this.fire(new Event('error', {
432+
code: error.code,
433+
message: error.message,
434+
PERMISSION_DENIED: error.PERMISSION_DENIED,
435+
POSITION_UNAVAILABLE: error.POSITION_UNAVAILABLE,
436+
TIMEOUT: error.TIMEOUT
437+
} as GeolocationPositionError));
432438

433439
this._finish();
434440
}

test/unit/ui/control/geolocate.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,36 @@ test('GeolocateControl error event', async () => {
5959
});
6060
});
6161

62+
test('GeolocateControl error event includes GeolocationPositionError constants', async () => {
63+
const map = createMap();
64+
const geolocate = new GeolocateControl();
65+
map.addControl(geolocate);
66+
67+
// Directly call _onError with a mock GeolocationPositionError to test
68+
// that PERMISSION_DENIED, POSITION_UNAVAILABLE, and TIMEOUT are passed through.
69+
// The mock-geolocation library doesn't support these constants.
70+
const mockError = {
71+
PERMISSION_DENIED: 1,
72+
POSITION_UNAVAILABLE: 2,
73+
TIMEOUT: 3,
74+
code: 1,
75+
message: 'User denied Geolocation',
76+
} as const satisfies GeolocationPositionError;
77+
78+
await afterUIChanges((resolve) => {
79+
geolocate.on('error', (error) => {
80+
expect(error.code).toEqual(1);
81+
expect(error.message).toEqual('User denied Geolocation');
82+
expect(error.PERMISSION_DENIED).toEqual(1);
83+
expect(error.POSITION_UNAVAILABLE).toEqual(2);
84+
expect(error.TIMEOUT).toEqual(3);
85+
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
86+
resolve();
87+
});
88+
geolocate._onError(mockError);
89+
});
90+
});
91+
6292
test('GeolocateControl outofmaxbounds event in active lock state', async () => {
6393
const map = createMap();
6494
const geolocate = new GeolocateControl();

0 commit comments

Comments
 (0)