2022-04-14 21:14:05 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-14 21:14:05 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08: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-14 21:14:05 +08:00
|
|
|
*/
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
import React from "react";
|
2024-05-02 22:53:35 +08:00
|
|
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
2022-12-19 07:17:15 +08:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { Beacon, Room, RoomMember, MatrixEvent, getBeaconInfoIdentifier } from "matrix-js-sdk/src/matrix";
|
|
|
|
|
|
|
|
import BeaconMarker from "../../../../src/components/views/beacon/BeaconMarker";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-04-20 00:47:19 +08:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
makeBeaconEvent,
|
|
|
|
makeBeaconInfoEvent,
|
|
|
|
makeRoomWithStateEvents,
|
2022-12-12 19:24:14 +08:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2022-04-14 21:14:05 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("<BeaconMarker />", () => {
|
2022-04-14 21:14:05 +08:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
// stable date for snapshots
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(global.Date, "now").mockReturnValue(now);
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const aliceId = "@alice:server";
|
2022-04-14 21:14:05 +08:00
|
|
|
|
|
|
|
const aliceMember = new RoomMember(roomId, aliceId);
|
|
|
|
|
2022-12-19 07:17:15 +08:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2023-01-04 18:00:13 +08:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
2022-04-14 21:14:05 +08:00
|
|
|
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 19:24:14 +08:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-14 21:14:05 +08:00
|
|
|
}),
|
|
|
|
getUserId: jest.fn().mockReturnValue(aliceId),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
});
|
|
|
|
|
|
|
|
// make fresh rooms every time
|
|
|
|
// as we update room state
|
2022-04-20 00:47:19 +08:00
|
|
|
const setupRoom = (stateEvents: MatrixEvent[] = []): Room => {
|
|
|
|
const room1 = makeRoomWithStateEvents(stateEvents, { roomId, mockClient });
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(room1, "getMember").mockReturnValue(aliceMember);
|
2022-04-14 21:14:05 +08:00
|
|
|
return room1;
|
|
|
|
};
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
const defaultEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: true }, "$alice-room1-1");
|
|
|
|
const notLiveEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false }, "$alice-room1-2");
|
|
|
|
|
2023-01-04 18:00:13 +08:00
|
|
|
const geoUri1 = "geo:51,41";
|
2022-12-12 19:24:14 +08:00
|
|
|
const location1 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: defaultEvent.getId(),
|
2023-01-04 18:00:13 +08:00
|
|
|
geoUri: geoUri1,
|
2022-12-12 19:24:14 +08:00
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
2023-01-04 18:00:13 +08:00
|
|
|
const geoUri2 = "geo:52,42";
|
2022-12-12 19:24:14 +08:00
|
|
|
const location2 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: defaultEvent.getId(),
|
2023-01-04 18:00:13 +08:00
|
|
|
geoUri: geoUri2,
|
2022-12-12 19:24:14 +08:00
|
|
|
timestamp: now + 10000,
|
|
|
|
});
|
2022-04-14 21:14:05 +08:00
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
map: mockMap,
|
|
|
|
beacon: new Beacon(defaultEvent),
|
|
|
|
};
|
|
|
|
|
2023-01-04 18:00:13 +08:00
|
|
|
const renderComponent = (props = {}) => {
|
|
|
|
const Wrapper = (wrapperProps = {}) => {
|
|
|
|
return <MatrixClientContext.Provider value={mockClient} {...wrapperProps} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
return render(<BeaconMarker {...defaultProps} {...props} />, {
|
|
|
|
wrapper: Wrapper,
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
2023-01-04 18:00:13 +08:00
|
|
|
};
|
2022-04-14 21:14:05 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders nothing when beacon is not live", () => {
|
2022-04-20 00:47:19 +08:00
|
|
|
const room = setupRoom([notLiveEvent]);
|
2022-04-14 21:14:05 +08:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(notLiveEvent));
|
2023-01-04 18:00:13 +08:00
|
|
|
const { asFragment } = renderComponent({ beacon });
|
|
|
|
expect(asFragment()).toMatchInlineSnapshot(`<DocumentFragment />`);
|
|
|
|
expect(screen.queryByTestId("avatar-img")).not.toBeInTheDocument();
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders nothing when beacon has no location", () => {
|
2022-04-20 00:47:19 +08:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 21:14:05 +08:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
2023-01-04 18:00:13 +08:00
|
|
|
const { asFragment } = renderComponent({ beacon });
|
|
|
|
expect(asFragment()).toMatchInlineSnapshot(`<DocumentFragment />`);
|
|
|
|
expect(screen.queryByTestId("avatar-img")).not.toBeInTheDocument();
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
|
|
|
|
2024-05-02 22:53:35 +08:00
|
|
|
it("renders marker when beacon has location", async () => {
|
2022-04-20 00:47:19 +08:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 21:14:05 +08:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
2023-01-04 18:00:13 +08:00
|
|
|
beacon?.addLocations([location1]);
|
|
|
|
const { asFragment } = renderComponent({ beacon });
|
2024-05-02 22:53:35 +08:00
|
|
|
await waitFor(() => {
|
|
|
|
expect(screen.getByTestId("avatar-img")).toBeInTheDocument();
|
|
|
|
});
|
2023-01-04 18:00:13 +08:00
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("updates with new locations", () => {
|
2023-01-04 18:00:13 +08:00
|
|
|
const lonLat1 = { lon: 41, lat: 51 };
|
|
|
|
const lonLat2 = { lon: 42, lat: 52 };
|
2022-04-20 00:47:19 +08:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 21:14:05 +08:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
2023-01-04 18:00:13 +08:00
|
|
|
beacon?.addLocations([location1]);
|
2022-04-14 21:14:05 +08:00
|
|
|
|
2023-01-04 18:00:13 +08:00
|
|
|
// render the component then add a new location, check mockMarker called as expected
|
|
|
|
renderComponent({ beacon });
|
|
|
|
expect(mockMarker.setLngLat).toHaveBeenLastCalledWith(lonLat1);
|
|
|
|
expect(mockMarker.addTo).toHaveBeenCalledWith(mockMap);
|
|
|
|
|
|
|
|
// add a location, check mockMarker called with new location details
|
2022-04-14 21:14:05 +08:00
|
|
|
act(() => {
|
2023-01-04 18:00:13 +08:00
|
|
|
beacon?.addLocations([location2]);
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
2023-01-04 18:00:13 +08:00
|
|
|
expect(mockMarker.setLngLat).toHaveBeenLastCalledWith(lonLat2);
|
|
|
|
expect(mockMarker.addTo).toHaveBeenCalledWith(mockMap);
|
2022-04-14 21:14:05 +08:00
|
|
|
});
|
|
|
|
});
|