UNPKG

@panoramax/web-viewer

Version:

Panoramax web viewer for geolocated pictures

316 lines (263 loc) 10.3 kB
import * as utils from "../../src/utils/utils"; jest.mock("../../src/utils/map", () => ({ COLORS_HEX: { SELECTED: 0x0000ff, } })); describe("getGrade", () => { it("works with null-like", () => { expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, null)).toBeNull(); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, undefined)).toBeNull(); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, "")).toBeNull(); }); it("works with grade values", () => { expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, 0)).toBe(1); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, 5)).toBe(1); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, 12)).toBe(2); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, 25)).toBe(3); expect(utils.getGrade(utils.QUALITYSCORE_RES_FLAT_VALUES, 40)).toBe(4); }); }); describe("getDistance", () => { it("works", () => { const p1 = [1,1]; const p2 = [2,2]; const res = utils.getDistance(p1, p2); expect(res).toBe(Math.sqrt(2)); }); }); describe("svgToPSVLink", () => { it("works", () => { const base64Svg = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0naHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmcnPjxjaXJjbGUgY3g9JzUnIGN5PSc1JyByPSc1JyBmaWxsPScjMDAwJy8+PC9zdmc+"; const fillColor = "red"; const result = utils.svgToPSVLink(base64Svg, fillColor); expect(result).toBeInstanceOf(HTMLButtonElement); expect(result.classList.contains("pnx-psv-tour-arrows")).toBe(true); expect(result.style.color).toBe(fillColor); expect(result.querySelector("svg")).not.toBeNull(); }); it("works with invalid input", () => { const invalidBase64Svg = "http://test.net/invalid_string"; const result = utils.svgToPSVLink(invalidBase64Svg, "blue"); expect(result).toBeInstanceOf(HTMLImageElement); expect(result.src).toBe(invalidBase64Svg); expect(result.alt).toBe(""); }); }); describe("getAzimuth", () => { it("works with 0 + NE", () => { const pointDepart = [0, 0]; const pointArrivee = [1, 1]; const azimuth = utils.getAzimuth(pointDepart, pointArrivee); expect(azimuth).toBe(45); }); it("works with 0 + SE", () => { const pointDepart = [0, 0]; const pointArrivee = [1, -1]; const azimuth = utils.getAzimuth(pointDepart, pointArrivee); expect(azimuth).toBe(135); }); it("works with 0 + N", () => { const pointDepart = [0, 0]; const pointArrivee = [0, 1]; const azimuth = utils.getAzimuth(pointDepart, pointArrivee); expect(azimuth).toBe(0); }); it("works with 0 + S", () => { const pointDepart = [0, 0]; const pointArrivee = [0, -1]; const azimuth = utils.getAzimuth(pointDepart, pointArrivee); expect(azimuth).toBe(180); }); }); describe("getRelativeHeading", () => { it("should throw an error if no picture selected", () => { expect(() => utils.getRelativeHeading()).toThrow("No picture selected"); }); it("should calculate relative heading correctly", () => { const pictureMetadata = { properties: { "view:azimuth": 30 }, sequence: { prevPic: "prevPictureId", nextPic: "nextPictureId" }, links: [ { nodeId: "prevPictureId", gps: [0, 0] }, { nodeId: "nextPictureId", gps: [2, 2] } ], gps: [1, 1] }; expect(utils.getRelativeHeading(pictureMetadata)).toBe(-15); }); it("works looking behind", () => { const pictureMetadata = { properties: { "view:azimuth": 226 }, sequence: { prevPic: "prevPictureId", nextPic: "nextPictureId" }, links: [ { nodeId: "prevPictureId", gps: [0, 0] }, { nodeId: "nextPictureId", gps: [2, 2] } ], gps: [1, 1] }; expect(utils.getRelativeHeading(pictureMetadata)).toBe(-179); }); it("works with distorted path", () => { const pictureMetadata = { properties: { "view:azimuth": 100 }, sequence: { prevPic: "prevPictureId", nextPic: "nextPictureId" }, links: [ { nodeId: "prevPictureId", gps: [0, 0] }, { nodeId: "nextPictureId", gps: [2, 1] } ], gps: [1, 0] }; expect(utils.getRelativeHeading(pictureMetadata)).toBe(10); }); it("works without previous link", () => { const pictureMetadata = { properties: { "view:azimuth": 100 }, sequence: { nextPic: "nextPictureId" }, links: [ { nodeId: "nextPictureId", gps: [2, 1] } ], gps: [1, 1] }; expect(utils.getRelativeHeading(pictureMetadata)).toBe(10); }); it("should handle missing prevPic or nextPic", () => { const metadataWithoutPrevNext = { properties: { "view:azimuth": 30 }, gps: [0, 0] }; expect(utils.getRelativeHeading(metadataWithoutPrevNext)).toBe(0); }); }); describe("getSimplifiedAngle", () => { it("returns \"N\"", () => { expect(utils.getSimplifiedAngle([0, 0], [0, 1])).toBe("N"); }); it("returns \"ENE\"", () => { expect(utils.getSimplifiedAngle([0, 0], [1, 1])).toBe("ENE"); }); it("returns \"ESE\"", () => { expect(utils.getSimplifiedAngle([0, 0], [1, -1])).toBe("ESE"); }); it("returns \"S\"", () => { expect(utils.getSimplifiedAngle([0, 0], [0, -1])).toBe("S"); }); it("returns \"WNW\"", () => { expect(utils.getSimplifiedAngle([0, 0], [-1, 1])).toBe("WNW"); }); it("returns \"WSW\"", () => { expect(utils.getSimplifiedAngle([0, 0], [-1, -1])).toBe("WSW"); }); }); describe("positionToXYZ", () => { it("works with xy", () => { const r = utils.positionToXYZ({ pitch: 10, yaw: -5 }); expect(r).toEqual({ x: -286.4788975654116, y: 572.9577951308232 }); }); it("works with xyz", () => { const r = utils.positionToXYZ({ pitch: 10, yaw: -5 }, 15); expect(r).toEqual({ x: -286.4788975654116, y: 572.9577951308232, z: 15 }); }); }); describe("xyzToPosition", () => { it("works with xyz", () => { const r = utils.xyzToPosition(-286.4788975654116, 572.9577951308232, 15); expect(r).toEqual({ pitch: 10, yaw: -5, zoom: 15 }); }); }); describe("degToDms", () => { it("converts positive decimal degrees to DMS correctly", () => { const result = utils.degToDms(45.7896541); expect(result).toEqual({ d: 45, m: 47, s: 22.755 }); }); it("converts negative decimal degrees to DMS correctly 1", () => { const result = utils.degToDms(-12.751234); expect(result).toEqual({ d: -12, m: 45, s: 4.442 }); }); it("converts negative decimal degrees to DMS correctly 2", () => { const result = utils.degToDms(-21.007598); expect(result).toEqual({ d: -21, m: 0, s: 27.353 }); }); it("converts zero degrees to DMS correctly", () => { const result = utils.degToDms(0); expect(result).toEqual({ d: 0, m: 0, s: 0 }); }); it("handles integer degrees correctly", () => { const result = utils.degToDms(90); expect(result).toEqual({ d: 90, m: 0, s: 0 }); }); }); describe("josmBboxParameters", () => { it("works with null-like", () => { expect(utils.josmBboxParameters(null)).toBeNull(); expect(utils.josmBboxParameters(undefined)).toBeNull(); }); it("works without azimuth", () => { const meta = { gps: [2.3522, 48.8566] }; const result = utils.josmBboxParameters(meta); expect(result).toBe("left=2.3522&right=2.3522&top=48.8566&bottom=48.8566&changeset_source=Panoramax"); }); it("works with azimuth = 0", () => { const meta = { gps: [2.3522, 48.8566], properties: { "view:azimuth": 0 } }; const result = utils.josmBboxParameters(meta); expect(result).toBe("left=2.3522&right=2.3524&top=48.8568&bottom=48.8566&changeset_source=Panoramax"); }); it("works with azimuth = 180", () => { const meta = { gps: [2.3522, 48.8566], properties: { "view:azimuth": 180 } }; const result = utils.josmBboxParameters(meta); expect(result).toBe("left=2.352&right=2.3524&top=48.8566&bottom=48.8564&changeset_source=Panoramax"); }); it("works with azimuth = 90", () => { const meta = { gps: [2.3522, 48.8566], properties: { "view:azimuth": 90 } }; const result = utils.josmBboxParameters(meta); expect(result).toBe("left=2.3522&right=2.3524&top=48.8568&bottom=48.8564&changeset_source=Panoramax"); }); it("works with azimuth = 270", () => { const meta = { gps: [2.3522, 48.8566], properties: { "view:azimuth": 270 } }; const result = utils.josmBboxParameters(meta); expect(result).toBe("left=2.352&right=2.3522&top=48.8568&bottom=48.8564&changeset_source=Panoramax"); }); }); describe("getCookie", () => { it("should return the value of the specified cookie", () => { jest.spyOn(document, "cookie", "get").mockReturnValueOnce("session=abc123"); expect(utils.getCookie("session")).toBe("abc123"); }); it("should return null if the cookie is not found", () => { jest.spyOn(document, "cookie", "get").mockReturnValueOnce("session=abc123"); expect(utils.getCookie("user_id")).toBeUndefined(); }); it("should return the correct value when multiple cookies are set", () => { jest.spyOn(document, "cookie", "get").mockReturnValueOnce("session=abc123; user_id=789; user_name=John"); expect(utils.getCookie("user_id")).toBe("789"); }); it("should return null if cookie with the specified name has no value", () => { jest.spyOn(document, "cookie", "get").mockReturnValueOnce("session=; user_id=789"); expect(utils.getCookie("session")).toBe(""); }); it("should return the correct value when the cookie contains =", () => { jest.spyOn(document, "cookie", "get").mockReturnValueOnce("custom_cookie=abc=123"); expect(utils.getCookie("custom_cookie")).toBe("abc=123"); }); }); describe("getUserAccount", () => { it("should return an object with user id and name when all cookies are present", () => { jest.spyOn(document, "cookie", "get").mockReturnValue("session=abc123; user_id=789; user_name=John"); expect(utils.getUserAccount()).toEqual({ id: "789", name: "John" }); }); it("should return null if session cookie is missing", () => { jest.spyOn(document, "cookie", "get").mockReturnValue("user_id=789; user_name=John"); expect(utils.getUserAccount()).toBeNull(); }); it("should return null if user_id cookie is missing", () => { jest.spyOn(document, "cookie", "get").mockReturnValue("session=abc123; user_name=John"); expect(utils.getUserAccount()).toBeNull(); }); it("should return null if user_name cookie is missing", () => { jest.spyOn(document, "cookie", "get").mockReturnValue("session=abc123; user_id=789"); expect(utils.getUserAccount()).toBeNull(); }); it("should return null if all cookies are missing", () => { expect(utils.getUserAccount()).toBeNull(); }); });