2021-12-16 17:20:55 +03: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.
|
|
|
|
*/
|
|
|
|
|
2024-08-01 15:01:05 +03:00
|
|
|
import React from "react";
|
2024-05-02 17:53:35 +03:00
|
|
|
import { fireEvent, render, waitFor } from "@testing-library/react";
|
2023-08-15 18:00:17 +03:00
|
|
|
import { LocationAssetType, ClientEvent, RoomMember, SyncState } from "matrix-js-sdk/src/matrix";
|
2022-12-19 02:17:15 +03:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 14:24:14 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-12-21 18:48:20 +03:00
|
|
|
|
2022-04-11 14:58:35 +03:00
|
|
|
import MLocationBody from "../../../../src/components/views/messages/MLocationBody";
|
2022-03-11 11:52:57 +03:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
|
|
|
import { MediaEventHelper } from "../../../../src/utils/MediaEventHelper";
|
2022-12-12 14:24:14 +03:00
|
|
|
import Modal from "../../../../src/Modal";
|
2022-03-11 11:52:57 +03:00
|
|
|
import SdkConfig from "../../../../src/SdkConfig";
|
2022-12-12 14:24:14 +03:00
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2022-03-25 17:36:22 +03:00
|
|
|
import { makeLocationEvent } from "../../../test-utils/location";
|
2022-12-12 14:24:14 +03:00
|
|
|
import { getMockClientWithEventEmitter } from "../../../test-utils";
|
2021-12-16 17:20:55 +03:00
|
|
|
|
|
|
|
describe("MLocationBody", () => {
|
2022-12-19 02:17:15 +03:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("<MLocationBody>", () => {
|
|
|
|
const roomId = "!room:server";
|
|
|
|
const userId = "@user:server";
|
2022-04-11 19:40:06 +03:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 14:24:14 +03:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-11 19:40:06 +03:00
|
|
|
}),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
});
|
|
|
|
const defaultEvent = makeLocationEvent("geo:51.5076,-0.1276", LocationAssetType.Pin);
|
2024-08-01 15:01:05 +03:00
|
|
|
const defaultProps: MLocationBody["props"] = {
|
2022-04-11 19:40:06 +03:00
|
|
|
mxEvent: defaultEvent,
|
|
|
|
highlights: [],
|
2022-12-12 14:24:14 +03:00
|
|
|
highlightLink: "",
|
2022-04-11 19:40:06 +03:00
|
|
|
onHeightChanged: jest.fn(),
|
|
|
|
onMessageAllowed: jest.fn(),
|
|
|
|
permalinkCreator: {} as RoomPermalinkCreator,
|
|
|
|
mediaEventHelper: {} as MediaEventHelper,
|
|
|
|
};
|
2022-12-12 14:24:14 +03:00
|
|
|
const getComponent = (props = {}) =>
|
2023-03-06 18:17:46 +03:00
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<MLocationBody {...defaultProps} {...props} />
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-06-15 17:38:35 +03:00
|
|
|
const getMapErrorComponent = () => {
|
2022-12-19 02:17:15 +03:00
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-06-15 17:38:35 +03:00
|
|
|
mockClient.getClientWellKnown.mockReturnValue({
|
2022-12-12 14:24:14 +03:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "bad-tile-server.com" },
|
2022-06-15 17:38:35 +03:00
|
|
|
});
|
|
|
|
const component = getComponent();
|
|
|
|
|
|
|
|
// simulate error initialising map in maplibregl
|
|
|
|
// @ts-ignore
|
2022-12-12 14:24:14 +03:00
|
|
|
mockMap.emit("error", { status: 404 });
|
2022-06-15 17:38:35 +03:00
|
|
|
|
|
|
|
return component;
|
|
|
|
};
|
2022-05-23 13:19:59 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("with error", () => {
|
2023-02-13 14:39:16 +03:00
|
|
|
let sdkConfigSpy: jest.SpyInstance<any>;
|
2022-03-11 11:52:57 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// eat expected errors to keep console clean
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-04-11 19:40:06 +03:00
|
|
|
mockClient.getClientWellKnown.mockReturnValue({});
|
2022-12-12 14:24:14 +03:00
|
|
|
sdkConfigSpy = jest.spyOn(SdkConfig, "get").mockReturnValue({});
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
sdkConfigSpy.mockRestore();
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
|
|
|
|
2024-05-02 17:53:35 +03:00
|
|
|
it("displays correct fallback content without error style when map_style_url is not configured", async () => {
|
2022-03-11 11:52:57 +03:00
|
|
|
const component = getComponent();
|
2024-05-02 17:53:35 +03:00
|
|
|
|
|
|
|
// The map code needs to be lazy loaded so this will take some time to appear
|
|
|
|
await waitFor(() =>
|
|
|
|
expect(component.container.querySelector(".mx_EventTile_body")).toBeInTheDocument(),
|
|
|
|
);
|
2023-03-06 18:17:46 +03:00
|
|
|
expect(component.container.querySelector(".mx_EventTile_body")).toMatchSnapshot();
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("displays correct fallback content when map_style_url is misconfigured", () => {
|
2022-06-15 17:38:35 +03:00
|
|
|
const component = getMapErrorComponent();
|
2023-03-06 18:17:46 +03:00
|
|
|
expect(component.container.querySelector(".mx_EventTile_body")).toMatchSnapshot();
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
2022-06-15 17:38:35 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("should clear the error on reconnect", () => {
|
2022-06-15 17:38:35 +03:00
|
|
|
const component = getMapErrorComponent();
|
2023-03-06 18:17:46 +03:00
|
|
|
expect(component.container.querySelector(".mx_EventTile_tileError")).toBeDefined();
|
2022-06-15 17:38:35 +03:00
|
|
|
mockClient.emit(ClientEvent.Sync, SyncState.Reconnecting, SyncState.Error);
|
2023-03-06 18:17:46 +03:00
|
|
|
expect(component.container.querySelector(".mx_EventTile_tileError")).toBeFalsy();
|
2022-06-15 17:38:35 +03:00
|
|
|
});
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
2022-04-11 19:40:06 +03:00
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
describe("without error", () => {
|
2022-04-11 19:40:06 +03:00
|
|
|
beforeEach(() => {
|
|
|
|
mockClient.getClientWellKnown.mockReturnValue({
|
2022-12-12 14:24:14 +03:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-11 19:40:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// MLocationBody uses random number for map id
|
|
|
|
// stabilise for test
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(global.Math, "random").mockReturnValue(0.123456);
|
2022-04-11 19:40:06 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 14:24:14 +03:00
|
|
|
jest.spyOn(global.Math, "random").mockRestore();
|
2022-04-11 19:40:06 +03:00
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders map correctly", () => {
|
2022-12-19 02:17:15 +03:00
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-11 19:40:06 +03:00
|
|
|
const component = getComponent();
|
|
|
|
|
2023-03-06 18:17:46 +03:00
|
|
|
expect(component.asFragment()).toMatchSnapshot();
|
2022-04-11 19:40:06 +03:00
|
|
|
// map was centered
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({
|
2022-12-12 14:24:14 +03:00
|
|
|
lat: 51.5076,
|
|
|
|
lon: -0.1276,
|
2022-04-11 19:40:06 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-06 18:17:46 +03:00
|
|
|
it("opens map dialog on click", async () => {
|
2023-02-16 12:38:44 +03:00
|
|
|
const modalSpy = jest
|
|
|
|
.spyOn(Modal, "createDialog")
|
|
|
|
.mockReturnValue({ finished: new Promise(() => {}), close: jest.fn() });
|
2022-04-11 19:40:06 +03:00
|
|
|
const component = getComponent();
|
|
|
|
|
2023-03-06 18:17:46 +03:00
|
|
|
await fireEvent.click(component.container.querySelector(".mx_Map")!);
|
2022-04-11 19:40:06 +03:00
|
|
|
|
|
|
|
expect(modalSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 14:24:14 +03:00
|
|
|
it("renders marker correctly for a self share", () => {
|
2022-04-11 19:40:06 +03: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-03-06 18:17:46 +03:00
|
|
|
expect(component.asFragment()).toMatchSnapshot();
|
2022-04-11 19:40:06 +03:00
|
|
|
});
|
|
|
|
});
|
2022-03-11 11:52:57 +03:00
|
|
|
});
|
2021-12-16 17:20:55 +03:00
|
|
|
});
|