2 //= require js-cookie/dist/js.cookie
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require leaflet.map
8 describe("OSM", function () {
9 describe(".apiUrl", function () {
10 it("returns a URL for a way", function () {
11 expect(OSM.apiUrl({ type: "way", id: 10 })).to.eq("/api/0.6/way/10/full");
14 it("returns a URL for a node", function () {
15 expect(OSM.apiUrl({ type: "node", id: 10 })).to.eq("/api/0.6/node/10");
18 it("returns a URL for a specific version", function () {
19 expect(OSM.apiUrl({ type: "node", id: 10, version: 2 })).to.eq("/api/0.6/node/10/2");
23 describe(".mapParams", function () {
24 beforeEach(function () {
28 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
30 // Test with another cookie set.
31 document.cookie = "_osm_session=deadbeef";
34 it("parses marker params", function () {
35 const params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
36 expect(params).to.have.property("mlat", 57.6247);
37 expect(params).to.have.property("mlon", -3.6845);
38 expect(params).to.have.property("marker", true);
41 it("parses object params", function () {
42 let params = OSM.mapParams("?node=1");
43 expect(params).to.have.property("object");
44 expect(params.object).to.eql({ type: "node", id: 1 });
46 params = OSM.mapParams("?way=1");
47 expect(params).to.have.property("object");
48 expect(params.object).to.eql({ type: "way", id: 1 });
50 params = OSM.mapParams("?relation=1");
51 expect(params).to.have.property("object");
52 expect(params.object).to.eql({ type: "relation", id: 1 });
55 it("parses bbox params", function () {
56 const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
57 let params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
58 expect(params).to.have.property("bounds").deep.equal(expected);
60 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
61 expect(params).to.have.property("bounds").deep.equal(expected);
64 it("parses mlat/mlon/zoom params", function () {
65 let params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
66 expect(params).to.have.property("lat", 57.6247);
67 expect(params).to.have.property("lon", -3.6845);
68 expect(params).to.have.property("zoom", 12);
70 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
71 expect(params).to.have.property("lat", 57.6247);
72 expect(params).to.have.property("lon", -3.6845);
73 expect(params).to.have.property("zoom", 16);
76 it("parses lat/lon/zoom from the hash", function () {
77 location.hash = "#map=16/57.6247/-3.6845";
78 const params = OSM.mapParams("?");
79 expect(params).to.have.property("lat", 57.6247);
80 expect(params).to.have.property("lon", -3.6845);
81 expect(params).to.have.property("zoom", 16);
84 it("sets lat/lon from OSM.home", function () {
85 OSM.home = { lat: 57.6247, lon: -3.6845 };
86 const params = OSM.mapParams("?");
87 expect(params).to.have.property("lat", 57.6247);
88 expect(params).to.have.property("lon", -3.6845);
91 it("sets bbox from OSM.location", function () {
92 OSM.location = { minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247 };
93 const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
94 const params = OSM.mapParams("?");
95 expect(params).to.have.property("bounds").deep.equal(expected);
98 it("parses params from the _osm_location cookie", function () {
99 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
100 const params = OSM.mapParams("?");
101 expect(params).to.have.property("lat", 57.6247);
102 expect(params).to.have.property("lon", -3.6845);
103 expect(params).to.have.property("zoom", 5);
104 expect(params).to.have.property("layers", "M");
107 it("defaults lat/lon to London", function () {
108 let params = OSM.mapParams("?");
109 expect(params).to.have.property("lat", 51.5);
110 expect(params).to.have.property("lon", -0.1);
111 expect(params).to.have.property("zoom", 5);
113 params = OSM.mapParams("?zoom=10");
114 expect(params).to.have.property("lat", 51.5);
115 expect(params).to.have.property("lon", -0.1);
116 expect(params).to.have.property("zoom", 10);
119 it("parses layers param", function () {
120 let params = OSM.mapParams("?");
121 expect(params).to.have.property("layers", "");
123 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
124 params = OSM.mapParams("?");
125 expect(params).to.have.property("layers", "C");
127 location.hash = "#map=5/57.6247/-3.6845&layers=M";
128 params = OSM.mapParams("?");
129 expect(params).to.have.property("layers", "M");
133 describe(".parseGeoURI", function () {
134 it("parses basic geoURIs", function () {
135 let params = OSM.parseGeoURI("geo:57.6247,-3.6845");
136 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
137 expect(params.zoom).to.be.undefined;
138 expect(params.uncertainty).to.be.undefined;
139 params = OSM.parseGeoURI("GEO:57.6247,-3.6845");
140 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
142 it("parses only geoURIs", function () {
143 let params = OSM.parseGeoURI("latlng:57.6247,-3.6845");
144 expect(params).to.be.undefined;
145 params = OSM.parseGeoURI("geo57.6247,-3.6845");
146 expect(params).to.be.undefined;
148 it("rejects geoURIs with less than 2 coordinates", function () {
149 const params = OSM.parseGeoURI("geo:57.6247");
150 expect(params).to.be.undefined;
152 it("parses geoURIs with altitude", function () {
153 const params = OSM.parseGeoURI("geo:57.6247,-3.6845,100");
154 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845, 100));
156 it("rejects geoURIs with more than 3 coordinates", function () {
157 const params = OSM.parseGeoURI("geo:123,57.6247,-3.6845,100");
158 expect(params).to.be.undefined;
160 it("ignores non-numeric coordinates", function () {
161 let params = OSM.parseGeoURI("geo:57.6247,-3.6845,abc");
162 expect(params.coords.lat).to.equal(57.6247);
163 expect(params.coords.lng).to.equal(-3.6845);
164 expect(isNaN(params.coords.alt)).to.be.true;
165 params = OSM.parseGeoURI("geo:57.6247,abc");
166 expect(params).to.be.undefined;
168 it("parses geoURIs with crs", function () {
169 let params = OSM.parseGeoURI("geo:57.6247,-3.6845;crs=wgs84");
170 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
171 params = OSM.parseGeoURI("geo:57.6247,-3.6845;CRS=wgs84");
172 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
173 params = OSM.parseGeoURI("geo:57.6247,-3.6845;CRS=WGS84");
174 expect(params.coords).to.deep.equal(L.latLng(57.6247, -3.6845));
176 it("rejects geoURIs with different crs", function () {
177 const params = OSM.parseGeoURI("geo:57.6247,-3.6845;crs=utm");
178 expect(params).to.be.undefined;
180 it("parses geoURIs with uncertainty", function () {
181 let params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=100");
182 expect(params.uncertainty).to.equal(100);
183 params = OSM.parseGeoURI("geo:57.6247,-3.6845;U=100");
184 expect(params.uncertainty).to.equal(100);
186 it("ignores negative uncertainty", function () {
187 const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=-100");
188 expect(params.uncertainty).to.be.undefined;
190 it("ignores non-numeric uncertainty", function () {
191 const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=abc");
192 expect(params.uncertainty).to.be.undefined;
194 it("parses uncertainty 0", function () {
195 const params = OSM.parseGeoURI("geo:57.6247,-3.6845;u=0");
196 expect(params.uncertainty).to.equal(0);
198 it("ignores uncertainty in the query parameters", function () {
199 const params = OSM.parseGeoURI("geo:57.6247,-3.6845?u=100");
200 expect(params.uncertainty).to.be.undefined;
202 it("parses geoURIs with zoom", function () {
203 let params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=16");
204 expect(params.zoom).to.equal(16);
205 params = OSM.parseGeoURI("geo:57.6247,-3.6845?Z=16");
206 expect(params.zoom).to.be.undefined;
208 it("ignores non-numeric zoom", function () {
209 const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=abc");
210 expect(params.zoom).to.be.undefined;
212 it("ignores negative zoom", function () {
213 const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=-100");
214 expect(params.zoom).to.be.undefined;
216 it("parses geoURIs with zoom level 0", function () {
217 const params = OSM.parseGeoURI("geo:57.6247,-3.6845?z=0");
218 expect(params.zoom).to.equal(0);
220 it("ignores zoom in the geouri parameters", function () {
221 const params = OSM.parseGeoURI("geo:57.6247,-3.6845;z=16");
222 expect(params.zoom).to.be.undefined;
226 describe(".parseHash", function () {
227 it("parses lat/lon/zoom params", function () {
228 const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
229 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
230 expect(args).to.have.property("zoom", 5);
233 it("parses layers params", function () {
234 const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
235 expect(args).to.have.property("layers", "M");
239 describe(".formatHash", function () {
240 it("formats lat/lon/zoom params", function () {
241 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
242 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
245 it("respects zoomPrecision", function () {
246 let args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
247 expect(OSM.formatHash(args)).to.eq("#map=5/57.62/-3.68");
250 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
251 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
254 args = { center: L.latLng(57.6247, -3.6845), zoom: 12 };
255 expect(OSM.formatHash(args)).to.eq("#map=12/57.6247/-3.6845");
258 it("formats layers params", function () {
259 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
260 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685&layers=C");
263 it("ignores default layers", function () {
264 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
265 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
270 describe(".zoomPrecision", function () {
271 it("suggests 1 digit for z0-2", function () {
272 expect(OSM.zoomPrecision(0)).to.eq(1);
273 expect(OSM.zoomPrecision(1)).to.eq(1);
274 expect(OSM.zoomPrecision(2)).to.eq(1);
277 it("suggests 2 digits for z3-6", function () {
278 expect(OSM.zoomPrecision(3)).to.eq(2);
279 expect(OSM.zoomPrecision(4)).to.eq(2);
280 expect(OSM.zoomPrecision(5)).to.eq(2);
281 expect(OSM.zoomPrecision(6)).to.eq(2);
284 it("suggests 3 digits for z7-9", function () {
285 expect(OSM.zoomPrecision(7)).to.eq(3);
286 expect(OSM.zoomPrecision(8)).to.eq(3);
287 expect(OSM.zoomPrecision(9)).to.eq(3);
290 it("suggests 4 digits for z10-12", function () {
291 expect(OSM.zoomPrecision(10)).to.eq(4);
292 expect(OSM.zoomPrecision(11)).to.eq(4);
293 expect(OSM.zoomPrecision(12)).to.eq(4);
296 it("suggests 5 digits for z13-16", function () {
297 expect(OSM.zoomPrecision(13)).to.eq(5);
298 expect(OSM.zoomPrecision(14)).to.eq(5);
299 expect(OSM.zoomPrecision(15)).to.eq(5);
300 expect(OSM.zoomPrecision(16)).to.eq(5);
303 it("suggests 6 digits for z17-19", function () {
304 expect(OSM.zoomPrecision(17)).to.eq(6);
305 expect(OSM.zoomPrecision(18)).to.eq(6);
306 expect(OSM.zoomPrecision(19)).to.eq(6);
309 it("suggests 7 digits for z20", function () {
310 expect(OSM.zoomPrecision(20)).to.eq(7);
314 describe(".locationCookie", function () {
315 it("creates a location cookie value", function () {
316 $("body").append("<div id='map'>");
317 const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
318 map.updateLayers("");
319 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
323 it("respects zoomPrecision", function () {
324 $("body").append("<div id='map'>");
325 const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
326 map.updateLayers("");
327 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
328 // map.setZoom() doesn't update the zoom level for some reason
329 // using map._zoom here to update the zoom level manually
331 expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
336 describe(".distance", function () {
337 it("computes distance between points", function () {
338 const latlng1 = L.latLng(51.76712, -0.00484),
339 latlng2 = L.latLng(51.7675159, -0.0078329);
341 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
342 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);