2021-12-17 10:58:24 +00: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.
|
|
|
|
*/
|
2022-03-11 08:52:57 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React from "react";
|
2023-02-22 10:52:55 +00:00
|
|
|
import { act, fireEvent, render, RenderResult } from "@testing-library/react";
|
2022-12-18 23:17:15 +00:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-03-09 17:14:07 +00:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-12-17 10:58:24 +00:00
|
|
|
|
2022-03-23 17:08:56 +00:00
|
|
|
import LocationPicker from "../../../../src/components/views/location/LocationPicker";
|
2022-03-09 17:14:07 +00:00
|
|
|
import { LocationShareType } from "../../../../src/components/views/location/shareLocation";
|
2022-12-12 11:24:14 +00:00
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2023-02-20 18:35:39 +00:00
|
|
|
import { getMockClientWithEventEmitter, mockPlatformPeg } from "../../../test-utils";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { findMapStyleUrl, LocationShareError } from "../../../../src/utils/location";
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("../../../../src/utils/location/findMapStyleUrl", () => ({
|
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue("tileserver.com"),
|
2022-03-09 17:14:07 +00:00
|
|
|
}));
|
2021-12-17 10:58:24 +00:00
|
|
|
|
2022-03-21 11:42:58 +00:00
|
|
|
// dropdown uses this
|
|
|
|
mockPlatformPeg({ overrideBrowserShortcuts: jest.fn().mockReturnValue(false) });
|
|
|
|
|
2021-12-17 10:58:24 +00:00
|
|
|
describe("LocationPicker", () => {
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("<LocationPicker />", () => {
|
|
|
|
const roomId = "!room:server.org";
|
|
|
|
const userId = "@user:server.org";
|
2022-03-09 17:14:07 +00:00
|
|
|
const sender = new RoomMember(roomId, userId);
|
|
|
|
const defaultProps = {
|
|
|
|
sender,
|
|
|
|
shareType: LocationShareType.Own,
|
|
|
|
onChoose: jest.fn(),
|
|
|
|
onFinished: jest.fn(),
|
|
|
|
};
|
2023-02-20 18:35:39 +00:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
2022-03-09 17:14:07 +00:00
|
|
|
isGuest: jest.fn(),
|
|
|
|
getClientWellKnown: jest.fn(),
|
2023-02-20 18:35:39 +00:00
|
|
|
});
|
|
|
|
const getComponent = (props = {}): RenderResult =>
|
|
|
|
render(<LocationPicker {...defaultProps} {...props} />, {
|
|
|
|
wrapper: ({ children }) => (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>{children}</MatrixClientContext.Provider>
|
|
|
|
),
|
2022-12-12 11:24:14 +00:00
|
|
|
});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2022-12-18 23:17:15 +00:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
|
|
|
const mockGeolocate = new maplibregl.GeolocateControl({});
|
2022-03-09 17:14:07 +00:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
|
|
|
|
|
|
|
const mockGeolocationPosition = {
|
|
|
|
coords: {
|
|
|
|
latitude: 43.2,
|
|
|
|
longitude: 12.4,
|
|
|
|
altitude: 12.3,
|
|
|
|
accuracy: 21,
|
|
|
|
},
|
|
|
|
timestamp: 123,
|
|
|
|
};
|
|
|
|
const mockClickEvent = {
|
|
|
|
lngLat: {
|
|
|
|
lat: 43.2,
|
|
|
|
lng: 12.4,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient as unknown as MatrixClient);
|
2022-03-09 17:14:07 +00:00
|
|
|
jest.clearAllMocks();
|
|
|
|
mocked(mockMap).addControl.mockReset();
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(findMapStyleUrl).mockReturnValue("tileserver.com");
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("displays error when map emits an error", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
// suppress expected error log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByTestId, getByText } = getComponent();
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).emit("error", { error: "Something went wrong" });
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByTestId("map-rendering-error")).toBeInTheDocument();
|
|
|
|
expect(
|
|
|
|
getByText(
|
|
|
|
"This homeserver is not configured correctly to display maps, " +
|
|
|
|
"or the configured map server may be unreachable.",
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
2022-03-11 08:52:57 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("displays error when map display is not configured properly", () => {
|
2022-03-11 08:52:57 +00:00
|
|
|
// suppress expected error log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-11 08:52:57 +00:00
|
|
|
mocked(findMapStyleUrl).mockImplementation(() => {
|
|
|
|
throw new Error(LocationShareError.MapStyleUrlNotConfigured);
|
|
|
|
});
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-03-11 08:52:57 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByText("This homeserver is not configured to display maps.")).toBeInTheDocument();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("displays error when map setup throws", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
// suppress expected error log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
// throw an error
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).addControl.mockImplementation(() => {
|
|
|
|
throw new Error("oups");
|
|
|
|
});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent();
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(
|
|
|
|
getByText(
|
|
|
|
"This homeserver is not configured correctly to display maps, " +
|
|
|
|
"or the configured map server may be unreachable.",
|
|
|
|
),
|
|
|
|
).toBeInTheDocument();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("initiates map with geolocation", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
getComponent();
|
|
|
|
|
|
|
|
expect(mockMap.addControl).toHaveBeenCalledWith(mockGeolocate);
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).emit("load");
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockGeolocate.trigger).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-03-17 08:26:47 +00:00
|
|
|
const testUserLocationShareTypes = (shareType: LocationShareType.Own | LocationShareType.Live) => {
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("user location behaviours", () => {
|
|
|
|
it("closes and displays error when geolocation errors", () => {
|
2022-03-17 08:26:47 +00:00
|
|
|
// suppress expected error log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-17 08:26:47 +00:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
getComponent({ onFinished, shareType });
|
|
|
|
|
|
|
|
expect(mockMap.addControl).toHaveBeenCalledWith(mockGeolocate);
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mockMap.emit("load");
|
2022-03-17 08:26:47 +00:00
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mockGeolocate.emit("error", {});
|
2022-03-17 08:26:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// dialog is closed on error
|
|
|
|
expect(onFinished).toHaveBeenCalled();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets position on geolocate event", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { container, getByTestId } = getComponent({ shareType });
|
2022-03-17 08:26:47 +00:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockGeolocate).emit("geolocate", mockGeolocationPosition);
|
2022-03-17 08:26:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// marker added
|
|
|
|
expect(maplibregl.Marker).toHaveBeenCalled();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockMarker.setLngLat).toHaveBeenCalledWith(new maplibregl.LngLat(12.4, 43.2));
|
2022-03-17 08:26:47 +00:00
|
|
|
// submit button is enabled when position is truthy
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByTestId("location-picker-submit-button")).not.toBeDisabled();
|
|
|
|
expect(container.querySelector(".mx_BaseAvatar")).toBeInTheDocument();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("disables submit button until geolocation completes", () => {
|
2022-05-27 07:13:50 +00:00
|
|
|
const onChoose = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByTestId } = getComponent({ shareType, onChoose });
|
2022-05-27 07:13:50 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
// button is disabled
|
|
|
|
expect(getByTestId("location-picker-submit-button")).toBeDisabled();
|
|
|
|
fireEvent.click(getByTestId("location-picker-submit-button"));
|
2022-05-27 07:13:50 +00:00
|
|
|
// nothing happens on button click
|
|
|
|
expect(onChoose).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockGeolocate).emit("geolocate", mockGeolocationPosition);
|
2022-05-27 07:13:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// submit button is enabled when position is truthy
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByTestId("location-picker-submit-button")).not.toBeDisabled();
|
2022-05-27 07:13:50 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("submits location", () => {
|
2022-03-17 08:26:47 +00:00
|
|
|
const onChoose = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByTestId } = getComponent({ onChoose, shareType });
|
2022-03-17 08:26:47 +00:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockGeolocate).emit("geolocate", mockGeolocationPosition);
|
2022-03-17 08:26:47 +00:00
|
|
|
// make sure button is enabled
|
|
|
|
});
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByTestId("location-picker-submit-button"));
|
2022-03-17 08:26:47 +00:00
|
|
|
// content of this call is tested in LocationShareMenu-test
|
|
|
|
expect(onChoose).toHaveBeenCalled();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-17 08:26:47 +00:00
|
|
|
};
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("for Own location share type", () => {
|
2022-03-21 11:42:58 +00:00
|
|
|
testUserLocationShareTypes(LocationShareType.Own);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("for Live location share type", () => {
|
2022-03-21 11:42:58 +00:00
|
|
|
const shareType = LocationShareType.Live;
|
|
|
|
testUserLocationShareTypes(shareType);
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders live duration dropdown with default option", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText } = getComponent({ shareType });
|
|
|
|
expect(getByText("Share for 15m")).toBeInTheDocument();
|
2022-03-21 11:42:58 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates selected duration", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByText, getByLabelText } = getComponent({ shareType });
|
2022-03-21 11:42:58 +00:00
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
// open dropdown
|
|
|
|
fireEvent.click(getByLabelText("Share for 15m"));
|
|
|
|
|
|
|
|
fireEvent.click(getByText("Share for 1h"));
|
2022-03-21 11:42:58 +00:00
|
|
|
|
|
|
|
// value updated
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(getByText("Share for 1h")).toMatchSnapshot();
|
2022-03-21 11:42:58 +00:00
|
|
|
});
|
|
|
|
});
|
2022-03-09 17:14:07 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("for Pin drop location share type", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
const shareType = LocationShareType.Pin;
|
2022-12-12 11:24:14 +00:00
|
|
|
it("initiates map with geolocation", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
getComponent({ shareType });
|
|
|
|
|
|
|
|
expect(mockMap.addControl).toHaveBeenCalledWith(mockGeolocate);
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).emit("load");
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockGeolocate.trigger).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("removes geolocation control on geolocation error", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
// suppress expected error log
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockImplementation(() => {});
|
2022-03-09 17:14:07 +00:00
|
|
|
const onFinished = jest.fn();
|
|
|
|
getComponent({ onFinished, shareType });
|
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mockMap.emit("load");
|
2022-03-09 17:14:07 +00:00
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mockGeolocate.emit("error", {});
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockMap.removeControl).toHaveBeenCalledWith(mockGeolocate);
|
|
|
|
// dialog is not closed
|
|
|
|
expect(onFinished).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not set position on geolocate event", () => {
|
2022-03-21 11:42:58 +00:00
|
|
|
mocked(maplibregl.Marker).mockClear();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { container } = getComponent({ shareType });
|
2022-03-09 17:14:07 +00:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockGeolocate).emit("geolocate", mockGeolocationPosition);
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-03-21 11:42:58 +00:00
|
|
|
// marker not added
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(container.querySelector("mx_Marker")).not.toBeInTheDocument();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets position on click event", () => {
|
2023-02-20 18:35:39 +00:00
|
|
|
const { container } = getComponent({ shareType });
|
2022-03-09 17:14:07 +00:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).emit("click", mockClickEvent);
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// marker added
|
|
|
|
expect(maplibregl.Marker).toHaveBeenCalled();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockMarker.setLngLat).toHaveBeenCalledWith(new maplibregl.LngLat(12.4, 43.2));
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
// marker is set, icon not avatar
|
2023-02-20 18:35:39 +00:00
|
|
|
expect(container.querySelector(".mx_Marker_icon")).toBeInTheDocument();
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("submits location", () => {
|
2022-03-09 17:14:07 +00:00
|
|
|
const onChoose = jest.fn();
|
2023-02-20 18:35:39 +00:00
|
|
|
const { getByTestId } = getComponent({ onChoose, shareType });
|
2022-03-09 17:14:07 +00:00
|
|
|
act(() => {
|
|
|
|
// @ts-ignore
|
2022-12-12 11:24:14 +00:00
|
|
|
mocked(mockMap).emit("click", mockClickEvent);
|
2022-03-09 17:14:07 +00:00
|
|
|
});
|
|
|
|
|
2023-02-20 18:35:39 +00:00
|
|
|
fireEvent.click(getByTestId("location-picker-submit-button"));
|
2022-03-09 17:14:07 +00:00
|
|
|
|
|
|
|
// content of this call is tested in LocationShareMenu-test
|
|
|
|
expect(onChoose).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-12-17 10:58:24 +00:00
|
|
|
});
|