2022-04-11 16:40:06 +00: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 11:24:14 +00:00
|
|
|
import React from "react";
|
2022-08-02 13:10:43 +00:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-12-12 11:24:14 +00:00
|
|
|
import { mount } from "enzyme";
|
|
|
|
import { act } from "react-dom/test-utils";
|
2022-12-18 23:17:15 +00:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-12-12 11:24:14 +00:00
|
|
|
import { ClientEvent } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
|
|
|
|
|
|
|
import Map from "../../../../src/components/views/location/Map";
|
|
|
|
import { findByTestId, getMockClientWithEventEmitter } from "../../../test-utils";
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
|
|
|
|
|
|
|
describe("<Map />", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
const defaultProps = {
|
2022-12-12 11:24:14 +00:00
|
|
|
centerGeoUri: "geo:52,41",
|
|
|
|
id: "test-123",
|
2022-04-11 16:40:06 +00:00
|
|
|
onError: jest.fn(),
|
|
|
|
onClick: jest.fn(),
|
|
|
|
};
|
|
|
|
const matrixClient = getMockClientWithEventEmitter({
|
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-11 16:40:06 +00:00
|
|
|
}),
|
|
|
|
});
|
|
|
|
const getComponent = (props = {}) =>
|
|
|
|
mount(<Map {...defaultProps} {...props} />, {
|
|
|
|
wrappingComponent: MatrixClientContext.Provider,
|
|
|
|
wrappingComponentProps: { value: matrixClient },
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
matrixClient.getClientWellKnown.mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(logger, "error").mockRestore();
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-18 23:17:15 +00:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-11 16:40:06 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
const component = getComponent();
|
|
|
|
expect(component).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("onClientWellKnown emits", () => {
|
|
|
|
it("updates map style when style url is truthy", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
getComponent();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
matrixClient.emit(ClientEvent.ClientWellKnown, {
|
2022-12-12 11:24:14 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "new.maps.com" },
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockMap.setStyle).toHaveBeenCalledWith("new.maps.com");
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("does not update map style when style url is truthy", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
getComponent();
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
matrixClient.emit(ClientEvent.ClientWellKnown, {
|
2022-04-14 13:14:05 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: undefined },
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(mockMap.setStyle).not.toHaveBeenCalledWith();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("map centering", () => {
|
|
|
|
it("does not try to center when no center uri provided", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
getComponent({ centerGeoUri: null });
|
|
|
|
expect(mockMap.setCenter).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("sets map center to centerGeoUri", () => {
|
|
|
|
getComponent({ centerGeoUri: "geo:51,42" });
|
2022-04-11 16:40:06 +00:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 42 });
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("handles invalid centerGeoUri", () => {
|
|
|
|
const logSpy = jest.spyOn(logger, "error").mockImplementation();
|
|
|
|
getComponent({ centerGeoUri: "123 Sesame Street" });
|
2022-04-11 16:40:06 +00:00
|
|
|
expect(mockMap.setCenter).not.toHaveBeenCalled();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(logSpy).toHaveBeenCalledWith("Could not set map center");
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates map center when centerGeoUri prop changes", () => {
|
|
|
|
const component = getComponent({ centerGeoUri: "geo:51,42" });
|
2022-04-11 16:40:06 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
component.setProps({ centerGeoUri: "geo:53,45" });
|
|
|
|
component.setProps({ centerGeoUri: "geo:56,47" });
|
2022-04-11 16:40:06 +00:00
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 51, lon: 42 });
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 53, lon: 45 });
|
|
|
|
expect(mockMap.setCenter).toHaveBeenCalledWith({ lat: 56, lon: 47 });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("map bounds", () => {
|
|
|
|
it("does not try to fit map bounds when no bounds provided", () => {
|
2022-04-19 11:35:39 +00:00
|
|
|
getComponent({ bounds: null });
|
|
|
|
expect(mockMap.fitBounds).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("fits map to bounds", () => {
|
2022-04-19 11:35:39 +00:00
|
|
|
const bounds = { north: 51, south: 50, east: 42, west: 41 };
|
|
|
|
getComponent({ bounds });
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledWith(
|
|
|
|
new maplibregl.LngLatBounds([bounds.west, bounds.south], [bounds.east, bounds.north]),
|
|
|
|
{ padding: 100, maxZoom: 15 },
|
|
|
|
);
|
2022-04-19 11:35:39 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("handles invalid bounds", () => {
|
|
|
|
const logSpy = jest.spyOn(logger, "error").mockImplementation();
|
|
|
|
const bounds = { north: "a", south: "b", east: 42, west: 41 };
|
2022-04-19 11:35:39 +00:00
|
|
|
getComponent({ bounds });
|
|
|
|
expect(mockMap.fitBounds).not.toHaveBeenCalled();
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(logSpy).toHaveBeenCalledWith("Invalid map bounds");
|
2022-04-19 11:35:39 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("updates map bounds when bounds prop changes", () => {
|
|
|
|
const component = getComponent({ centerGeoUri: "geo:51,42" });
|
2022-04-19 11:35:39 +00:00
|
|
|
|
|
|
|
const bounds = { north: 51, south: 50, east: 42, west: 41 };
|
|
|
|
const bounds2 = { north: 53, south: 51, east: 45, west: 44 };
|
|
|
|
component.setProps({ bounds });
|
|
|
|
component.setProps({ bounds: bounds2 });
|
|
|
|
expect(mockMap.fitBounds).toHaveBeenCalledTimes(2);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("children", () => {
|
|
|
|
it("renders without children", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
const component = getComponent({ children: null });
|
|
|
|
|
|
|
|
component.setProps({});
|
|
|
|
|
|
|
|
// no error
|
|
|
|
expect(component).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("renders children with map renderProp", () => {
|
|
|
|
const children = ({ map }) => (
|
|
|
|
<div data-test-id="test-child" data-map={map}>
|
|
|
|
Hello, world
|
|
|
|
</div>
|
|
|
|
);
|
2022-04-11 16:40:06 +00:00
|
|
|
|
|
|
|
const component = getComponent({ children });
|
|
|
|
|
|
|
|
// renders child with map instance
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(findByTestId(component, "test-child").props()["data-map"]).toEqual(mockMap);
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("onClick", () => {
|
|
|
|
it("eats clicks to maplibre attribution button", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
const onClick = jest.fn();
|
|
|
|
const component = getComponent({ onClick });
|
|
|
|
|
|
|
|
act(() => {
|
|
|
|
// this is added to the dom by maplibregl
|
|
|
|
// which is mocked
|
|
|
|
// just fake the target
|
2022-12-12 11:24:14 +00:00
|
|
|
const fakeEl = document.createElement("div");
|
|
|
|
fakeEl.className = "maplibregl-ctrl-attrib-button";
|
|
|
|
component.simulate("click", { target: fakeEl });
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(onClick).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("calls onClick", () => {
|
2022-04-11 16:40:06 +00:00
|
|
|
const onClick = jest.fn();
|
|
|
|
const component = getComponent({ onClick });
|
|
|
|
|
|
|
|
act(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
component.simulate("click");
|
2022-04-11 16:40:06 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(onClick).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|