3 //= require js-cookie/dist/js.cookie
5 //= require leaflet/dist/leaflet-src
6 //= require leaflet.osm
7 //= require leaflet.map
8 //= require i18n/translations
11 describe("OSM", function () {
12 describe(".apiUrl", function () {
13 it("returns a URL for a way", function () {
14 expect(OSM.apiUrl({type: "way", id: 10})).to.eq("/api/0.6/way/10/full");
17 it("returns a URL for a node", function () {
18 expect(OSM.apiUrl({type: "node", id: 10})).to.eq("/api/0.6/node/10");
21 it("returns a URL for a specific version", function () {
22 expect(OSM.apiUrl({type: "node", id: 10, version: 2})).to.eq("/api/0.6/node/10/2");
26 describe(".params", function () {
27 it("parses params", function () {
28 var params = OSM.params("?foo=a&bar=b");
29 expect(params).to.have.property("foo", "a");
30 expect(params).to.have.property("bar", "b");
34 describe(".mapParams", function () {
35 beforeEach(function () {
38 document.location.hash = "";
39 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
41 // Test with another cookie set.
42 document.cookie = "_osm_session=deadbeef";
45 it("parses marker params", function () {
46 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
47 expect(params).to.have.property("mlat", 57.6247);
48 expect(params).to.have.property("mlon", -3.6845);
49 expect(params).to.have.property("marker", true);
52 it("parses object params", function () {
53 var params = OSM.mapParams("?node=1");
54 expect(params).to.have.property("object");
55 expect(params.object).to.eql({type: "node", id: 1});
57 params = OSM.mapParams("?way=1");
58 expect(params).to.have.property("object");
59 expect(params.object).to.eql({type: "way", id: 1});
61 params = OSM.mapParams("?relation=1");
62 expect(params).to.have.property("object");
63 expect(params.object).to.eql({type: "relation", id: 1});
66 it("parses bbox params", function () {
67 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
68 var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
69 expect(params).to.have.property("bounds").deep.equal(expected);
71 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
72 expect(params).to.have.property("bounds").deep.equal(expected);
75 it("parses mlat/mlon/zoom params", function () {
76 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
77 expect(params).to.have.property("lat", 57.6247);
78 expect(params).to.have.property("lon", -3.6845);
79 expect(params).to.have.property("zoom", 12);
81 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
82 expect(params).to.have.property("lat", 57.6247);
83 expect(params).to.have.property("lon", -3.6845);
84 expect(params).to.have.property("zoom", 16);
87 it("parses lat/lon/zoom from the hash", function () {
88 document.location.hash = "#map=16/57.6247/-3.6845";
89 params = OSM.mapParams("?");
90 expect(params).to.have.property("lat", 57.6247);
91 expect(params).to.have.property("lon", -3.6845);
92 expect(params).to.have.property("zoom", 16);
95 it("sets lat/lon from OSM.home", function () {
96 OSM.home = {lat: 57.6247, lon: -3.6845};
97 var params = OSM.mapParams("?");
98 expect(params).to.have.property("lat", 57.6247);
99 expect(params).to.have.property("lon", -3.6845);
102 it("sets bbox from OSM.location", function () {
103 OSM.location = {minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247};
104 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
105 var params = OSM.mapParams("?");
106 expect(params).to.have.property("bounds").deep.equal(expected);
109 it("parses params from the _osm_location cookie", function () {
110 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
111 var params = OSM.mapParams("?");
112 expect(params).to.have.property("lat", 57.6247);
113 expect(params).to.have.property("lon", -3.6845);
114 expect(params).to.have.property("zoom", 5);
115 expect(params).to.have.property("layers", "M");
118 it("defaults lat/lon to London", function () {
119 var params = OSM.mapParams("?");
120 expect(params).to.have.property("lat", 51.5);
121 expect(params).to.have.property("lon", -0.1);
122 expect(params).to.have.property("zoom", 5);
124 params = OSM.mapParams("?zoom=10");
125 expect(params).to.have.property("lat", 51.5);
126 expect(params).to.have.property("lon", -0.1);
127 expect(params).to.have.property("zoom", 10);
130 it("parses layers param", function () {
131 var params = OSM.mapParams("?");
132 expect(params).to.have.property("layers", "");
134 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
135 params = OSM.mapParams("?");
136 expect(params).to.have.property("layers", "C");
138 document.location.hash = "#map=5/57.6247/-3.6845&layers=M"
139 params = OSM.mapParams("?");
140 expect(params).to.have.property("layers", "M");
144 describe(".parseHash", function () {
145 it("parses lat/lon/zoom params", function () {
146 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
147 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
148 expect(args).to.have.property("zoom", 5);
151 it("parses layers params", function () {
152 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
153 expect(args).to.have.property("layers", "M");
157 describe(".formatHash", function () {
158 it("formats lat/lon/zoom params", function () {
159 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
160 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
163 it("respects zoomPrecision", function () {
164 var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
165 expect(OSM.formatHash(args)).to.eq("#map=5/57.625/-3.685");
167 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
168 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
171 it("formats layers params", function () {
172 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
173 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845&layers=C");
176 it("ignores default layers", function () {
177 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
178 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
182 describe(".zoomPrecision", function () {
183 it("suggests 0 digits for z0-1", function () {
184 expect(OSM.zoomPrecision(0)).to.eq(0);
185 expect(OSM.zoomPrecision(1)).to.eq(0);
188 it("suggests 1 digit for z2", function () {
189 expect(OSM.zoomPrecision(2)).to.eq(1);
192 it("suggests 2 digits for z3-4", function () {
193 expect(OSM.zoomPrecision(3)).to.eq(2);
194 expect(OSM.zoomPrecision(4)).to.eq(2);
197 it("suggests 3 digits for z5-8", function () {
198 expect(OSM.zoomPrecision(5)).to.eq(3);
199 expect(OSM.zoomPrecision(6)).to.eq(3);
200 expect(OSM.zoomPrecision(7)).to.eq(3);
201 expect(OSM.zoomPrecision(8)).to.eq(3);
204 it("suggests 4 digits for z9-16", function () {
205 expect(OSM.zoomPrecision(9)).to.eq(4);
206 expect(OSM.zoomPrecision(10)).to.eq(4);
207 expect(OSM.zoomPrecision(11)).to.eq(4);
208 expect(OSM.zoomPrecision(12)).to.eq(4);
209 expect(OSM.zoomPrecision(13)).to.eq(4);
210 expect(OSM.zoomPrecision(14)).to.eq(4);
211 expect(OSM.zoomPrecision(15)).to.eq(4);
212 expect(OSM.zoomPrecision(16)).to.eq(4);
215 it("suggests 5 digits for z17-20", function () {
216 expect(OSM.zoomPrecision(17)).to.eq(5);
217 expect(OSM.zoomPrecision(18)).to.eq(5);
218 expect(OSM.zoomPrecision(19)).to.eq(5);
219 expect(OSM.zoomPrecision(20)).to.eq(5);
223 describe(".locationCookie", function () {
224 it("creates a location cookie value", function () {
225 $("body").html($("<div id='map'>"));
226 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
227 map.updateLayers("");
228 expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
231 it("respects zoomPrecision", 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.6845|57.6247|9|M");
238 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
242 describe(".distance", function () {
243 it("computes distance between points", function () {
244 var latlng1 = L.latLng(51.76712,-0.00484),
245 latlng2 = L.latLng(51.7675159, -0.0078329);
247 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
248 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);