geo.getCurrentPosition and some testing helpers (#8150)

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry 2022-03-25 12:30:50 +01:00 committed by GitHub
parent f229ad6407
commit 0d513b3a2d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 99 additions and 4 deletions

View file

@ -23,6 +23,7 @@ import {
mapGeolocationPositionToTimedGeo,
watchPosition,
} from "../../../src/utils/beacon";
import { getCurrentPosition } from "../../../src/utils/beacon/geolocation";
import { makeGeolocationPosition, mockGeolocation } from "../../test-utils/beacon";
describe('geolocation utilities', () => {
@ -166,7 +167,7 @@ describe('geolocation utilities', () => {
const [, , options] = geolocation.watchPosition.mock.calls[0];
expect(options).toEqual({
maximumAge: 1000,
maximumAge: 2000,
timeout: 5000,
});
});
@ -204,4 +205,36 @@ describe('geolocation utilities', () => {
expect(errorHandler).toHaveBeenCalledWith(GeolocationError.PermissionDenied);
});
});
describe('getCurrentPosition()', () => {
it('throws with unavailable error when geolocation is not available', async () => {
// suppress expected errors from test log
jest.spyOn(logger, 'error').mockImplementation(() => { });
// remove the mock we added
// @ts-ignore illegal assignment to readonly property
navigator.geolocation = undefined;
await expect(() => getCurrentPosition()).rejects.toThrow(GeolocationError.Unavailable);
});
it('throws with geolocation error when geolocation.getCurrentPosition fails', async () => {
// suppress expected errors from test log
jest.spyOn(logger, 'error').mockImplementation(() => { });
const timeoutError = getMockGeolocationPositionError(3, 'message');
geolocation.getCurrentPosition.mockImplementation((callback, error) => error(timeoutError));
await expect(() => getCurrentPosition()).rejects.toThrow(GeolocationError.Timeout);
});
it('resolves with current location', async () => {
jest.spyOn(logger, 'error').mockImplementation(() => { });
geolocation.getCurrentPosition.mockImplementation((callback, error) => callback(defaultPosition));
const result = await getCurrentPosition();
expect(result).toEqual(defaultPosition);
});
});
});