]> git.openstreetmap.org Git - rails.git/blobdiff - test/javascripts/osm_test.js
Merge remote-tracking branch 'upstream/pull/5937'
[rails.git] / test / javascripts / osm_test.js
index 0450943149ed9693bb4588996fb726015eabc906..8f4471140d21e59e8002f6fc596af464d9d03389 100644 (file)
@@ -4,8 +4,6 @@
 //= require leaflet/dist/leaflet-src
 //= require leaflet.osm
 //= require leaflet.map
-//= require i18n/translations
-//= require qs/dist/qs
 
 describe("OSM", function () {
   describe(".apiUrl", function () {
@@ -22,19 +20,11 @@ describe("OSM", function () {
     });
   });
 
-  describe(".params", function () {
-    it("parses params", function () {
-      var params = OSM.params("?foo=a&bar=b");
-      expect(params).to.have.property("foo", "a");
-      expect(params).to.have.property("bar", "b");
-    });
-  });
-
   describe(".mapParams", function () {
     beforeEach(function () {
       delete OSM.home;
       delete OSM.location;
-      document.location.hash = "";
+      location.hash = "";
       document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
 
       // Test with another cookie set.
@@ -42,14 +32,14 @@ describe("OSM", function () {
     });
 
     it("parses marker params", function () {
-      var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
+      const params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
       expect(params).to.have.property("mlat", 57.6247);
       expect(params).to.have.property("mlon", -3.6845);
       expect(params).to.have.property("marker", true);
     });
 
     it("parses object params", function () {
-      var params = OSM.mapParams("?node=1");
+      let params = OSM.mapParams("?node=1");
       expect(params).to.have.property("object");
       expect(params.object).to.eql({ type: "node", id: 1 });
 
@@ -63,8 +53,8 @@ describe("OSM", function () {
     });
 
     it("parses bbox params", function () {
-      var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
-      var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
+      const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
+      let params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
       expect(params).to.have.property("bounds").deep.equal(expected);
 
       params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
@@ -72,7 +62,7 @@ describe("OSM", function () {
     });
 
     it("parses mlat/mlon/zoom params", function () {
-      var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
+      let params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
       expect(params).to.have.property("lat", 57.6247);
       expect(params).to.have.property("lon", -3.6845);
       expect(params).to.have.property("zoom", 12);
@@ -83,8 +73,35 @@ describe("OSM", function () {
       expect(params).to.have.property("zoom", 16);
     });
 
+    it("parses geoURIs", function () {
+      const params = OSM.mapParams("?geouri=geo%3A57.6247%2C-3.6845");
+      expect(params).to.have.property("lat", 57.6247);
+      expect(params).to.have.property("lon", -3.6845);
+      expect(params).to.have.property("mlat", 57.6247);
+      expect(params).to.have.property("mlon", -3.6845);
+      expect(params).to.have.property("zoom", 12);
+    });
+
+    it("parses zoom in geoURIs", function () {
+      const params = OSM.mapParams("?geouri=geo%3A57.6247%2C-3.6845%3Fz%3D16");
+      expect(params).to.have.property("lat", 57.6247);
+      expect(params).to.have.property("lon", -3.6845);
+      expect(params).to.have.property("mlat", 57.6247);
+      expect(params).to.have.property("mlon", -3.6845);
+      expect(params).to.have.property("zoom", 16);
+    });
+
+    it("parses uncertainty in geoURIs", function () {
+      const params = OSM.mapParams("?geouri=geo%3A57.6247%2C-3.6845%3Bu%3D100");
+      const expected = L.latLngBounds([57.62290336944585, -3.6878552857327764], [57.62649663055414, -3.6811447142672233]);
+      expect(params).to.have.property("mlat", 57.6247);
+      expect(params).to.have.property("mlon", -3.6845);
+      expect(params).to.have.property("mrad", 100);
+      expect(params).to.have.property("bounds").deep.equal(expected);
+    });
+
     it("parses lat/lon/zoom from the hash", function () {
-      document.location.hash = "#map=16/57.6247/-3.6845";
+      location.hash = "#map=16/57.6247/-3.6845";
       const params = OSM.mapParams("?");
       expect(params).to.have.property("lat", 57.6247);
       expect(params).to.have.property("lon", -3.6845);
@@ -93,21 +110,21 @@ describe("OSM", function () {
 
     it("sets lat/lon from OSM.home", function () {
       OSM.home = { lat: 57.6247, lon: -3.6845 };
-      var params = OSM.mapParams("?");
+      const params = OSM.mapParams("?");
       expect(params).to.have.property("lat", 57.6247);
       expect(params).to.have.property("lon", -3.6845);
     });
 
     it("sets bbox from OSM.location", function () {
       OSM.location = { minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247 };
-      var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
-      var params = OSM.mapParams("?");
+      const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
+      const params = OSM.mapParams("?");
       expect(params).to.have.property("bounds").deep.equal(expected);
     });
 
     it("parses params from the _osm_location cookie", function () {
       document.cookie = "_osm_location=-3.6845|57.6247|5|M";
-      var params = OSM.mapParams("?");
+      const params = OSM.mapParams("?");
       expect(params).to.have.property("lat", 57.6247);
       expect(params).to.have.property("lon", -3.6845);
       expect(params).to.have.property("zoom", 5);
@@ -115,7 +132,7 @@ describe("OSM", function () {
     });
 
     it("defaults lat/lon to London", function () {
-      var params = OSM.mapParams("?");
+      let params = OSM.mapParams("?");
       expect(params).to.have.property("lat", 51.5);
       expect(params).to.have.property("lon", -0.1);
       expect(params).to.have.property("zoom", 5);
@@ -127,40 +144,133 @@ describe("OSM", function () {
     });
 
     it("parses layers param", function () {
-      var params = OSM.mapParams("?");
+      let params = OSM.mapParams("?");
       expect(params).to.have.property("layers", "");
 
       document.cookie = "_osm_location=-3.6845|57.6247|5|C";
       params = OSM.mapParams("?");
       expect(params).to.have.property("layers", "C");
 
-      document.location.hash = "#map=5/57.6247/-3.6845&layers=M";
+      location.hash = "#map=5/57.6247/-3.6845&layers=M";
       params = OSM.mapParams("?");
       expect(params).to.have.property("layers", "M");
     });
   });
 
+  describe(".parseGeoURI", function () {
+    it("parses basic geoURIs", function () {
+      let params = OSM.parseGeoURI("geo:57.6247,-3.6845");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
+      expect(params.zoom).to.be.undefined;
+      expect(params.uncertainty).to.be.undefined;
+      params = OSM.parseGeoURI("GEO:57.6247,-3.6845");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
+    });
+    it("parses only geoURIs", function () {
+      let params = OSM.parseGeoURI("latlng:57.6247,-3.6845");
+      expect(params).to.be.undefined;
+      params = OSM.parseGeoURI("geo57.6247,-3.6845");
+      expect(params).to.be.undefined;
+    });
+    it("rejects geoURIs with less than 2 coordinates", function () {
+      const params = OSM.parseGeoURI("geo:57.6247");
+      expect(params).to.be.undefined;
+    });
+    it("parses geoURIs with altitude", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845,100");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845, 100));
+    });
+    it("rejects geoURIs with more than 3 coordinates", function () {
+      const params = OSM.parseGeoURI("geo:123,57.6247,-3.6845,100");
+      expect(params).to.be.undefined;
+    });
+    it("ignores non-numeric coordinates", function () {
+      let params = OSM.parseGeoURI("geo:57.6247,-3.6845,abc");
+      expect(params.coords.lat).to.equal(57.6247);
+      expect(params.coords.lng).to.equal(-3.6845);
+      expect(isNaN(params.coords.alt)).to.be.true;
+      params = OSM.parseGeoURI("geo:57.6247,abc");
+      expect(params).to.be.undefined;
+    });
+    it("parses geoURIs with crs", function () {
+      let params = OSM.parseGeoURI("geo:57.6247,-3.6845;crs=wgs84");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
+      params = OSM.parseGeoURI("geo:57.6247,-3.6845;CRS=wgs84");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
+      params = OSM.parseGeoURI("geo:57.6247,-3.6845;CRS=WGS84");
+      expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
+    });
+    it("rejects geoURIs with different crs", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845;crs=utm");
+      expect(params).to.be.undefined;
+    });
+    it("parses geoURIs with uncertainty", function () {
+      let params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=100");
+      expect(params.uncertainty).to.equal(100);
+      params = OSM.parseGeoURI("geo:57.6247,-3.6845;U=100");
+      expect(params.uncertainty).to.equal(100);
+    });
+    it("ignores negative uncertainty", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=-100");
+      expect(params.uncertainty).to.be.undefined;
+    });
+    it("ignores non-numeric uncertainty", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=abc");
+      expect(params.uncertainty).to.be.undefined;
+    });
+    it("parses uncertainty 0", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=0");
+      expect(params.uncertainty).to.equal(0);
+    });
+    it("ignores uncertainty in the query parameters", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845?u=100");
+      expect(params.uncertainty).to.be.undefined;
+    });
+    it("parses geoURIs with zoom", function () {
+      let params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=16");
+      expect(params.zoom).to.equal(16);
+      params = OSM.parseGeoURI("geo:57.6247,-3.6845?Z=16");
+      expect(params.zoom).to.be.undefined;
+    });
+    it("ignores non-numeric zoom", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=abc");
+      expect(params.zoom).to.be.undefined;
+    });
+    it("ignores negative zoom", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=-100");
+      expect(params.zoom).to.be.undefined;
+    });
+    it("parses geoURIs with zoom level 0", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=0");
+      expect(params.zoom).to.equal(0);
+    });
+    it("ignores zoom in the geouri parameters", function () {
+      const params = OSM.parseGeoURI("geo:57.6247,-3.6845;z=16");
+      expect(params.zoom).to.be.undefined;
+    });
+  });
+
   describe(".parseHash", function () {
     it("parses lat/lon/zoom params", function () {
-      var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
+      const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
       expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
       expect(args).to.have.property("zoom", 5);
     });
 
     it("parses layers params", function () {
-      var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
+      const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
       expect(args).to.have.property("layers", "M");
     });
   });
 
   describe(".formatHash", function () {
     it("formats lat/lon/zoom params", function () {
-      var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
+      const args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
       expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
     });
 
     it("respects zoomPrecision", function () {
-      var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
+      let args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
       expect(OSM.formatHash(args)).to.eq("#map=5/57.62/-3.68");
 
 
@@ -173,12 +283,12 @@ describe("OSM", function () {
     });
 
     it("formats layers params", function () {
-      var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
+      const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
       expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685&layers=C");
     });
 
     it("ignores default layers", function () {
-      var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
+      const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
       expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
     });
   });
@@ -230,28 +340,30 @@ describe("OSM", function () {
 
   describe(".locationCookie", function () {
     it("creates a location cookie value", function () {
-      $("body").html($("<div id='map'>"));
-      var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
+      $("body").append("<div id='map'>");
+      const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
       map.updateLayers("");
       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
+      $("#map").remove();
     });
 
     it("respects zoomPrecision", function () {
-      $("body").html($("<div id='map'>"));
-      var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
+      $("body").append("<div id='map'>");
+      const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
       map.updateLayers("");
       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
       // map.setZoom() doesn't update the zoom level for some reason
       // using map._zoom here to update the zoom level manually
       map._zoom = 5;
       expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
+      $("#map").remove();
     });
   });
 
   describe(".distance", function () {
     it("computes distance between points", function () {
-      var latlng1 = L.latLng(51.76712, -0.00484),
-          latlng2 = L.latLng(51.7675159, -0.0078329);
+      const latlng1 = L.latLng(51.76712, -0.00484),
+            latlng2 = L.latLng(51.7675159, -0.0078329);
 
       expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
       expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);