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";
|
2022-08-02 15:10:43 +02:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 12:24:14 +01:00
|
|
|
import { mount } from "enzyme";
|
2022-12-19 00:17:15 +01:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
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-19 18:47:19 +02:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
|
|
|
makeBeaconEvent,
|
|
|
|
makeBeaconInfoEvent,
|
|
|
|
makeRoomWithStateEvents,
|
2022-12-12 12:24:14 +01:00
|
|
|
} from "../../../test-utils";
|
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2022-04-14 15:14:05 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("<BeaconMarker />", () => {
|
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";
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
const aliceMember = new RoomMember(roomId, aliceId);
|
|
|
|
|
2022-12-19 00:17:15 +01:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
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
|
|
|
}),
|
|
|
|
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-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");
|
|
|
|
const notLiveEvent = makeBeaconInfoEvent(aliceId, roomId, { isLive: false }, "$alice-room1-2");
|
|
|
|
|
|
|
|
const location1 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: defaultEvent.getId(),
|
|
|
|
geoUri: "geo:51,41",
|
|
|
|
timestamp: now + 1,
|
|
|
|
});
|
|
|
|
const location2 = makeBeaconEvent(aliceId, {
|
|
|
|
beaconInfoId: defaultEvent.getId(),
|
|
|
|
geoUri: "geo:52,42",
|
|
|
|
timestamp: now + 10000,
|
|
|
|
});
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
map: mockMap,
|
|
|
|
beacon: new Beacon(defaultEvent),
|
|
|
|
};
|
|
|
|
|
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<BeaconMarker {...defaultProps} {...props} />, {
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: mockClient },
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders nothing when beacon is not live", () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([notLiveEvent]);
|
2022-04-14 15:14:05 +02:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(notLiveEvent));
|
|
|
|
const component = getComponent({ beacon });
|
|
|
|
expect(component.html()).toBe(null);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders nothing when beacon has no location", () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 15:14:05 +02:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
|
|
|
const component = getComponent({ beacon });
|
|
|
|
expect(component.html()).toBe(null);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("renders marker when beacon has location", () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 15:14:05 +02:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
|
|
|
beacon.addLocations([location1]);
|
|
|
|
const component = getComponent({ beacon });
|
|
|
|
expect(component).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("updates with new locations", () => {
|
2022-04-19 18:47:19 +02:00
|
|
|
const room = setupRoom([defaultEvent]);
|
2022-04-14 15:14:05 +02:00
|
|
|
const beacon = room.currentState.beacons.get(getBeaconInfoIdentifier(defaultEvent));
|
|
|
|
beacon.addLocations([location1]);
|
|
|
|
const component = getComponent({ beacon });
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("SmartMarker").props()["geoUri"]).toEqual("geo:51,41");
|
2022-04-14 15:14:05 +02:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
beacon.addLocations([location2]);
|
|
|
|
});
|
|
|
|
component.setProps({});
|
|
|
|
|
|
|
|
// updated to latest location
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(component.find("SmartMarker").props()["geoUri"]).toEqual("geo:52,42");
|
2022-04-14 15:14:05 +02:00
|
|
|
});
|
|
|
|
});
|