2022-04-11 09:16:32 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-11 09:16:32 +00:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-04-11 09:16:32 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
import React, { ComponentProps } from "react";
|
2023-02-28 08:58:23 +00:00
|
|
|
import { act, fireEvent, render } from "@testing-library/react";
|
2022-12-18 23:17:15 +00:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2023-08-09 15:10:54 +00:00
|
|
|
import {
|
|
|
|
BeaconEvent,
|
|
|
|
getBeaconInfoIdentifier,
|
|
|
|
RelationType,
|
|
|
|
MatrixEvent,
|
|
|
|
EventType,
|
|
|
|
Relations,
|
2023-08-23 09:04:25 +00:00
|
|
|
M_BEACON,
|
2024-03-25 12:21:02 +00:00
|
|
|
Room,
|
2023-08-09 15:10:54 +00:00
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2022-12-12 11:24:14 +00:00
|
|
|
|
|
|
|
import MBeaconBody from "../../../../src/components/views/messages/MBeaconBody";
|
2022-04-19 16:47:19 +00:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
makeBeaconEvent,
|
|
|
|
makeBeaconInfoEvent,
|
2022-07-06 14:34:33 +00:00
|
|
|
makeRoomWithBeacons,
|
2022-04-19 16:47:19 +00:00
|
|
|
makeRoomWithStateEvents,
|
2022-12-12 11:24:14 +00:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import Modal from "../../../../src/Modal";
|
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
|
|
|
import * as mapUtilHooks from "../../../../src/utils/location/useMap";
|
|
|
|
import { LocationShareError } from "../../../../src/utils/location";
|
|
|
|
|
|
|
|
describe("<MBeaconBody />", () => {
|
2022-04-11 09:16:32 +00:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
// stable date for snapshots
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const aliceId = "@alice:server";
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-12-18 23:17:15 +00:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-12 08:13:55 +00:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
|
|
|
|
2022-04-11 09:16:32 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2022-04-12 08:13:55 +00:00
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-12 08:13:55 +00:00
|
|
|
}),
|
2022-04-11 09:16:32 +00:00
|
|
|
getUserId: jest.fn().mockReturnValue(aliceId),
|
|
|
|
getRoom: jest.fn(),
|
2022-06-29 09:07:36 +00:00
|
|
|
redactEvent: jest.fn(),
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const defaultEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
2022-04-12 08:13:55 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
const defaultProps: ComponentProps<typeof MBeaconBody> = {
|
2022-04-11 09:16:32 +00:00
|
|
|
mxEvent: defaultEvent,
|
|
|
|
highlights: [],
|
2022-12-12 11:24:14 +00:00
|
|
|
highlightLink: "",
|
2022-04-11 09:16:32 +00:00
|
|
|
onHeightChanged: jest.fn(),
|
|
|
|
onMessageAllowed: jest.fn(),
|
|
|
|
// we dont use these and they pollute the snapshots
|
|
|
|
permalinkCreator: {} as unknown as RoomPermalinkCreator,
|
|
|
|
mediaEventHelper: {} as unknown as MediaEventHelper,
|
|
|
|
};
|
2022-04-12 08:13:55 +00:00
|
|
|
|
2022-04-11 09:16:32 +00:00
|
|
|
const getComponent = (props = {}) =>
|
2023-02-28 08:58:23 +00:00
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<MBeaconBody {...defaultProps} {...props} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
const modalSpy = jest.spyOn(Modal, "createDialog").mockReturnValue({
|
|
|
|
finished: Promise.resolve([true]),
|
|
|
|
close: () => {},
|
|
|
|
});
|
2022-05-23 10:19:59 +00:00
|
|
|
|
2022-04-12 08:13:55 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
const testBeaconStatuses = () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders stopped beacon UI for an explicitly stopped beacon", () => {
|
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false }, "$alice-room1-1");
|
2022-07-06 14:34:33 +00:00
|
|
|
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Live location ended");
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders stopped beacon UI for an expired beacon", () => {
|
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
2022-07-06 14:34:33 +00:00
|
|
|
roomId,
|
|
|
|
// puts this beacons live period in the past
|
|
|
|
{ isLive: true, timestamp: now - 600000, timeout: 500 },
|
2022-12-12 11:24:14 +00:00
|
|
|
"$alice-room1-1",
|
2022-07-06 14:34:33 +00:00
|
|
|
);
|
|
|
|
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Live location ended");
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders loading beacon UI for a beacon that has not started yet", () => {
|
2022-07-06 14:34:33 +00:00
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
|
|
|
roomId,
|
|
|
|
// puts this beacons start timestamp in the future
|
|
|
|
{ isLive: true, timestamp: now + 60000, timeout: 500 },
|
2022-12-12 11:24:14 +00:00
|
|
|
"$alice-room1-1",
|
2022-07-06 14:34:33 +00:00
|
|
|
);
|
|
|
|
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Loading live location…");
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
2022-06-09 12:25:30 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not open maximised map when on click when beacon is stopped", () => {
|
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
2022-07-06 14:34:33 +00:00
|
|
|
roomId,
|
|
|
|
// puts this beacons live period in the past
|
|
|
|
{ isLive: true, timestamp: now - 600000, timeout: 500 },
|
2022-12-12 11:24:14 +00:00
|
|
|
"$alice-room1-1",
|
2022-07-06 14:34:33 +00:00
|
|
|
);
|
|
|
|
makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
2023-02-28 08:58:23 +00:00
|
|
|
fireEvent.click(component.container.querySelector(".mx_MBeaconBody_map")!);
|
2022-07-06 14:34:33 +00:00
|
|
|
|
|
|
|
expect(modalSpy).not.toHaveBeenCalled();
|
2022-04-14 13:14:05 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders stopped UI when a beacon event is not the latest beacon for a user", () => {
|
2022-07-06 14:34:33 +00:00
|
|
|
const aliceBeaconInfo1 = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
|
|
|
roomId,
|
|
|
|
// this one is a little older
|
|
|
|
{ isLive: true, timestamp: now - 500 },
|
2022-12-12 11:24:14 +00:00
|
|
|
"$alice-room1-1",
|
2022-07-06 14:34:33 +00:00
|
|
|
);
|
|
|
|
aliceBeaconInfo1.event.origin_server_ts = now - 500;
|
2022-12-12 11:24:14 +00:00
|
|
|
const aliceBeaconInfo2 = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-2");
|
2022-04-14 13:14:05 +00:00
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
makeRoomWithStateEvents([aliceBeaconInfo1, aliceBeaconInfo2], { roomId, mockClient });
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo1 });
|
|
|
|
// beacon1 has been superceded by beacon2
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Live location ended");
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders stopped UI when a beacon event is replaced", () => {
|
2022-07-06 14:34:33 +00:00
|
|
|
const aliceBeaconInfo1 = makeBeaconInfoEvent(
|
|
|
|
aliceId,
|
|
|
|
roomId,
|
|
|
|
// this one is a little older
|
|
|
|
{ isLive: true, timestamp: now - 500 },
|
2022-12-12 11:24:14 +00:00
|
|
|
"$alice-room1-1",
|
2022-07-06 14:34:33 +00:00
|
|
|
);
|
|
|
|
aliceBeaconInfo1.event.origin_server_ts = now - 500;
|
2022-12-12 11:24:14 +00:00
|
|
|
const aliceBeaconInfo2 = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-2");
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
const room = makeRoomWithStateEvents([aliceBeaconInfo1], { roomId, mockClient });
|
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo1 });
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo1))!;
|
2022-07-06 14:34:33 +00:00
|
|
|
// update alice's beacon with a new edition
|
|
|
|
// beacon instance emits
|
|
|
|
act(() => {
|
|
|
|
beaconInstance.update(aliceBeaconInfo2);
|
|
|
|
});
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
// beacon1 has been superceded by beacon2
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Live location ended");
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
|
|
|
};
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-07-06 14:34:33 +00:00
|
|
|
testBeaconStatuses();
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("on liveness change", () => {
|
|
|
|
it("renders stopped UI when a beacon stops being live", () => {
|
|
|
|
const aliceBeaconInfo = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2022-04-19 16:47:19 +00:00
|
|
|
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo))!;
|
2022-04-11 09:16:32 +00:00
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore cheat to force beacon to not live
|
|
|
|
beaconInstance._isLive = false;
|
|
|
|
beaconInstance.emit(BeaconEvent.LivenessChange, false, beaconInstance);
|
|
|
|
});
|
|
|
|
|
|
|
|
// stopped UI
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Live location ended");
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("latestLocationState", () => {
|
|
|
|
const aliceBeaconInfo = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
|
|
|
|
|
|
|
const location1 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: aliceBeaconInfo.getId(),
|
|
|
|
geoUri: "geo:51,41",
|
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
|
|
|
const location2 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: aliceBeaconInfo.getId(),
|
|
|
|
geoUri: "geo:52,42",
|
|
|
|
timestamp: now + 10000,
|
|
|
|
});
|
|
|
|
|
|
|
|
it("renders a live beacon without a location correctly", () => {
|
2022-04-19 16:47:19 +00:00
|
|
|
makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2022-04-11 09:16:32 +00:00
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo });
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container).toHaveTextContent("Loading live location…");
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does nothing on click when a beacon has no location", () => {
|
2022-04-19 16:47:19 +00:00
|
|
|
makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2022-04-14 13:14:05 +00:00
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo });
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
fireEvent.click(component.container.querySelector(".mx_MBeaconBody_map")!);
|
2022-04-19 11:35:39 +00:00
|
|
|
|
|
|
|
expect(modalSpy).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders a live beacon with a location correctly", () => {
|
2022-04-19 16:47:19 +00:00
|
|
|
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo))!;
|
2022-04-19 11:35:39 +00:00
|
|
|
beaconInstance.addLocations([location1]);
|
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo });
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.container.querySelector(".maplibregl-canvas-container")).toBeDefined();
|
2022-04-19 11:35:39 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("opens maximised map view on click when beacon has a live location", () => {
|
2022-04-19 16:47:19 +00:00
|
|
|
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo))!;
|
2022-04-19 11:35:39 +00:00
|
|
|
beaconInstance.addLocations([location1]);
|
|
|
|
const component = getComponent({ mxEvent: aliceBeaconInfo });
|
|
|
|
|
2023-02-28 08:58:23 +00:00
|
|
|
fireEvent.click(component.container.querySelector(".mx_Map")!);
|
2022-04-19 11:35:39 +00:00
|
|
|
|
|
|
|
// opens modal
|
|
|
|
expect(modalSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates latest location", () => {
|
2022-04-19 16:47:19 +00:00
|
|
|
const room = makeRoomWithStateEvents([aliceBeaconInfo], { roomId, mockClient });
|
2023-02-28 08:58:23 +00:00
|
|
|
getComponent({ mxEvent: aliceBeaconInfo });
|
2022-04-11 09:16:32 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(aliceBeaconInfo))!;
|
2022-04-11 09:16:32 +00:00
|
|
|
act(() => {
|
|
|
|
beaconInstance.addLocations([location1]);
|
|
|
|
});
|
|
|
|
|
2022-04-12 08:13:55 +00:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 41 });
|
|
|
|
expect(mockMarker.setLngLat).toHaveBeenCalledWith({ lat: 51, lon: 41 });
|
2022-04-11 09:16:32 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
beaconInstance.addLocations([location2]);
|
|
|
|
});
|
|
|
|
|
2022-04-12 08:13:55 +00:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 52, lon: 42 });
|
|
|
|
expect(mockMarker.setLngLat).toHaveBeenCalledWith({ lat: 52, lon: 42 });
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|
|
|
|
});
|
2022-06-29 09:07:36 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("redaction", () => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const makeEvents = (): {
|
|
|
|
beaconInfoEvent: MatrixEvent;
|
|
|
|
location1: MatrixEvent;
|
|
|
|
location2: MatrixEvent;
|
|
|
|
} => {
|
2022-12-12 11:24:14 +00:00
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
2022-06-29 09:07:36 +00:00
|
|
|
|
|
|
|
const location1 = makeBeaconEvent(
|
2022-12-12 11:24:14 +00:00
|
|
|
aliceId,
|
|
|
|
{ beaconInfoId: beaconInfoEvent.getId(), geoUri: "geo:51,41", timestamp: now + 1 },
|
2022-06-29 09:07:36 +00:00
|
|
|
roomId,
|
|
|
|
);
|
2022-12-12 11:24:14 +00:00
|
|
|
location1.event.event_id = "1";
|
2022-06-29 09:07:36 +00:00
|
|
|
const location2 = makeBeaconEvent(
|
2022-12-12 11:24:14 +00:00
|
|
|
aliceId,
|
|
|
|
{ beaconInfoId: beaconInfoEvent.getId(), geoUri: "geo:52,42", timestamp: now + 10000 },
|
2022-06-29 09:07:36 +00:00
|
|
|
roomId,
|
|
|
|
);
|
2022-12-12 11:24:14 +00:00
|
|
|
location2.event.event_id = "2";
|
2022-06-29 09:07:36 +00:00
|
|
|
return { beaconInfoEvent, location1, location2 };
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
const redactionEvent = new MatrixEvent({ type: EventType.RoomRedaction, content: { reason: "test reason" } });
|
2022-06-29 09:07:36 +00:00
|
|
|
|
2024-03-25 12:21:02 +00:00
|
|
|
const setupRoomWithBeacon = (beaconInfoEvent: MatrixEvent, locationEvents: MatrixEvent[] = []): Room => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const room = makeRoomWithStateEvents([beaconInfoEvent], { roomId, mockClient });
|
2023-02-13 17:01:43 +00:00
|
|
|
const beaconInstance = room.currentState.beacons.get(getBeaconInfoIdentifier(beaconInfoEvent))!;
|
2022-06-29 09:07:36 +00:00
|
|
|
beaconInstance.addLocations(locationEvents);
|
2024-03-25 12:21:02 +00:00
|
|
|
return room;
|
2022-06-29 09:07:36 +00:00
|
|
|
};
|
|
|
|
const mockGetRelationsForEvent = (locationEvents: MatrixEvent[] = []) => {
|
|
|
|
const relations = new Relations(RelationType.Reference, M_BEACON.name, mockClient);
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(relations, "getRelations").mockReturnValue(locationEvents);
|
2022-06-29 09:07:36 +00:00
|
|
|
|
|
|
|
const getRelationsForEvent = jest.fn().mockReturnValue(relations);
|
|
|
|
|
|
|
|
return getRelationsForEvent;
|
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does nothing when getRelationsForEvent is falsy", () => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const { beaconInfoEvent, location1, location2 } = makeEvents();
|
2024-03-25 12:21:02 +00:00
|
|
|
const room = setupRoomWithBeacon(beaconInfoEvent, [location1, location2]);
|
2022-06-29 09:07:36 +00:00
|
|
|
|
|
|
|
getComponent({ mxEvent: beaconInfoEvent });
|
|
|
|
|
|
|
|
act(() => {
|
2024-03-25 12:21:02 +00:00
|
|
|
beaconInfoEvent.makeRedacted(redactionEvent, room);
|
2022-06-29 09:07:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// no error, no redactions
|
|
|
|
expect(mockClient.redactEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("cleans up redaction listener on unmount", () => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const { beaconInfoEvent, location1, location2 } = makeEvents();
|
|
|
|
setupRoomWithBeacon(beaconInfoEvent, [location1, location2]);
|
2022-12-12 11:24:14 +00:00
|
|
|
const removeListenerSpy = jest.spyOn(beaconInfoEvent, "removeListener");
|
2022-06-29 09:07:36 +00:00
|
|
|
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
component.unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(removeListenerSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does nothing when beacon has no related locations", async () => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const { beaconInfoEvent } = makeEvents();
|
|
|
|
// no locations
|
2024-03-25 12:21:02 +00:00
|
|
|
const room = setupRoomWithBeacon(beaconInfoEvent, []);
|
2022-06-29 09:07:36 +00:00
|
|
|
const getRelationsForEvent = await mockGetRelationsForEvent();
|
|
|
|
|
|
|
|
getComponent({ mxEvent: beaconInfoEvent, getRelationsForEvent });
|
|
|
|
|
|
|
|
act(() => {
|
2024-03-25 12:21:02 +00:00
|
|
|
beaconInfoEvent.makeRedacted(redactionEvent, room);
|
2022-06-29 09:07:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(getRelationsForEvent).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
beaconInfoEvent.getId(),
|
|
|
|
RelationType.Reference,
|
|
|
|
M_BEACON.name,
|
2022-06-29 09:07:36 +00:00
|
|
|
);
|
|
|
|
expect(mockClient.redactEvent).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("redacts related locations on beacon redaction", async () => {
|
2022-06-29 09:07:36 +00:00
|
|
|
const { beaconInfoEvent, location1, location2 } = makeEvents();
|
2024-03-25 12:21:02 +00:00
|
|
|
const room = setupRoomWithBeacon(beaconInfoEvent, [location1, location2]);
|
2022-06-29 09:07:36 +00:00
|
|
|
|
|
|
|
const getRelationsForEvent = await mockGetRelationsForEvent([location1, location2]);
|
|
|
|
|
|
|
|
getComponent({ mxEvent: beaconInfoEvent, getRelationsForEvent });
|
|
|
|
|
|
|
|
act(() => {
|
2024-03-25 12:21:02 +00:00
|
|
|
beaconInfoEvent.makeRedacted(redactionEvent, room);
|
2022-06-29 09:07:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(getRelationsForEvent).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
beaconInfoEvent.getId(),
|
|
|
|
RelationType.Reference,
|
|
|
|
M_BEACON.name,
|
2022-06-29 09:07:36 +00:00
|
|
|
);
|
|
|
|
expect(mockClient.redactEvent).toHaveBeenCalledTimes(2);
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.redactEvent).toHaveBeenCalledWith(roomId, location1.getId(), undefined, {
|
|
|
|
reason: "test reason",
|
|
|
|
});
|
|
|
|
expect(mockClient.redactEvent).toHaveBeenCalledWith(roomId, location2.getId(), undefined, {
|
|
|
|
reason: "test reason",
|
|
|
|
});
|
2022-06-29 09:07:36 +00:00
|
|
|
});
|
|
|
|
});
|
2022-07-06 14:34:33 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("when map display is not configured", () => {
|
2022-07-06 14:34:33 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
// mock map utils to raise MapStyleUrlNotConfigured error
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(mapUtilHooks, "useMap").mockImplementation(({ onError }) => {
|
2023-02-04 19:38:27 +00:00
|
|
|
onError?.(new Error(LocationShareError.MapStyleUrlNotConfigured));
|
2022-12-12 11:24:14 +00:00
|
|
|
return mockMap;
|
|
|
|
});
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders maps unavailable error for a live beacon with location", () => {
|
|
|
|
const beaconInfoEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
|
|
|
const location1 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: beaconInfoEvent.getId(),
|
|
|
|
geoUri: "geo:51,41",
|
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
2022-07-06 14:34:33 +00:00
|
|
|
|
|
|
|
makeRoomWithBeacons(roomId, mockClient, [beaconInfoEvent], [location1]);
|
|
|
|
|
|
|
|
const component = getComponent({ mxEvent: beaconInfoEvent });
|
2023-02-28 08:58:23 +00:00
|
|
|
expect(component.getByTestId("map-rendering-error")).toMatchSnapshot();
|
2022-07-06 14:34:33 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// test that statuses display as expected with a map display error
|
|
|
|
testBeaconStatuses();
|
|
|
|
});
|
2022-04-11 09:16:32 +00:00
|
|
|
});
|