Live location sharing: geolocation utilities (#8126)

* geolocation utilities

Signed-off-by: Kerry Archibald <kerrya@element.io>

* remove debug

Signed-off-by: Kerry Archibald <kerrya@element.io>

* comments for ts-ignores

Signed-off-by: Kerry Archibald <kerrya@element.io>
This commit is contained in:
Kerry 2022-03-23 18:08:56 +01:00 committed by GitHub
parent 3534e9b6ce
commit 1495c23a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 358 additions and 95 deletions

View file

@ -18,6 +18,7 @@ import { makeBeaconInfoContent, makeBeaconContent } from "matrix-js-sdk/src/cont
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
import { M_BEACON, M_BEACON_INFO } from "matrix-js-sdk/src/@types/beacon";
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
import { MockedObject } from "jest-mock";
type InfoContentProps = {
timeout: number;
@ -107,7 +108,7 @@ export const makeBeaconEvent = (
*/
export const makeGeolocationPosition = (
{ timestamp, coords }:
{ timestamp?: number, coords: Partial<GeolocationCoordinates> },
{ timestamp?: number, coords?: Partial<GeolocationCoordinates> },
): GeolocationPosition => ({
timestamp: timestamp ?? 1647256791840,
coords: {
@ -121,3 +122,22 @@ export const makeGeolocationPosition = (
...coords,
},
});
/**
* Creates a basic mock of Geolocation
* sets navigator.geolocation to the mock
* and returns mock
*/
export const mockGeolocation = (): MockedObject<Geolocation> => {
const mockGeolocation = {
clearWatch: jest.fn(),
getCurrentPosition: jest.fn().mockImplementation(callback => callback(makeGeolocationPosition({}))),
watchPosition: jest.fn().mockImplementation(callback => callback(makeGeolocationPosition({}))),
} as unknown as MockedObject<Geolocation>;
// jest jsdom does not provide geolocation
// @ts-ignore illegal assignment to readonly property
navigator.geolocation = mockGeolocation;
return mockGeolocation;
};