Apply prettier formatting
This commit is contained in:
parent
1cac306093
commit
526645c791
1576 changed files with 65385 additions and 62478 deletions
|
@ -19,10 +19,10 @@ import { Beacon } from "matrix-js-sdk/src/matrix";
|
|||
import { Bounds, getBeaconBounds } from "../../../src/utils/beacon/bounds";
|
||||
import { makeBeaconEvent, makeBeaconInfoEvent } from "../../test-utils";
|
||||
|
||||
describe('getBeaconBounds()', () => {
|
||||
const userId = '@user:server';
|
||||
const roomId = '!room:server';
|
||||
const makeBeaconWithLocation = (latLon: {lat: number, lon: number}) => {
|
||||
describe("getBeaconBounds()", () => {
|
||||
const userId = "@user:server";
|
||||
const roomId = "!room:server";
|
||||
const makeBeaconWithLocation = (latLon: { lat: number; lon: number }) => {
|
||||
const geoUri = `geo:${latLon.lat},${latLon.lon}`;
|
||||
const beacon = new Beacon(makeBeaconInfoEvent(userId, roomId, { isLive: true }));
|
||||
// @ts-ignore private prop, sets internal live property so addLocations works
|
||||
|
@ -57,39 +57,45 @@ describe('getBeaconBounds()', () => {
|
|||
const auckland = makeBeaconWithLocation(geo.auckland);
|
||||
const lima = makeBeaconWithLocation(geo.lima);
|
||||
|
||||
it('should return undefined when there are no beacons', () => {
|
||||
it("should return undefined when there are no beacons", () => {
|
||||
expect(getBeaconBounds([])).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should return undefined when no beacons have locations', () => {
|
||||
it("should return undefined when no beacons have locations", () => {
|
||||
const beacon = new Beacon(makeBeaconInfoEvent(userId, roomId));
|
||||
expect(getBeaconBounds([beacon])).toBeUndefined();
|
||||
});
|
||||
|
||||
type TestCase = [string, Beacon[], Bounds];
|
||||
it.each<TestCase>([
|
||||
['one beacon', [london],
|
||||
[
|
||||
"one beacon",
|
||||
[london],
|
||||
{ north: geo.london.lat, south: geo.london.lat, east: geo.london.lon, west: geo.london.lon },
|
||||
],
|
||||
['beacons in the northern hemisphere, west of meridian',
|
||||
[
|
||||
"beacons in the northern hemisphere, west of meridian",
|
||||
[london, reykjavik],
|
||||
{ north: geo.reykjavik.lat, south: geo.london.lat, east: geo.london.lon, west: geo.reykjavik.lon },
|
||||
],
|
||||
['beacons in the northern hemisphere, both sides of meridian',
|
||||
[
|
||||
"beacons in the northern hemisphere, both sides of meridian",
|
||||
[london, reykjavik, paris],
|
||||
// reykjavik northmost and westmost, paris southmost and eastmost
|
||||
{ north: geo.reykjavik.lat, south: geo.paris.lat, east: geo.paris.lon, west: geo.reykjavik.lon },
|
||||
],
|
||||
['beacons in the southern hemisphere',
|
||||
[
|
||||
"beacons in the southern hemisphere",
|
||||
[auckland, lima],
|
||||
// lima northmost and westmost, auckland southmost and eastmost
|
||||
{ north: geo.lima.lat, south: geo.auckland.lat, east: geo.auckland.lon, west: geo.lima.lon },
|
||||
],
|
||||
['beacons in both hemispheres',
|
||||
[
|
||||
"beacons in both hemispheres",
|
||||
[auckland, lima, paris],
|
||||
{ north: geo.paris.lat, south: geo.auckland.lat, east: geo.auckland.lon, west: geo.lima.lon },
|
||||
],
|
||||
])('gets correct bounds for %s', (_description, beacons, expectedBounds) => {
|
||||
])("gets correct bounds for %s", (_description, beacons, expectedBounds) => {
|
||||
expect(getBeaconBounds(beacons)).toEqual(expectedBounds);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -16,35 +16,31 @@ limitations under the License.
|
|||
|
||||
import { Beacon } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import {
|
||||
msUntilExpiry,
|
||||
sortBeaconsByLatestExpiry,
|
||||
sortBeaconsByLatestCreation,
|
||||
} from "../../../src/utils/beacon";
|
||||
import { msUntilExpiry, sortBeaconsByLatestExpiry, sortBeaconsByLatestCreation } from "../../../src/utils/beacon";
|
||||
import { makeBeaconInfoEvent } from "../../test-utils";
|
||||
|
||||
describe('beacon utils', () => {
|
||||
describe("beacon utils", () => {
|
||||
// 14.03.2022 16:15
|
||||
const now = 1647270879403;
|
||||
const HOUR_MS = 3600000;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.spyOn(global.Date, 'now').mockReturnValue(now);
|
||||
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
jest.spyOn(global.Date, 'now').mockRestore();
|
||||
jest.spyOn(global.Date, "now").mockRestore();
|
||||
});
|
||||
|
||||
describe('msUntilExpiry', () => {
|
||||
it('returns remaining duration', () => {
|
||||
describe("msUntilExpiry", () => {
|
||||
it("returns remaining duration", () => {
|
||||
const start = now - HOUR_MS;
|
||||
const durationMs = HOUR_MS * 3;
|
||||
|
||||
expect(msUntilExpiry(start, durationMs)).toEqual(HOUR_MS * 2);
|
||||
});
|
||||
|
||||
it('returns 0 when expiry has already passed', () => {
|
||||
it("returns 0 when expiry has already passed", () => {
|
||||
// created 3h ago
|
||||
const start = now - HOUR_MS * 3;
|
||||
// 1h durations
|
||||
|
@ -54,65 +50,49 @@ describe('beacon utils', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('sortBeaconsByLatestExpiry()', () => {
|
||||
const roomId = '!room:server';
|
||||
const aliceId = '@alive:server';
|
||||
describe("sortBeaconsByLatestExpiry()", () => {
|
||||
const roomId = "!room:server";
|
||||
const aliceId = "@alive:server";
|
||||
|
||||
// 12h old, 12h left
|
||||
const beacon1 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS },
|
||||
'$1',
|
||||
));
|
||||
const beacon1 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS }, "$1"),
|
||||
);
|
||||
// 10h left
|
||||
const beacon2 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS * 10, timestamp: now },
|
||||
'$2',
|
||||
));
|
||||
const beacon2 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 10, timestamp: now }, "$2"),
|
||||
);
|
||||
|
||||
// 1ms left
|
||||
const beacon3 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS + 1, timestamp: now - HOUR_MS },
|
||||
'$3',
|
||||
));
|
||||
const beacon3 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS + 1, timestamp: now - HOUR_MS }, "$3"),
|
||||
);
|
||||
|
||||
it('sorts beacons by descending expiry time', () => {
|
||||
expect([beacon2, beacon3, beacon1].sort(sortBeaconsByLatestExpiry)).toEqual([
|
||||
beacon1, beacon2, beacon3,
|
||||
]);
|
||||
it("sorts beacons by descending expiry time", () => {
|
||||
expect([beacon2, beacon3, beacon1].sort(sortBeaconsByLatestExpiry)).toEqual([beacon1, beacon2, beacon3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sortBeaconsByLatestCreation()', () => {
|
||||
const roomId = '!room:server';
|
||||
const aliceId = '@alive:server';
|
||||
describe("sortBeaconsByLatestCreation()", () => {
|
||||
const roomId = "!room:server";
|
||||
const aliceId = "@alive:server";
|
||||
|
||||
// 12h old, 12h left
|
||||
const beacon1 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS },
|
||||
'$1',
|
||||
));
|
||||
const beacon1 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 24, timestamp: now - 12 * HOUR_MS }, "$1"),
|
||||
);
|
||||
// 10h left
|
||||
const beacon2 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS * 10, timestamp: now },
|
||||
'$2',
|
||||
));
|
||||
const beacon2 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS * 10, timestamp: now }, "$2"),
|
||||
);
|
||||
|
||||
// 1ms left
|
||||
const beacon3 = new Beacon(makeBeaconInfoEvent(aliceId,
|
||||
roomId,
|
||||
{ timeout: HOUR_MS + 1, timestamp: now - HOUR_MS },
|
||||
'$3',
|
||||
));
|
||||
const beacon3 = new Beacon(
|
||||
makeBeaconInfoEvent(aliceId, roomId, { timeout: HOUR_MS + 1, timestamp: now - HOUR_MS }, "$3"),
|
||||
);
|
||||
|
||||
it('sorts beacons by descending creation time', () => {
|
||||
expect([beacon1, beacon2, beacon3].sort(sortBeaconsByLatestCreation)).toEqual([
|
||||
beacon2, beacon3, beacon1,
|
||||
]);
|
||||
it("sorts beacons by descending creation time", () => {
|
||||
expect([beacon1, beacon2, beacon3].sort(sortBeaconsByLatestCreation)).toEqual([beacon2, beacon3, beacon1]);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -24,13 +24,9 @@ import {
|
|||
watchPosition,
|
||||
} from "../../../src/utils/beacon";
|
||||
import { getCurrentPosition } from "../../../src/utils/beacon/geolocation";
|
||||
import {
|
||||
makeGeolocationPosition,
|
||||
mockGeolocation,
|
||||
getMockGeolocationPositionError,
|
||||
} from "../../test-utils";
|
||||
import { makeGeolocationPosition, mockGeolocation, getMockGeolocationPositionError } from "../../test-utils";
|
||||
|
||||
describe('geolocation utilities', () => {
|
||||
describe("geolocation utilities", () => {
|
||||
let geolocation;
|
||||
const defaultPosition = makeGeolocationPosition({});
|
||||
|
||||
|
@ -39,15 +35,15 @@ describe('geolocation utilities', () => {
|
|||
|
||||
beforeEach(() => {
|
||||
geolocation = mockGeolocation();
|
||||
jest.spyOn(Date, 'now').mockReturnValue(now);
|
||||
jest.spyOn(Date, "now").mockReturnValue(now);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.spyOn(Date, 'now').mockRestore();
|
||||
jest.spyOn(logger, 'error').mockRestore();
|
||||
jest.spyOn(Date, "now").mockRestore();
|
||||
jest.spyOn(logger, "error").mockRestore();
|
||||
});
|
||||
|
||||
describe('getGeoUri', () => {
|
||||
describe("getGeoUri", () => {
|
||||
it("Renders a URI with only lat and lon", () => {
|
||||
const pos = {
|
||||
latitude: 43.2,
|
||||
|
@ -106,50 +102,51 @@ describe('geolocation utilities', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('mapGeolocationError', () => {
|
||||
describe("mapGeolocationError", () => {
|
||||
beforeEach(() => {
|
||||
// suppress expected errors from test log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
jest.spyOn(logger, "error").mockImplementation(() => {});
|
||||
});
|
||||
|
||||
it('returns default for other error', () => {
|
||||
const error = new Error('oh no..');
|
||||
it("returns default for other error", () => {
|
||||
const error = new Error("oh no..");
|
||||
expect(mapGeolocationError(error)).toEqual(GeolocationError.Default);
|
||||
});
|
||||
|
||||
it('returns unavailable for unavailable error', () => {
|
||||
it("returns unavailable for unavailable error", () => {
|
||||
const error = new Error(GeolocationError.Unavailable);
|
||||
expect(mapGeolocationError(error)).toEqual(GeolocationError.Unavailable);
|
||||
});
|
||||
|
||||
it('maps geo error permissiondenied correctly', () => {
|
||||
const error = getMockGeolocationPositionError(1, 'message');
|
||||
it("maps geo error permissiondenied correctly", () => {
|
||||
const error = getMockGeolocationPositionError(1, "message");
|
||||
expect(mapGeolocationError(error)).toEqual(GeolocationError.PermissionDenied);
|
||||
});
|
||||
|
||||
it('maps geo position unavailable error correctly', () => {
|
||||
const error = getMockGeolocationPositionError(2, 'message');
|
||||
it("maps geo position unavailable error correctly", () => {
|
||||
const error = getMockGeolocationPositionError(2, "message");
|
||||
expect(mapGeolocationError(error)).toEqual(GeolocationError.PositionUnavailable);
|
||||
});
|
||||
|
||||
it('maps geo timeout error correctly', () => {
|
||||
const error = getMockGeolocationPositionError(3, 'message');
|
||||
it("maps geo timeout error correctly", () => {
|
||||
const error = getMockGeolocationPositionError(3, "message");
|
||||
expect(mapGeolocationError(error)).toEqual(GeolocationError.Timeout);
|
||||
});
|
||||
});
|
||||
|
||||
describe('mapGeolocationPositionToTimedGeo()', () => {
|
||||
it('maps geolocation position correctly', () => {
|
||||
describe("mapGeolocationPositionToTimedGeo()", () => {
|
||||
it("maps geolocation position correctly", () => {
|
||||
expect(mapGeolocationPositionToTimedGeo(defaultPosition)).toEqual({
|
||||
timestamp: now, geoUri: 'geo:54.001927,-8.253491;u=1',
|
||||
timestamp: now,
|
||||
geoUri: "geo:54.001927,-8.253491;u=1",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('watchPosition()', () => {
|
||||
it('throws with unavailable error when geolocation is not available', () => {
|
||||
describe("watchPosition()", () => {
|
||||
it("throws with unavailable error when geolocation is not available", () => {
|
||||
// suppress expected errors from test log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
jest.spyOn(logger, "error").mockImplementation(() => {});
|
||||
|
||||
// remove the mock we added
|
||||
// @ts-ignore illegal assignment to readonly property
|
||||
|
@ -161,7 +158,7 @@ describe('geolocation utilities', () => {
|
|||
expect(() => watchPosition(positionHandler, errorHandler)).toThrow(GeolocationError.Unavailable);
|
||||
});
|
||||
|
||||
it('sets up position handler with correct options', () => {
|
||||
it("sets up position handler with correct options", () => {
|
||||
const positionHandler = jest.fn();
|
||||
const errorHandler = jest.fn();
|
||||
watchPosition(positionHandler, errorHandler);
|
||||
|
@ -173,7 +170,7 @@ describe('geolocation utilities', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('returns clearWatch function', () => {
|
||||
it("returns clearWatch function", () => {
|
||||
const watchId = 1;
|
||||
geolocation.watchPosition.mockReturnValue(watchId);
|
||||
const positionHandler = jest.fn();
|
||||
|
@ -185,7 +182,7 @@ describe('geolocation utilities', () => {
|
|||
expect(geolocation.clearWatch).toHaveBeenCalledWith(watchId);
|
||||
});
|
||||
|
||||
it('calls position handler with position', () => {
|
||||
it("calls position handler with position", () => {
|
||||
const positionHandler = jest.fn();
|
||||
const errorHandler = jest.fn();
|
||||
watchPosition(positionHandler, errorHandler);
|
||||
|
@ -193,11 +190,11 @@ describe('geolocation utilities', () => {
|
|||
expect(positionHandler).toHaveBeenCalledWith(defaultPosition);
|
||||
});
|
||||
|
||||
it('maps geolocation position error and calls error handler', () => {
|
||||
it("maps geolocation position error and calls error handler", () => {
|
||||
// suppress expected errors from test log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
geolocation.watchPosition.mockImplementation(
|
||||
(_callback, error) => error(getMockGeolocationPositionError(1, 'message')),
|
||||
jest.spyOn(logger, "error").mockImplementation(() => {});
|
||||
geolocation.watchPosition.mockImplementation((_callback, error) =>
|
||||
error(getMockGeolocationPositionError(1, "message")),
|
||||
);
|
||||
const positionHandler = jest.fn();
|
||||
const errorHandler = jest.fn();
|
||||
|
@ -207,10 +204,10 @@ describe('geolocation utilities', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('getCurrentPosition()', () => {
|
||||
it('throws with unavailable error when geolocation is not available', async () => {
|
||||
describe("getCurrentPosition()", () => {
|
||||
it("throws with unavailable error when geolocation is not available", async () => {
|
||||
// suppress expected errors from test log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
jest.spyOn(logger, "error").mockImplementation(() => {});
|
||||
|
||||
// remove the mock we added
|
||||
// @ts-ignore illegal assignment to readonly property
|
||||
|
@ -219,17 +216,17 @@ describe('geolocation utilities', () => {
|
|||
await expect(() => getCurrentPosition()).rejects.toThrow(GeolocationError.Unavailable);
|
||||
});
|
||||
|
||||
it('throws with geolocation error when geolocation.getCurrentPosition fails', async () => {
|
||||
it("throws with geolocation error when geolocation.getCurrentPosition fails", async () => {
|
||||
// suppress expected errors from test log
|
||||
jest.spyOn(logger, 'error').mockImplementation(() => { });
|
||||
jest.spyOn(logger, "error").mockImplementation(() => {});
|
||||
|
||||
const timeoutError = getMockGeolocationPositionError(3, 'message');
|
||||
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 () => {
|
||||
it("resolves with current location", async () => {
|
||||
geolocation.getCurrentPosition.mockImplementation((callback, error) => callback(defaultPosition));
|
||||
|
||||
const result = await getCurrentPosition();
|
||||
|
|
|
@ -19,28 +19,28 @@ import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
|||
import { shouldDisplayAsBeaconTile } from "../../../src/utils/beacon/timeline";
|
||||
import { makeBeaconInfoEvent } from "../../test-utils";
|
||||
|
||||
describe('shouldDisplayAsBeaconTile', () => {
|
||||
const userId = '@user:server';
|
||||
const roomId = '!room:server';
|
||||
describe("shouldDisplayAsBeaconTile", () => {
|
||||
const userId = "@user:server";
|
||||
const roomId = "!room:server";
|
||||
const liveBeacon = makeBeaconInfoEvent(userId, roomId, { isLive: true });
|
||||
const notLiveBeacon = makeBeaconInfoEvent(userId, roomId, { isLive: false });
|
||||
const memberEvent = new MatrixEvent({ type: EventType.RoomMember });
|
||||
const redactedBeacon = makeBeaconInfoEvent(userId, roomId, { isLive: false });
|
||||
redactedBeacon.makeRedacted(redactedBeacon);
|
||||
|
||||
it('returns true for a beacon with live property set to true', () => {
|
||||
it("returns true for a beacon with live property set to true", () => {
|
||||
expect(shouldDisplayAsBeaconTile(liveBeacon)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns true for a redacted beacon', () => {
|
||||
it("returns true for a redacted beacon", () => {
|
||||
expect(shouldDisplayAsBeaconTile(redactedBeacon)).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false for a beacon with live property set to false', () => {
|
||||
it("returns false for a beacon with live property set to false", () => {
|
||||
expect(shouldDisplayAsBeaconTile(notLiveBeacon)).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false for a non beacon event', () => {
|
||||
it("returns false for a non beacon event", () => {
|
||||
expect(shouldDisplayAsBeaconTile(memberEvent)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue