@panoramax/web-viewer
Version:
Panoramax web viewer for geolocated pictures
249 lines (225 loc) • 7.2 kB
JavaScript
import Map from "../../../src/components/ui/Map";
const createParent = () => ({
addEventListener: jest.fn(),
dispatchEvent: jest.fn(),
isWidthSmall: jest.fn(),
onceReady: () => Promise.resolve(),
onceAPIReady: () => Promise.resolve(),
users: ["geovisio"],
api: {
onceReady: () => Promise.resolve(),
getDataBbox: jest.fn(),
getPicturesTilesUrl: jest.fn(),
_getMapRequestTransform: jest.fn(),
getMapStyle: () => ({ sources: {}, layers: [], metadata: {} }),
},
_t: {
maplibre: {},
}
});
describe("_postLoad", () => {
it("send ready event", async () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
const listener = jest.fn();
m.addEventListener("ready", listener, { once: true });
await m._postLoad();
expect(listener).toHaveBeenCalledTimes(1);
});
});
describe("reloadVectorTiles", () => {
it("works", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
const setter = jest.fn();
m.getSource = (user) => ({ tiles: [`https://bla.xyz/${user}/x/y/z`], setTiles: setter });
m._userLayers = ["geovisio", "toto"];
m.reloadVectorTiles();
expect(setter.mock.calls).toMatchSnapshot();
});
});
describe("hasTwoBackgrounds", () => {
it("is true if 2 bg", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.getLayer = (id) => id == "pnx-aerial";
expect(m.hasTwoBackgrounds()).toBeTruthy();
});
it("is false if 1 bg", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.getLayer = (id) => id == "pnx-aerial" ? undefined : {};
expect(m.hasTwoBackgrounds()).toBeFalsy();
});
});
describe("getBackground", () => {
it("works if raster is enabled", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c, { raster: { type: "raster" } });
m.getLayer = () => true;
m.getLayoutProperty = () => "hidden";
expect(m.getBackground()).toBe("streets");
m.getLayoutProperty = () => "visible";
expect(m.getBackground()).toBe("aerial");
});
it("works if no raster enabled", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.getLayer = () => undefined;
expect(m.getBackground()).toBe("streets");
});
});
describe("setBackground", () => {
it("works if raster is enabled", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c, { raster: { type: "raster" } });
m.setLayoutProperty = jest.fn();
m.getLayer = () => true;
return new Promise(resolve => {
m.on("background-changed", e => {
expect(m.setLayoutProperty.mock.calls).toMatchSnapshot();
expect(e.background).toEqual("aerial");
resolve();
});
m.setBackground("aerial");
});
});
it("skips if setting streets and no raster available", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.setLayoutProperty = jest.fn();
m.getLayer = () => undefined;
m.setBackground("streets");
expect(m.setLayoutProperty.mock.calls.length).toBe(0);
});
it("fails if no raster available", () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.getLayer = () => undefined;
expect(() => m.setBackground("aerial")).toThrowError("No aerial imagery available");
});
});
describe("getVisibleUsers", () => {
it("works", async () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m.getSource = () => true;
m.setPaintProperty = jest.fn();
await m._postLoad();
m.getLayoutProperty = () => "visible";
expect(m.getVisibleUsers()).toStrictEqual(["geovisio"]);
});
});
describe("setVisibleUsers", () => {
it("works when no users exists", async () => {
const p = createParent();
const c = document.createElement("div");
const m = new Map(p, c);
m._createPicturesTilesLayer = (url, id) => m._userLayers.add(id);
m._addedLayers = [];
m.addLayer = jest.fn();
await m.setVisibleUsers(["blabla"]);
expect(m.addLayer.mock.calls).toMatchSnapshot();
expect(p.dispatchEvent.mock.calls).toMatchSnapshot();
});
it("works when user already exist but is hidden", async () => {
const p = createParent();
p.users = ["blabla", "geovisio"];
const c = document.createElement("div");
const m = new Map(p, c);
m.setPaintProperty = jest.fn();
m.getSource = () => true;
m.getLayer = () => true;
let cptlCount = 0;
let deCalls = [];
return new Promise(async (resolve) => {
m.on("users-changed", resolve);
await m._postLoad();
}).then(() => {
m.setLayoutProperty = jest.fn();
m._createPicturesTilesLayer = () => { cptlCount++; return Promise.resolve(); };
expect(m._userLayers).toEqual(new Set(["blabla", "geovisio"]));
m.setVisibleUsers(["blabla"]);
}).then(() => {
expect(cptlCount).toBe(0);
expect(deCalls).toMatchSnapshot();
expect(m.setLayoutProperty.mock.calls).toMatchSnapshot();
});
});
});
describe("filterUserLayersContent", () => {
it("works", async () => {
const p = createParent();
p.users = ["blabla", "geovisio"];
const c = document.createElement("div");
const m = new Map(p, c);
m.getSource = () => true;
m.setPaintProperty = jest.fn();
m.getLayer = () => true;
await m._postLoad();
m.setFilter = jest.fn();
m.filterUserLayersContent("pictures", [["test", "true"]]);
expect(m.setFilter.mock.calls).toMatchSnapshot();
});
});
describe("reloadLayersStyles", () => {
it("works", async () => {
const p = createParent();
p.users = ["blabla", "geovisio"];
const c = document.createElement("div");
const m = new Map(p, c);
m.getSource = () => true;
m.setPaintProperty = jest.fn();
await m._postLoad();
m.setLayoutProperty = jest.fn();
m.setPaintProperty = jest.fn();
m.reloadLayersStyles();
expect(m.setLayoutProperty.mock.calls).toMatchSnapshot();
expect(m.setPaintProperty.mock.calls).toMatchSnapshot();
});
});
describe("addEventListener", () => {
let map;
beforeEach(() => {
const p = createParent();
const c = document.createElement("div");
map = new Map(p, c);
});
it("should add an event listener", () => {
const listener = jest.fn();
map.addEventListener("ready", listener);
map.fire("ready");
expect(listener).toHaveBeenCalled();
});
it("should add an event listener that only fires once", () => {
const listener = jest.fn();
map.addEventListener("ready", listener, { once: true });
map.fire("ready");
map.fire("ready");
expect(listener).toHaveBeenCalledTimes(1);
});
it("should not add an event listener if options.once is false", () => {
const listener = jest.fn();
map.addEventListener("ready", listener, { once: false });
map.fire("ready");
map.fire("ready");
expect(listener).toHaveBeenCalledTimes(2);
});
it("should default options.once to false if not provided", () => {
const listener = jest.fn();
map.addEventListener("ready", listener);
map.fire("ready");
map.fire("ready");
expect(listener).toHaveBeenCalledTimes(2);
});
});