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(".params", function () {
24 it("parses params", function () {
25 const params = OSM.params("?foo=a&bar=b");
26 expect(params).to.have.property("foo", "a");
27 expect(params).to.have.property("bar", "b");
31 describe(".mapParams", function () {
32 beforeEach(function () {
36 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
38 // Test with another cookie set.
39 document.cookie = "_osm_session=deadbeef";
42 it("parses marker params", function () {
43 const params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
44 expect(params).to.have.property("mlat", 57.6247);
45 expect(params).to.have.property("mlon", -3.6845);
46 expect(params).to.have.property("marker", true);
49 it("parses object params", function () {
50 let params = OSM.mapParams("?node=1");
51 expect(params).to.have.property("object");
52 expect(params.object).to.eql({ type: "node", id: 1 });
54 params = OSM.mapParams("?way=1");
55 expect(params).to.have.property("object");
56 expect(params.object).to.eql({ type: "way", id: 1 });
58 params = OSM.mapParams("?relation=1");
59 expect(params).to.have.property("object");
60 expect(params.object).to.eql({ type: "relation", id: 1 });
63 it("parses bbox params", function () {
64 const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
65 let params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
66 expect(params).to.have.property("bounds").deep.equal(expected);
68 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
69 expect(params).to.have.property("bounds").deep.equal(expected);
72 it("parses mlat/mlon/zoom params", function () {
73 let params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
74 expect(params).to.have.property("lat", 57.6247);
75 expect(params).to.have.property("lon", -3.6845);
76 expect(params).to.have.property("zoom", 12);
78 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
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("parses lat/lon/zoom from the hash", function () {
85 location.hash = "#map=16/57.6247/-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);
89 expect(params).to.have.property("zoom", 16);
92 it("sets lat/lon from OSM.home", function () {
93 OSM.home = { lat: 57.6247, lon: -3.6845 };
94 const params = OSM.mapParams("?");
95 expect(params).to.have.property("lat", 57.6247);
96 expect(params).to.have.property("lon", -3.6845);
99 it("sets bbox from OSM.location", function () {
100 OSM.location = { minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247 };
101 const expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
102 const params = OSM.mapParams("?");
103 expect(params).to.have.property("bounds").deep.equal(expected);
106 it("parses params from the _osm_location cookie", function () {
107 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
108 const params = OSM.mapParams("?");
109 expect(params).to.have.property("lat", 57.6247);
110 expect(params).to.have.property("lon", -3.6845);
111 expect(params).to.have.property("zoom", 5);
112 expect(params).to.have.property("layers", "M");
115 it("defaults lat/lon to London", function () {
116 let params = OSM.mapParams("?");
117 expect(params).to.have.property("lat", 51.5);
118 expect(params).to.have.property("lon", -0.1);
119 expect(params).to.have.property("zoom", 5);
121 params = OSM.mapParams("?zoom=10");
122 expect(params).to.have.property("lat", 51.5);
123 expect(params).to.have.property("lon", -0.1);
124 expect(params).to.have.property("zoom", 10);
127 it("parses layers param", function () {
128 let params = OSM.mapParams("?");
129 expect(params).to.have.property("layers", "");
131 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
132 params = OSM.mapParams("?");
133 expect(params).to.have.property("layers", "C");
135 location.hash = "#map=5/57.6247/-3.6845&layers=M";
136 params = OSM.mapParams("?");
137 expect(params).to.have.property("layers", "M");
141 describe(".parseHash", function () {
142 it("parses lat/lon/zoom params", function () {
143 const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
144 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
145 expect(args).to.have.property("zoom", 5);
148 it("parses layers params", function () {
149 const args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
150 expect(args).to.have.property("layers", "M");
154 describe(".formatHash", function () {
155 it("formats lat/lon/zoom params", function () {
156 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
157 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
160 it("respects zoomPrecision", function () {
161 let args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
162 expect(OSM.formatHash(args)).to.eq("#map=5/57.62/-3.68");
165 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
166 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
169 args = { center: L.latLng(57.6247, -3.6845), zoom: 12 };
170 expect(OSM.formatHash(args)).to.eq("#map=12/57.6247/-3.6845");
173 it("formats layers params", function () {
174 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
175 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685&layers=C");
178 it("ignores default layers", function () {
179 const args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
180 expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
185 describe(".zoomPrecision", function () {
186 it("suggests 1 digit for z0-2", function () {
187 expect(OSM.zoomPrecision(0)).to.eq(1);
188 expect(OSM.zoomPrecision(1)).to.eq(1);
189 expect(OSM.zoomPrecision(2)).to.eq(1);
192 it("suggests 2 digits for z3-6", function () {
193 expect(OSM.zoomPrecision(3)).to.eq(2);
194 expect(OSM.zoomPrecision(4)).to.eq(2);
195 expect(OSM.zoomPrecision(5)).to.eq(2);
196 expect(OSM.zoomPrecision(6)).to.eq(2);
199 it("suggests 3 digits for z7-9", function () {
200 expect(OSM.zoomPrecision(7)).to.eq(3);
201 expect(OSM.zoomPrecision(8)).to.eq(3);
202 expect(OSM.zoomPrecision(9)).to.eq(3);
205 it("suggests 4 digits for z10-12", function () {
206 expect(OSM.zoomPrecision(10)).to.eq(4);
207 expect(OSM.zoomPrecision(11)).to.eq(4);
208 expect(OSM.zoomPrecision(12)).to.eq(4);
211 it("suggests 5 digits for z13-16", function () {
212 expect(OSM.zoomPrecision(13)).to.eq(5);
213 expect(OSM.zoomPrecision(14)).to.eq(5);
214 expect(OSM.zoomPrecision(15)).to.eq(5);
215 expect(OSM.zoomPrecision(16)).to.eq(5);
218 it("suggests 6 digits for z17-19", function () {
219 expect(OSM.zoomPrecision(17)).to.eq(6);
220 expect(OSM.zoomPrecision(18)).to.eq(6);
221 expect(OSM.zoomPrecision(19)).to.eq(6);
224 it("suggests 7 digits for z20", function () {
225 expect(OSM.zoomPrecision(20)).to.eq(7);
229 describe(".locationCookie", function () {
230 it("creates a location cookie value", function () {
231 $("body").html($("<div id='map'>"));
232 const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
233 map.updateLayers("");
234 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
237 it("respects zoomPrecision", function () {
238 $("body").html($("<div id='map'>"));
239 const map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
240 map.updateLayers("");
241 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|9|M");
242 // map.setZoom() doesn't update the zoom level for some reason
243 // using map._zoom here to update the zoom level manually
245 expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
249 describe(".distance", function () {
250 it("computes distance between points", function () {
251 const latlng1 = L.latLng(51.76712, -0.00484),
252 latlng2 = L.latLng(51.7675159, -0.0078329);
254 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
255 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);