2 //= require js-cookie/dist/js.cookie
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require leaflet.map
7 //= require i18n/translations
10 describe("OSM", function () {
11 describe(".apiUrl", function () {
12 it("returns a URL for a way", function () {
13 expect(OSM.apiUrl({ type: "way", id: 10 })).to.eq("/api/0.6/way/10/full");
16 it("returns a URL for a node", function () {
17 expect(OSM.apiUrl({ type: "node", id: 10 })).to.eq("/api/0.6/node/10");
20 it("returns a URL for a specific version", function () {
21 expect(OSM.apiUrl({ type: "node", id: 10, version: 2 })).to.eq("/api/0.6/node/10/2");
25 describe(".params", function () {
26 it("parses params", function () {
27 var params = OSM.params("?foo=a&bar=b");
28 expect(params).to.have.property("foo", "a");
29 expect(params).to.have.property("bar", "b");
33 describe(".mapParams", function () {
34 beforeEach(function () {
37 document.location.hash = "";
38 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
40 // Test with another cookie set.
41 document.cookie = "_osm_session=deadbeef";
44 it("parses marker params", function () {
45 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
46 expect(params).to.have.property("mlat", 57.6247);
47 expect(params).to.have.property("mlon", -3.6845);
48 expect(params).to.have.property("marker", true);
51 it("parses object params", function () {
52 var params = OSM.mapParams("?node=1");
53 expect(params).to.have.property("object");
54 expect(params.object).to.eql({ type: "node", id: 1 });
56 params = OSM.mapParams("?way=1");
57 expect(params).to.have.property("object");
58 expect(params.object).to.eql({ type: "way", id: 1 });
60 params = OSM.mapParams("?relation=1");
61 expect(params).to.have.property("object");
62 expect(params.object).to.eql({ type: "relation", id: 1 });
65 it("parses bbox params", function () {
66 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
67 var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
68 expect(params).to.have.property("bounds").deep.equal(expected);
70 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
71 expect(params).to.have.property("bounds").deep.equal(expected);
74 it("parses mlat/mlon/zoom params", function () {
75 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
76 expect(params).to.have.property("lat", 57.6247);
77 expect(params).to.have.property("lon", -3.6845);
78 expect(params).to.have.property("zoom", 12);
80 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
81 expect(params).to.have.property("lat", 57.6247);
82 expect(params).to.have.property("lon", -3.6845);
83 expect(params).to.have.property("zoom", 16);
86 it("parses lat/lon/zoom from the hash", function () {
87 document.location.hash = "#map=16/57.6247/-3.6845";
88 const params = OSM.mapParams("?");
89 expect(params).to.have.property("lat", 57.6247);
90 expect(params).to.have.property("lon", -3.6845);
91 expect(params).to.have.property("zoom", 16);
94 it("sets lat/lon from OSM.home", function () {
95 OSM.home = { lat: 57.6247, lon: -3.6845 };
96 var params = OSM.mapParams("?");
97 expect(params).to.have.property("lat", 57.6247);
98 expect(params).to.have.property("lon", -3.6845);
101 it("sets bbox from OSM.location", function () {
102 OSM.location = { minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247 };
103 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
104 var params = OSM.mapParams("?");
105 expect(params).to.have.property("bounds").deep.equal(expected);
108 it("parses params from the _osm_location cookie", function () {
109 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
110 var params = OSM.mapParams("?");
111 expect(params).to.have.property("lat", 57.6247);
112 expect(params).to.have.property("lon", -3.6845);
113 expect(params).to.have.property("zoom", 5);
114 expect(params).to.have.property("layers", "M");
117 it("defaults lat/lon to London", function () {
118 var params = OSM.mapParams("?");
119 expect(params).to.have.property("lat", 51.5);
120 expect(params).to.have.property("lon", -0.1);
121 expect(params).to.have.property("zoom", 5);
123 params = OSM.mapParams("?zoom=10");
124 expect(params).to.have.property("lat", 51.5);
125 expect(params).to.have.property("lon", -0.1);
126 expect(params).to.have.property("zoom", 10);
129 it("parses layers param", function () {
130 var params = OSM.mapParams("?");
131 expect(params).to.have.property("layers", "");
133 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
134 params = OSM.mapParams("?");
135 expect(params).to.have.property("layers", "C");
137 document.location.hash = "#map=5/57.6247/-3.6845&layers=M";
138 params = OSM.mapParams("?");
139 expect(params).to.have.property("layers", "M");
143 describe(".parseHash", function () {
144 it("parses lat/lon/zoom params", function () {
145 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
146 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
147 expect(args).to.have.property("zoom", 5);
150 it("parses layers params", function () {
151 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
152 expect(args).to.have.property("layers", "M");
156 describe(".formatHash", function () {
157 it("formats lat/lon/zoom params", function () {
158 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
159 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
162 it("respects zoomPrecision", function () {
163 var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
164 expect(OSM.formatHash(args)).to.eq("#map=5/57.62/-3.68");
167 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
168 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
171 args = { center: L.latLng(57.6247, -3.6845), zoom: 12 };
172 expect(OSM.formatHash(args)).to.eq("#map=12/57.6247/-3.6845");
175 it("formats layers params", function () {
176 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
177 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685&layers=C");
180 it("ignores default layers", function () {
181 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
182 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
187 describe(".zoomPrecision", function () {
188 it("suggests 1 digit for z0-2", function () {
189 expect(OSM.zoomPrecision(0)).to.eq(1);
190 expect(OSM.zoomPrecision(1)).to.eq(1);
191 expect(OSM.zoomPrecision(2)).to.eq(1);
194 it("suggests 2 digits for z3-6", function () {
195 expect(OSM.zoomPrecision(3)).to.eq(2);
196 expect(OSM.zoomPrecision(4)).to.eq(2);
197 expect(OSM.zoomPrecision(5)).to.eq(2);
198 expect(OSM.zoomPrecision(6)).to.eq(2);
201 it("suggests 3 digits for z7-9", function () {
202 expect(OSM.zoomPrecision(7)).to.eq(3);
203 expect(OSM.zoomPrecision(8)).to.eq(3);
204 expect(OSM.zoomPrecision(9)).to.eq(3);
207 it("suggests 4 digits for z10-12", function () {
208 expect(OSM.zoomPrecision(10)).to.eq(4);
209 expect(OSM.zoomPrecision(11)).to.eq(4);
210 expect(OSM.zoomPrecision(12)).to.eq(4);
213 it("suggests 5 digits for z13-16", function () {
214 expect(OSM.zoomPrecision(13)).to.eq(5);
215 expect(OSM.zoomPrecision(14)).to.eq(5);
216 expect(OSM.zoomPrecision(15)).to.eq(5);
217 expect(OSM.zoomPrecision(16)).to.eq(5);
220 it("suggests 6 digits for z17-19", function () {
221 expect(OSM.zoomPrecision(17)).to.eq(6);
222 expect(OSM.zoomPrecision(18)).to.eq(6);
223 expect(OSM.zoomPrecision(19)).to.eq(6);
226 it("suggests 7 digits for z20", function () {
227 expect(OSM.zoomPrecision(20)).to.eq(7);
231 describe(".locationCookie", function () {
232 it("creates a location cookie value", function () {
233 $("body").html($("<div id='map'>"));
234 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
235 map.updateLayers("");
236 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
239 it("respects zoomPrecision", function () {
240 $("body").html($("<div id='map'>"));
241 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
242 map.updateLayers("");
243 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
244 // map.setZoom() doesn't update the zoom level for some reason
245 // using map._zoom here to update the zoom level manually
247 expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
251 describe(".distance", function () {
252 it("computes distance between points", function () {
253 var latlng1 = L.latLng(51.76712, -0.00484),
254 latlng2 = L.latLng(51.7675159, -0.0078329);
256 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
257 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);