2022-04-14 15:14:05 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import React from "react";
|
2024-05-02 16:53:35 +02:00
|
|
|
import { act, fireEvent, render, RenderResult, waitFor } from "@testing-library/react";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { MatrixClient, MatrixEvent, Room, RoomMember, getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix";
|
2022-12-19 00:17:15 +01:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
|
|
|
import BeaconViewDialog from "../../../../src/components/views/beacon/BeaconViewDialog";
|
2022-04-14 15:14:05 +02:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
makeBeaconEvent,
|
|
|
|
makeBeaconInfoEvent,
|
2022-07-18 10:34:39 +02:00
|
|
|
makeRoomWithBeacons,
|
2022-04-19 18:47:19 +02:00
|
|
|
makeRoomWithStateEvents,
|
2022-12-12 12:24:14 +01:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
|
|
|
import { OwnBeaconStore } from "../../../../src/stores/OwnBeaconStore";
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<BeaconViewDialog />", () => {
|
2022-04-14 15:14:05 +02:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
// stable date for snapshots
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const aliceId = "@alice:server";
|
|
|
|
const bobId = "@bob:server";
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
const aliceMember = new RoomMember(roomId, aliceId);
|
|
|
|
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 12:24:14 +01:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-14 15:14:05 +02:00
|
|
|
}),
|
2022-04-21 09:06:57 +02:00
|
|
|
getUserId: jest.fn().mockReturnValue(bobId),
|
2022-04-14 15:14:05 +02:00
|
|
|
getRoom: jest.fn(),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
2022-04-21 09:06:57 +02:00
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|
|
|
|
|
2022-12-19 00:17:15 +01:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2023-02-21 22:32:37 +01:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
2022-04-21 18:56:11 +02:00
|
|
|
|
2022-04-14 15:14:05 +02:00
|
|
|
// make fresh rooms every time
|
|
|
|
// as we update room state
|
2022-04-19 18:47:19 +02:00
|
|
|
const setupRoom = (stateEvents: MatrixEvent[] = []): Room => {
|
|
|
|
const room1 = makeRoomWithStateEvents(stateEvents, { roomId, mockClient });
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(room1, "getMember").mockReturnValue(aliceMember);
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
return room1;
|
|
|
|
};
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const defaultEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const location1 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: defaultEvent.getId(),
|
|
|
|
geoUri: "geo:51,41",
|
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
roomId,
|
|
|
|
matrixClient: mockClient as MatrixClient,
|
|
|
|
};
|
|
|
|
|
2024-04-12 14:56:23 +02:00
|
|
|
const getComponent = (props = {}): RenderResult => render(<BeaconViewDialog {...defaultProps} {...props} />);
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
const openSidebar = (getByTestId: RenderResult["getByTestId"]) => {
|
|
|
|
fireEvent.click(getByTestId("beacon-view-dialog-open-sidebar"));
|
|
|
|
};
|
2022-07-18 10:34:39 +02:00
|
|
|
|
2022-04-21 09:06:57 +02:00
|
|
|
beforeEach(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(OwnBeaconStore.instance, "getLiveBeaconIds").mockRestore();
|
|
|
|
jest.spyOn(OwnBeaconStore.instance, "getBeaconById").mockRestore();
|
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
2022-04-21 18:56:11 +02:00
|
|
|
jest.clearAllMocks();
|
2022-04-21 09:06:57 +02:00
|
|
|
});
|
|
|
|
|
2024-05-02 16:53:35 +02:00
|
|
|
it("renders a map with markers", async () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-14 15:14:05 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
getComponent();
|
|
|
|
// centered on default event
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({
|
|
|
|
lon: 41,
|
|
|
|
lat: 51,
|
|
|
|
});
|
|
|
|
// marker added
|
2024-05-02 16:53:35 +02:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(mockMarker.addTo).toHaveBeenCalledWith(mockMap);
|
|
|
|
});
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not render any own beacon status when user is not live sharing", () => {
|
2022-04-21 09:06:57 +02:00
|
|
|
// default event belongs to alice, we are bob
|
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-21 09:06:57 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { queryByText } = getComponent();
|
|
|
|
expect(queryByText("Live location enabled")).not.toBeInTheDocument();
|
2022-04-21 09:06:57 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders own beacon status when user is live sharing", () => {
|
2022-04-21 09:06:57 +02:00
|
|
|
// default event belongs to alice
|
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-21 09:06:57 +02:00
|
|
|
beacon.addLocations([location1]);
|
|
|
|
// mock own beacon store to show default event as alice's live beacon
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(OwnBeaconStore.instance, "getLiveBeaconIds").mockReturnValue([beacon.identifier]);
|
|
|
|
jest.spyOn(OwnBeaconStore.instance, "getBeaconById").mockReturnValue(beacon);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container.querySelector(".mx_DialogOwnBeaconStatus")).toMatchSnapshot();
|
2022-04-21 09:06:57 +02:00
|
|
|
});
|
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
it("updates markers on changes to beacons", async () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-14 15:14:05 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container } = getComponent();
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
// one marker
|
|
|
|
expect(mockMarker.addTo).toHaveBeenCalledTimes(1);
|
|
|
|
expect(container.getElementsByClassName("mx_Marker").length).toEqual(1);
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
const anotherBeaconEvent = makeBeaconInfoEvent(bobId, roomId, { isLive: true }, "$bob-room1-1");
|
2022-04-14 15:14:05 +02:00
|
|
|
act(() => {
|
|
|
|
// emits RoomStateEvent.BeaconLiveness
|
|
|
|
room.currentState.setStateEvents([anotherBeaconEvent]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const beacon2 = room.currentState.beacons.get(getBeaconInfoIdentifier(anotherBeaconEvent))!;
|
|
|
|
beacon2.addLocations([location1]);
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// two markers now!
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.getElementsByClassName("mx_Marker").length).toEqual(2);
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|
2022-04-19 13:35:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("does not update bounds or center on changing beacons", () => {
|
2022-04-21 18:56:11 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-21 18:56:11 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container } = getComponent();
|
|
|
|
expect(container.getElementsByClassName("mx_Marker").length).toEqual(1);
|
2022-04-21 18:56:11 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const anotherBeaconEvent = makeBeaconInfoEvent(bobId, roomId, { isLive: true }, "$bob-room1-1");
|
2022-04-21 18:56:11 +02:00
|
|
|
act(() => {
|
|
|
|
// emits RoomStateEvent.BeaconLiveness
|
|
|
|
room.currentState.setStateEvents([anotherBeaconEvent]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const beacon2 = room.currentState.beacons.get(getBeaconInfoIdentifier(anotherBeaconEvent))!;
|
|
|
|
beacon2.addLocations([location1]);
|
2022-04-21 18:56:11 +02:00
|
|
|
});
|
2023-02-21 22:32:37 +01:00
|
|
|
// called once on init
|
2022-04-21 18:56:11 +02:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledTimes(1);
|
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders a fallback when there are no locations", () => {
|
2022-07-27 17:42:59 +02:00
|
|
|
// this is a cornercase, should not be a reachable state in UI anymore
|
|
|
|
const onFinished = jest.fn();
|
|
|
|
const room = setupRoom([defaultEvent]);
|
|
|
|
room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
2023-02-21 22:32:37 +01:00
|
|
|
const { getByTestId } = getComponent({ onFinished });
|
2022-07-27 17:42:59 +02:00
|
|
|
|
|
|
|
// map placeholder
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(getByTestId("beacon-view-dialog-map-fallback")).toMatchSnapshot();
|
2022-07-27 17:42:59 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
fireEvent.click(getByTestId("beacon-view-dialog-fallback-close"));
|
2022-07-27 17:42:59 +02:00
|
|
|
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders map without markers when no live beacons remain", () => {
|
2022-04-19 13:35:39 +02:00
|
|
|
const onFinished = jest.fn();
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-19 13:35:39 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container } = getComponent({ onFinished });
|
|
|
|
expect(container.getElementsByClassName("mx_Marker").length).toEqual(1);
|
2022-04-19 13:35:39 +02:00
|
|
|
|
|
|
|
// this will replace the defaultEvent
|
|
|
|
// leading to no more live beacons
|
2022-12-12 12:24:14 +01:00
|
|
|
const anotherBeaconEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false }, "$alice-room1-2");
|
2022-04-19 13:35:39 +02:00
|
|
|
|
2022-07-27 17:42:59 +02:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 41 });
|
|
|
|
// reset call counts
|
|
|
|
mocked(mockMap.setCenter).mockClear();
|
|
|
|
mocked(mockMap.fitBounds).mockClear();
|
|
|
|
|
2022-04-19 13:35:39 +02:00
|
|
|
act(() => {
|
|
|
|
// emits RoomStateEvent.BeaconLiveness
|
|
|
|
room.currentState.setStateEvents([anotherBeaconEvent]);
|
|
|
|
});
|
|
|
|
|
2022-07-27 17:42:59 +02:00
|
|
|
// no more avatars
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.getElementsByClassName("mx_Marker").length).toEqual(0);
|
2022-07-27 17:42:59 +02:00
|
|
|
// map still rendered
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.querySelector("#mx_Map_mx_BeaconViewDialog")).toBeInTheDocument();
|
2022-07-27 17:42:59 +02:00
|
|
|
// map location unchanged
|
|
|
|
expect(mockMap.setCenter).not.toHaveBeenCalled();
|
|
|
|
expect(mockMap.fitBounds).not.toHaveBeenCalled();
|
2022-04-19 13:35:39 +02:00
|
|
|
});
|
2022-04-20 10:14:24 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("sidebar", () => {
|
|
|
|
it("opens sidebar on view list button click", () => {
|
2022-04-20 10:14:24 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-20 10:14:24 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container, getByTestId } = getComponent();
|
2022-04-20 10:14:24 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
openSidebar(getByTestId);
|
2022-04-20 10:14:24 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.querySelector(".mx_DialogSidebar")).toBeInTheDocument();
|
2022-04-20 10:14:24 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("closes sidebar on close button click", () => {
|
2022-04-20 10:14:24 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2023-02-13 18:01:43 +01:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent))!;
|
2022-04-20 10:14:24 +02:00
|
|
|
beacon.addLocations([location1]);
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container, getByTestId } = getComponent();
|
2022-04-20 10:14:24 +02:00
|
|
|
|
|
|
|
// open the sidebar
|
2023-02-21 22:32:37 +01:00
|
|
|
openSidebar(getByTestId);
|
2022-04-20 10:14:24 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.querySelector(".mx_DialogSidebar")).toBeInTheDocument();
|
2022-04-20 10:14:24 +02:00
|
|
|
|
|
|
|
// now close it
|
2023-02-21 22:32:37 +01:00
|
|
|
fireEvent.click(getByTestId("dialog-sidebar-close"));
|
2022-04-20 10:14:24 +02:00
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
expect(container.querySelector(".mx_DialogSidebar")).not.toBeInTheDocument();
|
2022-04-20 10:14:24 +02:00
|
|
|
});
|
|
|
|
});
|
2022-07-18 10:34:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("focused beacons", () => {
|
|
|
|
const beacon2Event = makeBeaconInfoEvent(bobId, roomId, { isLive: true }, "$bob-room1-2");
|
2022-07-18 10:34:39 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const location2 = makeBeaconEvent(bobId, {
|
|
|
|
beaconInfoId: beacon2Event.getId(),
|
|
|
|
geoUri: "geo:33,22",
|
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
const fitBoundsOptions = { maxZoom: 15, padding: 100 };
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("opens map with both beacons in view on first load without initialFocusedBeacon", () => {
|
2022-07-18 10:34:39 +02:00
|
|
|
const [beacon1, beacon2] = makeRoomWithBeacons(
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId,
|
|
|
|
mockClient,
|
|
|
|
[defaultEvent, beacon2Event],
|
|
|
|
[location1, location2],
|
2022-07-18 10:34:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
getComponent({ beacons: [beacon1, beacon2] });
|
|
|
|
|
|
|
|
// start centered on mid point between both beacons
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 42, lon: 31.5 });
|
|
|
|
// only called once
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledTimes(1);
|
|
|
|
// bounds fit both beacons, only called once
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledWith(
|
|
|
|
new maplibregl.LngLatBounds([22, 33], [41, 51]),
|
|
|
|
fitBoundsOptions,
|
|
|
|
);
|
2022-07-18 10:34:39 +02:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("opens map with both beacons in view on first load with an initially focused beacon", () => {
|
2022-07-18 10:34:39 +02:00
|
|
|
const [beacon1, beacon2] = makeRoomWithBeacons(
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId,
|
|
|
|
mockClient,
|
|
|
|
[defaultEvent, beacon2Event],
|
|
|
|
[location1, location2],
|
2022-07-18 10:34:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
getComponent({ beacons: [beacon1, beacon2], initialFocusedBeacon: beacon1 });
|
|
|
|
|
|
|
|
// start centered on initialFocusedBeacon
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 41 });
|
|
|
|
// only called once
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledTimes(1);
|
|
|
|
// bounds fit both beacons, only called once
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledWith(
|
|
|
|
new maplibregl.LngLatBounds([22, 33], [41, 51]),
|
|
|
|
fitBoundsOptions,
|
|
|
|
);
|
2022-07-18 10:34:39 +02:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("focuses on beacon location on sidebar list item click", () => {
|
2022-07-18 10:34:39 +02:00
|
|
|
const [beacon1, beacon2] = makeRoomWithBeacons(
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId,
|
|
|
|
mockClient,
|
|
|
|
[defaultEvent, beacon2Event],
|
|
|
|
[location1, location2],
|
2022-07-18 10:34:39 +02:00
|
|
|
);
|
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container, getByTestId } = getComponent({ beacons: [beacon1, beacon2] });
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
// reset call counts on map mocks after initial render
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
openSidebar(getByTestId);
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
act(() => {
|
2023-02-21 22:32:37 +01:00
|
|
|
const listItems = container.querySelectorAll(".mx_BeaconListItem");
|
2022-07-18 10:34:39 +02:00
|
|
|
// click on the first beacon in the list
|
2023-02-21 22:32:37 +01:00
|
|
|
fireEvent.click(listItems[0]!);
|
2022-07-18 10:34:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// centered on clicked beacon
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 41 });
|
|
|
|
// only called once
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledTimes(1);
|
|
|
|
// bounds fitted just to clicked beacon
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledWith(
|
|
|
|
new maplibregl.LngLatBounds([41, 51], [41, 51]),
|
|
|
|
fitBoundsOptions,
|
|
|
|
);
|
2022-07-18 10:34:39 +02:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("refocuses on same beacon when clicking list item again", () => {
|
2022-07-18 10:34:39 +02:00
|
|
|
// test the map responds to refocusing the same beacon
|
|
|
|
const [beacon1, beacon2] = makeRoomWithBeacons(
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId,
|
|
|
|
mockClient,
|
|
|
|
[defaultEvent, beacon2Event],
|
|
|
|
[location1, location2],
|
2022-07-18 10:34:39 +02:00
|
|
|
);
|
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
const { container, getByTestId } = getComponent({ beacons: [beacon1, beacon2] });
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
// reset call counts on map mocks after initial render
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
2023-02-21 22:32:37 +01:00
|
|
|
openSidebar(getByTestId);
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// click on the second beacon in the list
|
2023-02-21 22:32:37 +01:00
|
|
|
const listItems = container.querySelectorAll(".mx_BeaconListItem");
|
|
|
|
fireEvent.click(listItems[1]!);
|
2022-07-18 10:34:39 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const expectedBounds = new maplibregl.LngLatBounds([22, 33], [22, 33]);
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
// date is mocked but this relies on timestamp, manually mock a tick
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now + 1);
|
2022-07-18 10:34:39 +02:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// click on the second beacon in the list
|
2023-02-21 22:32:37 +01:00
|
|
|
const listItems = container.querySelectorAll(".mx_BeaconListItem");
|
|
|
|
fireEvent.click(listItems[1]!);
|
2022-07-18 10:34:39 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// centered on clicked beacon
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 33, lon: 22 });
|
|
|
|
// bounds fitted just to clicked beacon
|
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledWith(expectedBounds, fitBoundsOptions);
|
|
|
|
// each called once per click
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledTimes(2);
|
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
});
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|