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