Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/ui/control/geolocate_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,13 @@ class GeolocateControl extends Evented<GeolocateControlEvents> implements IContr
this._userLocationDotMarker.addClassName('mapboxgl-user-location-dot-stale');
}

this.fire(new Event('error', error));
this.fire(new Event('error', {
code: error.code,
message: error.message,
PERMISSION_DENIED: error.PERMISSION_DENIED,
POSITION_UNAVAILABLE: error.POSITION_UNAVAILABLE,
TIMEOUT: error.TIMEOUT
} as GeolocationPositionError));

this._finish();
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/ui/control/geolocate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,36 @@ test('GeolocateControl error event', async () => {
});
});

test('GeolocateControl error event includes GeolocationPositionError constants', async () => {
const map = createMap();
const geolocate = new GeolocateControl();
map.addControl(geolocate);

// Directly call _onError with a mock GeolocationPositionError to test
// that PERMISSION_DENIED, POSITION_UNAVAILABLE, and TIMEOUT are passed through.
// The mock-geolocation library doesn't support these constants.
const mockError = {
PERMISSION_DENIED: 1,
POSITION_UNAVAILABLE: 2,
TIMEOUT: 3,
code: 1,
message: 'User denied Geolocation',
} as const satisfies GeolocationPositionError;

await afterUIChanges((resolve) => {
geolocate.on('error', (error) => {
expect(error.code).toEqual(1);
expect(error.message).toEqual('User denied Geolocation');
expect(error.PERMISSION_DENIED).toEqual(1);
expect(error.POSITION_UNAVAILABLE).toEqual(2);
expect(error.TIMEOUT).toEqual(3);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
resolve();
});
geolocate._onError(mockError);
});
});

test('GeolocateControl outofmaxbounds event in active lock state', async () => {
const map = createMap();
const geolocate = new GeolocateControl();
Expand Down