Skip to content

Commit cf5ac98

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 a39a141 commit cf5ac98

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
@@ -412,7 +412,13 @@ class GeolocateControl extends Evented<GeolocateControlEvents> implements IContr
412412
this._userLocationDotMarker.addClassName('mapboxgl-user-location-dot-stale');
413413
}
414414

415-
this.fire(new Event('error', error));
415+
this.fire(new Event('error', {
416+
code: error.code,
417+
message: error.message,
418+
PERMISSION_DENIED: error.PERMISSION_DENIED,
419+
POSITION_UNAVAILABLE: error.POSITION_UNAVAILABLE,
420+
TIMEOUT: error.TIMEOUT
421+
} as GeolocationPositionError));
416422

417423
this._finish();
418424
}

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

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

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

0 commit comments

Comments
 (0)