@panoramax/web-viewer
Version:
Panoramax web viewer for geolocated pictures
139 lines (117 loc) • 4.6 kB
JavaScript
import * as map from "../../src/utils/map";
describe("getThumbGif", () => {
it("works", () => {
const lang = { loading: "Loading..." };
const res = map.getThumbGif(lang);
expect(res).toBeDefined();
expect(res.tagName).toBe("IMG");
expect(res.alt).toBe(lang.loading);
expect(res.title).toBe(lang.loading);
});
});
describe("isNullCoordinates", () => {
it.each([
[undefined, true],
[null, true],
[[0,0], true],
[[48.7, -1.5], false],
[[], true],
])("works with %s", (input, expected) => {
expect(map.isNullCoordinates(input)).toBe(expected);
});
});
describe("isLabelLayer", () => {
it("works with just text-field", () => {
const layer = { type: "symbol", layout: { "text-field": "Label" } };
expect(map.isLabelLayer(layer)).toBe(true);
});
it("works with text-field + minzoom < 15", () => {
const layer = { type: "symbol", layout: { "text-field": "Label" }, minzoom: 10 };
expect(map.isLabelLayer(layer)).toBe(true);
});
it("works with text-field + minzoom >= 15", () => {
const layer = { type: "symbol", layout: { "text-field": "Label" }, minzoom: 15 };
expect(map.isLabelLayer(layer)).toBe(false);
});
it("works with non-symbol layer", () => {
const layer = { type: "fill", layout: { "text-field": "Label" } };
expect(map.isLabelLayer(layer)).toBe(false);
});
it("works without text-field", () => {
const layer = { type: "symbol", layout: {} };
expect(map.isLabelLayer(layer)).toBe(false);
});
});
describe("getUserLayerId", () => {
it("works with default tiles", () => {
expect(map.getUserLayerId("geovisio", "pictures")).toBe("geovisio_pictures");
});
it("works with specific user tiles", () => {
expect(map.getUserLayerId("toto", "pictures")).toBe("geovisio_toto_pictures");
});
});
describe("getUserSourceId", () => {
it("works with default tiles", () => {
expect(map.getUserSourceId("geovisio")).toBe("geovisio");
});
it("works with specific user tiles", () => {
expect(map.getUserSourceId("toto")).toBe("geovisio_toto");
});
});
describe("switchCoefValue", () => {
it("works", () => {
const l = {id: "bla", paint: { "circle-radius": ["bla", ["get", "coef"]]}, layout: {"circle-sort": "coef"}};
const res = map.switchCoefValue(l, "coef_360");
expect(res).toEqual({id: "bla", paint: { "circle-radius": ["bla", ["get", "coef_360"]]}, layout: {"circle-sort": "coef_360"}})
});
});
Object.defineProperty(window, "localStorage", { value: {
setItem: jest.fn(),
getItem: jest.fn(),
} });
describe("getMapParamsFromLocalStorage", () => {
it("should return an empty object if no params are stored", () => {
localStorage.getItem.mockReturnValue(null);
const result = map.getMapParamsFromLocalStorage();
expect(result).toEqual({});
});
it("should return parsed params if they exist in localStorage", () => {
const params = { center: { lat: "48.8566000", lng: "2.3522000" }, zoom: "10.0", background: "aerial", theme: "age" };
localStorage.getItem.mockReturnValue(JSON.stringify(params));
const result = map.getMapParamsFromLocalStorage();
expect(localStorage.getItem.mock.calls).toMatchSnapshot();
expect(result).toEqual(params);
});
it("should return an empty object if JSON parsing fails", () => {
console.warn = jest.fn();
localStorage.getItem.mockReturnValue("invalid-json");
const result = map.getMapParamsFromLocalStorage();
expect(result).toEqual({});
expect(console.warn.mock.calls[0][0]).toEqual("Can't read map parameters stored in localStorage");
});
});
describe("saveMapParamsToLocalStorage", () => {
const mockMap = {
getCenter: jest.fn().mockReturnValue({ lat: 48.8566, lng: 2.3522 }),
getZoom: jest.fn().mockReturnValue(10),
getBackground: jest.fn().mockReturnValue("streets"),
_mapFilters: { theme: "qualityscore" },
on: jest.fn(),
};
beforeEach(() => {
jest.clearAllMocks();
});
it("should add event listeners to the map", () => {
map.saveMapParamsToLocalStorage(mockMap);
expect(mockMap.on).toHaveBeenCalledWith("background-changed", expect.any(Function));
expect(mockMap.on).toHaveBeenCalledWith("filters-changed", expect.any(Function));
expect(mockMap.on).toHaveBeenCalledWith("moveend", expect.any(Function));
expect(mockMap.on).toHaveBeenCalledWith("zoomend", expect.any(Function));
expect(mockMap.on).toHaveBeenCalledWith("dragend", expect.any(Function));
expect(mockMap.on).toHaveBeenCalledWith("boxzoomend", expect.any(Function));
});
it("should save map parameters to localStorage", () => {
map.saveMapParamsToLocalStorage(mockMap);
expect(localStorage.setItem.mock.calls).toMatchSnapshot();
});
});