2022-04-11 11:29:24 +03:00
|
|
|
/*
|
2024-09-09 16:57:16 +03:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-04-11 11:29:24 +03:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 16:57:16 +03: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 11:29:24 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-10-14 19:11:58 +03:00
|
|
|
import { render } from "jest-matrix-react";
|
2022-04-11 11:29:24 +03:00
|
|
|
import { mocked } from "jest-mock";
|
2022-12-19 02:17:15 +03:00
|
|
|
import * as maplibregl from "maplibre-gl";
|
2022-04-11 11:29:24 +03:00
|
|
|
|
|
|
|
import SmartMarker from "../../../../src/components/views/location/SmartMarker";
|
|
|
|
|
|
|
|
jest.mock("../../../../src/utils/location/findMapStyleUrl", () => ({
|
|
|
|
findMapStyleUrl: jest.fn().mockReturnValue("tileserver.com"),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe("<SmartMarker />", () => {
|
2022-12-19 02:17:15 +03:00
|
|
|
const mapOptions = { container: {} as unknown as HTMLElement, style: "" };
|
|
|
|
const mockMap = new maplibregl.Map(mapOptions);
|
2022-04-11 11:29:24 +03:00
|
|
|
const mockMarker = new maplibregl.Marker();
|
|
|
|
|
|
|
|
const defaultProps = {
|
|
|
|
map: mockMap,
|
|
|
|
geoUri: "geo:43.2,54.6",
|
|
|
|
};
|
2023-02-20 21:35:39 +03:00
|
|
|
const getComponent = (props = {}): JSX.Element => <SmartMarker {...defaultProps} {...props} />;
|
2022-04-11 11:29:24 +03:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("creates a marker on mount", () => {
|
2023-02-20 21:35:39 +03:00
|
|
|
const { container } = render(getComponent());
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-04-11 11:29:24 +03: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-20 21:35:39 +03:00
|
|
|
const { rerender } = render(getComponent({ geoUri: "geo:40,50" }));
|
2022-04-11 11:29:24 +03:00
|
|
|
|
2023-02-20 21:35:39 +03:00
|
|
|
rerender(getComponent({ geoUri: "geo:41,51" }));
|
|
|
|
rerender(getComponent({ geoUri: "geo:42,52" }));
|
2022-04-11 11:29:24 +03: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-20 21:35:39 +03:00
|
|
|
const { unmount, container } = render(getComponent());
|
|
|
|
expect(container).toMatchSnapshot();
|
2022-04-11 11:29:24 +03:00
|
|
|
|
2023-02-20 21:35:39 +03:00
|
|
|
unmount();
|
2022-04-11 11:29:24 +03:00
|
|
|
expect(mockMarker.remove).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|