2021-12-16 22:20:55 +08:00
|
|
|
/*
|
|
|
|
Copyright 2021 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.
|
|
|
|
*/
|
|
|
|
|
2023-02-13 19:39:16 +08:00
|
|
|
import React, { ComponentProps } from "react";
|
2022-08-02 21:10:43 +08:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-03-11 16:52:57 +08:00
|
|
|
import { mount } from "enzyme";
|
2022-04-11 19:58:35 +08:00
|
|
|
import { LocationAssetType } from "matrix-js-sdk/src/@types/location";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { ClientEvent, RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-12-19 07:17:15 +08:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2023-02-28 16:58:23 +08:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 19:24:14 +08:00
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
import { SyncState } from "matrix-js-sdk/src/sync";
|
2021-12-21 23:48:20 +08:00
|
|
|
|
2022-04-11 19:58:35 +08:00
|
|
|
import MLocationBody from "../../../../src/components/views/messages/MLocationBody";
|
2022-03-11 16:52:57 +08:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2022-12-12 19:24:14 +08:00
|
|
|
import Modal from "../../../../src/Modal";
|
2022-03-11 16:52:57 +08:00
|
|
|
import SdkConfig from "../../../../src/SdkConfig";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2022-03-25 22:36:22 +08:00
|
|
|
import { makeLocationEvent } from "../../../test-utils/location";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
2021-12-16 22:20:55 +08:00
|
|
|
|
|
|
|
describe("MLocationBody", () => {
|
2022-12-19 07:17:15 +08:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("<MLocationBody>", () => {
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const userId = "@user:server";
|
2022-04-12 00:40:06 +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-12 00:40:06 +08:00
|
|
|
}),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
});
|
|
|
|
const defaultEvent = makeLocationEvent("geo:51.5076,-0.1276", LocationAssetType.Pin);
|
2023-02-13 19:39:16 +08:00
|
|
|
const defaultProps: ComponentProps<typeof MLocationBody> = {
|
2022-04-12 00:40:06 +08:00
|
|
|
mxEvent: defaultEvent,
|
|
|
|
highlights: [],
|
2022-12-12 19:24:14 +08:00
|
|
|
highlightLink: "",
|
2022-04-12 00:40:06 +08:00
|
|
|
onHeightChanged: jest.fn(),
|
|
|
|
onMessageAllowed: jest.fn(),
|
|
|
|
permalinkCreator: {} as RoomPermalinkCreator,
|
|
|
|
mediaEventHelper: {} as MediaEventHelper,
|
|
|
|
};
|
2022-12-12 19:24:14 +08:00
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<MLocationBody {...defaultProps} {...props} />, {
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: mockClient },
|
|
|
|
});
|
2022-06-15 22:38:35 +08:00
|
|
|
const getMapErrorComponent = () => {
|
2022-12-19 07:17:15 +08:00
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-06-15 22:38:35 +08:00
|
|
|
mockClient.getClientWellKnown.mockReturnValue({
|
2022-12-12 19:24:14 +08:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "bad-tile-server.com" },
|
2022-06-15 22:38:35 +08:00
|
|
|
});
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
// simulate error initialising map in maplibregl
|
|
|
|
// @ts-ignore
|
2022-12-12 19:24:14 +08:00
|
|
|
mockMap.emit("error", { status: 404 });
|
2022-06-15 22:38:35 +08:00
|
|
|
|
|
|
|
return component;
|
|
|
|
};
|
2022-05-23 18:19:59 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("with error", () => {
|
2023-02-13 19:39:16 +08:00
|
|
|
let sdkConfigSpy: jest.SpyInstance<any>;
|
2022-03-11 16:52:57 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// eat expected errors to keep console clean
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-04-12 00:40:06 +08:00
|
|
|
mockClient.getClientWellKnown.mockReturnValue({});
|
2022-12-12 19:24:14 +08:00
|
|
|
sdkConfigSpy = jest.spyOn(SdkConfig, "get").mockReturnValue({});
|
2022-03-11 16:52:57 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
sdkConfigSpy.mockRestore();
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-11 16:52:57 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("displays correct fallback content without error style when map_style_url is not configured", () => {
|
2022-03-11 16:52:57 +08:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component.find(".mx_EventTile_body")).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("displays correct fallback content when map_style_url is misconfigured", () => {
|
2022-06-15 22:38:35 +08:00
|
|
|
const component = getMapErrorComponent();
|
2022-03-11 16:52:57 +08:00
|
|
|
component.setProps({});
|
|
|
|
expect(component.find(".mx_EventTile_body")).toMatchSnapshot();
|
|
|
|
});
|
2022-06-15 22:38:35 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("should clear the error on reconnect", () => {
|
2022-06-15 22:38:35 +08:00
|
|
|
const component = getMapErrorComponent();
|
|
|
|
expect((component.state() as React.ComponentState).error).toBeDefined();
|
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Reconnecting, SyncState.Error);
|
|
|
|
expect((component.state() as React.ComponentState).error).toBeUndefined();
|
|
|
|
});
|
2022-03-11 16:52:57 +08:00
|
|
|
});
|
2022-04-12 00:40:06 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("without error", () => {
|
2022-04-12 00:40:06 +08:00
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.getClientWellKnown.mockReturnValue({
|
2022-12-12 19:24:14 +08:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
// MLocationBody uses random number for map id
|
|
|
|
// stabilise for test
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(global.Math, "random").mockReturnValue(0.123456);
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 19:24:14 +08:00
|
|
|
jest.spyOn(global.Math, "random").mockRestore();
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders map correctly", () => {
|
2022-12-19 07:17:15 +08:00
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-12 00:40:06 +08:00
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
expect(component).toMatchSnapshot();
|
|
|
|
// map was centered
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({
|
2022-12-12 19:24:14 +08:00
|
|
|
lat: 51.5076,
|
|
|
|
lon: -0.1276,
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("opens map dialog on click", () => {
|
2023-02-16 17:38:44 +08:00
|
|
|
const modalSpy = jest
|
|
|
|
.spyOn(Modal, "createDialog")
|
|
|
|
.mockReturnValue({ finished: new Promise(() => {}), close: jest.fn() });
|
2022-04-12 00:40:06 +08:00
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 19:24:14 +08:00
|
|
|
component.find("Map").at(0).simulate("click");
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(modalSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders marker correctly for a non-self share", () => {
|
2022-12-19 07:17:15 +08:00
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-12 00:40:06 +08:00
|
|
|
const component = getComponent();
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(component.find("SmartMarker").at(0).props()).toEqual(
|
2022-04-12 00:40:06 +08:00
|
|
|
expect.objectContaining({
|
|
|
|
map: mockMap,
|
2022-12-12 19:24:14 +08:00
|
|
|
geoUri: "geo:51.5076,-0.1276",
|
2022-04-12 00:40:06 +08:00
|
|
|
roomMember: undefined,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("renders marker correctly for a self share", () => {
|
2022-04-12 00:40:06 +08:00
|
|
|
const selfShareEvent = makeLocationEvent("geo:51.5076,-0.1276", LocationAssetType.Self);
|
|
|
|
const member = new RoomMember(roomId, userId);
|
|
|
|
// @ts-ignore cheat assignment to property
|
|
|
|
selfShareEvent.sender = member;
|
|
|
|
const component = getComponent({ mxEvent: selfShareEvent });
|
|
|
|
|
|
|
|
// render self locations with user avatars
|
2023-02-13 19:39:16 +08:00
|
|
|
expect(component.find("SmartMarker").at(0).prop("roomMember")).toEqual(member);
|
2022-04-12 00:40:06 +08:00
|
|
|
});
|
|
|
|
});
|
2022-03-11 16:52:57 +08:00
|
|
|
});
|
2021-12-16 22:20:55 +08:00
|
|
|
});
|