2022-04-11 16:29:24 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-11 16:29:24 +08:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-04-11 16:29:24 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-10-15 00:11:58 +08:00
|
|
|
import { render } from "jest-matrix-react";
|
2022-04-11 16:29:24 +08:00
|
|
|
import { mocked } from "jest-mock";
|
2022-12-19 07:17:15 +08:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-04-11 16:29:24 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
import SmartMarker from "../../../../../src/components/views/location/SmartMarker";
|
2022-04-11 16:29:24 +08:00
|
|
|
|
2024-10-15 21:57:26 +08:00
|
|
|
jest.mock("../../../../../src/utils/location/findMapStyleUrl", () => ({
|
2022-04-11 16:29:24 +08:00
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue("tileserver.com"),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("<SmartMarker />", () => {
|
2022-12-19 07:17:15 +08:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-11 16:29:24 +08:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
map: mockMap,
|
|
|
|
geoUri: "geo:43.2,54.6",
|
|
|
|
};
|
2023-02-21 02:35:39 +08:00
|
|
|
const getComponent = (props = {}): JSX.Element => <SmartMarker {...defaultProps} {...props} />;
|
2022-04-11 16:29:24 +08:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates a marker on mount", () => {
|
2023-02-21 02:35:39 +08:00
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-04-11 16:29:24 +08:00
|
|
|
|
|
|
|
// marker added only once
|
|
|
|
expect(maplibregl.Marker).toHaveBeenCalledTimes(1);
|
|
|
|
// set to correct position
|
|
|
|
expect(mockMarker.setLngLat).toHaveBeenCalledWith({ lon: 54.6, lat: 43.2 });
|
|
|
|
// added to map
|
|
|
|
expect(mockMarker.addTo).toHaveBeenCalledWith(mockMap);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates marker position on change", () => {
|
2023-02-21 02:35:39 +08:00
|
|
|
const { rerender } = render(getComponent({ geoUri: "geo:40,50" }));
|
2022-04-11 16:29:24 +08:00
|
|
|
|
2023-02-21 02:35:39 +08:00
|
|
|
rerender(getComponent({ geoUri: "geo:41,51" }));
|
|
|
|
rerender(getComponent({ geoUri: "geo:42,52" }));
|
2022-04-11 16:29:24 +08:00
|
|
|
|
|
|
|
// marker added only once
|
|
|
|
expect(maplibregl.Marker).toHaveBeenCalledTimes(1);
|
|
|
|
// set positions
|
|
|
|
expect(mocked(mockMarker.setLngLat)).toHaveBeenCalledWith({ lat: 40, lon: 50 });
|
|
|
|
expect(mocked(mockMarker.setLngLat)).toHaveBeenCalledWith({ lat: 41, lon: 51 });
|
|
|
|
expect(mocked(mockMarker.setLngLat)).toHaveBeenCalledWith({ lat: 42, lon: 52 });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes marker on unmount", () => {
|
2023-02-21 02:35:39 +08:00
|
|
|
const { unmount, container } = render(getComponent());
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-04-11 16:29:24 +08:00
|
|
|
|
2023-02-21 02:35:39 +08:00
|
|
|
unmount();
|
2022-04-11 16:29:24 +08:00
|
|
|
expect(mockMarker.remove).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|