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
9 describe("OSM", function () {
10 describe(".apiUrl", function () {
11 it("returns a URL for a way", function () {
12 expect(OSM.apiUrl({ type: "way", id: 10 })).to.eq("/api/0.6/way/10/full");
15 it("returns a URL for a node", function () {
16 expect(OSM.apiUrl({ type: "node", id: 10 })).to.eq("/api/0.6/node/10");
19 it("returns a URL for a specific version", function () {
20 expect(OSM.apiUrl({ type: "node", id: 10, version: 2 })).to.eq("/api/0.6/node/10/2");
24 describe(".params", function () {
25 it("parses params", function () {
26 var params = OSM.params("?foo=a&bar=b");
27 expect(params).to.have.property("foo", "a");
28 expect(params).to.have.property("bar", "b");
32 describe(".mapParams", function () {
33 beforeEach(function () {
36 document.location.hash = "";
37 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
39 // Test with another cookie set.
40 document.cookie = "_osm_session=deadbeef";
43 it("parses marker params", function () {
44 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
45 expect(params).to.have.property("mlat", 57.6247);
46 expect(params).to.have.property("mlon", -3.6845);
47 expect(params).to.have.property("marker", true);
50 it("parses object params", function () {
51 var params = OSM.mapParams("?node=1");
52 expect(params).to.have.property("object");
53 expect(params.object).to.eql({ type: "node", id: 1 });
55 params = OSM.mapParams("?way=1");
56 expect(params).to.have.property("object");
57 expect(params.object).to.eql({ type: "way", id: 1 });
59 params = OSM.mapParams("?relation=1");
60 expect(params).to.have.property("object");
61 expect(params.object).to.eql({ type: "relation", id: 1 });
64 it("parses bbox params", function () {
65 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
66 var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
67 expect(params).to.have.property("bounds").deep.equal(expected);
69 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
70 expect(params).to.have.property("bounds").deep.equal(expected);
73 it("parses mlat/mlon/zoom params", function () {
74 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
75 expect(params).to.have.property("lat", 57.6247);
76 expect(params).to.have.property("lon", -3.6845);
77 expect(params).to.have.property("zoom", 12);
79 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
80 expect(params).to.have.property("lat", 57.6247);
81 expect(params).to.have.property("lon", -3.6845);
82 expect(params).to.have.property("zoom", 16);
85 it("parses lat/lon/zoom from the hash", function () {
86 document.location.hash = "#map=16/57.6247/-3.6845";
87 const params = OSM.mapParams("?");
88 expect(params).to.have.property("lat", 57.6247);
89 expect(params).to.have.property("lon", -3.6845);
90 expect(params).to.have.property("zoom", 16);
93 it("sets lat/lon from OSM.home", function () {
94 OSM.home = { lat: 57.6247, lon: -3.6845 };
95 var params = OSM.mapParams("?");
96 expect(params).to.have.property("lat", 57.6247);
97 expect(params).to.have.property("lon", -3.6845);
100 it("sets bbox from OSM.location", function () {
101 OSM.location = { minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247 };
102 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
103 var params = OSM.mapParams("?");
104 expect(params).to.have.property("bounds").deep.equal(expected);
107 it("parses params from the _osm_location cookie", function () {
108 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
109 var params = OSM.mapParams("?");
110 expect(params).to.have.property("lat", 57.6247);
111 expect(params).to.have.property("lon", -3.6845);
112 expect(params).to.have.property("zoom", 5);
113 expect(params).to.have.property("layers", "M");
116 it("defaults lat/lon to London", function () {
117 var params = OSM.mapParams("?");
118 expect(params).to.have.property("lat", 51.5);
119 expect(params).to.have.property("lon", -0.1);
120 expect(params).to.have.property("zoom", 5);
122 params = OSM.mapParams("?zoom=10");
123 expect(params).to.have.property("lat", 51.5);
124 expect(params).to.have.property("lon", -0.1);
125 expect(params).to.have.property("zoom", 10);
128 it("parses layers param", function () {
129 var params = OSM.mapParams("?");
130 expect(params).to.have.property("layers", "");
132 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
133 params = OSM.mapParams("?");
134 expect(params).to.have.property("layers", "C");
136 document.location.hash = "#map=5/57.6247/-3.6845&layers=M";
137 params = OSM.mapParams("?");
138 expect(params).to.have.property("layers", "M");
142 describe(".parseHash", function () {
143 it("parses lat/lon/zoom params", function () {
144 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
145 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
146 expect(args).to.have.property("zoom", 5);
149 it("parses layers params", function () {
150 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
151 expect(args).to.have.property("layers", "M");
155 describe(".formatHash", function () {
156 it("formats lat/lon/zoom params", function () {
157 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
158 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
161 it("respects zoomPrecision", function () {
162 var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
163 expect(OSM.formatHash(args)).to.eq("#map=5/57.62/-3.68");
166 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
167 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
170 args = { center: L.latLng(57.6247, -3.6845), zoom: 12 };
171 expect(OSM.formatHash(args)).to.eq("#map=12/57.6247/-3.6845");
174 it("formats layers params", function () {
175 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
176 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685&layers=C");
179 it("ignores default layers", function () {
180 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
181 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
186 describe(".zoomPrecision", function () {
187 it("suggests 1 digit for z0-2", function () {
188 expect(OSM.zoomPrecision(0)).to.eq(1);
189 expect(OSM.zoomPrecision(1)).to.eq(1);
190 expect(OSM.zoomPrecision(2)).to.eq(1);
193 it("suggests 2 digits for z3-6", function () {
194 expect(OSM.zoomPrecision(3)).to.eq(2);
195 expect(OSM.zoomPrecision(4)).to.eq(2);
196 expect(OSM.zoomPrecision(5)).to.eq(2);
197 expect(OSM.zoomPrecision(6)).to.eq(2);
200 it("suggests 3 digits for z7-9", function () {
201 expect(OSM.zoomPrecision(7)).to.eq(3);
202 expect(OSM.zoomPrecision(8)).to.eq(3);
203 expect(OSM.zoomPrecision(9)).to.eq(3);
206 it("suggests 4 digits for z10-12", function () {
207 expect(OSM.zoomPrecision(10)).to.eq(4);
208 expect(OSM.zoomPrecision(11)).to.eq(4);
209 expect(OSM.zoomPrecision(12)).to.eq(4);
212 it("suggests 5 digits for z13-16", function () {
213 expect(OSM.zoomPrecision(13)).to.eq(5);
214 expect(OSM.zoomPrecision(14)).to.eq(5);
215 expect(OSM.zoomPrecision(15)).to.eq(5);
216 expect(OSM.zoomPrecision(16)).to.eq(5);
219 it("suggests 6 digits for z17-19", function () {
220 expect(OSM.zoomPrecision(17)).to.eq(6);
221 expect(OSM.zoomPrecision(18)).to.eq(6);
222 expect(OSM.zoomPrecision(19)).to.eq(6);
225 it("suggests 7 digits for z20", function () {
226 expect(OSM.zoomPrecision(20)).to.eq(7);
230 describe(".locationCookie", function () {
231 it("creates a location cookie value", function () {
232 $("body").html($("<div id='map'>"));
233 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
234 map.updateLayers("");
235 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
238 it("respects zoomPrecision", function () {
239 $("body").html($("<div id='map'>"));
240 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
241 map.updateLayers("");
242 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
243 // map.setZoom() doesn't update the zoom level for some reason
244 // using map._zoom here to update the zoom level manually
246 expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
250 describe(".distance", function () {
251 it("computes distance between points", function () {
252 var latlng1 = L.latLng(51.76712, -0.00484),
253 latlng2 = L.latLng(51.7675159, -0.0078329);
255 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
256 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);