2 //= require jquery.cookie
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require leaflet.map
7 //= require i18n/translations
8 //= require querystring
10 var querystring = require('querystring-component');
12 describe("OSM", function () {
13 describe(".apiUrl", function () {
14 it("returns a URL for a way", function () {
15 expect(OSM.apiUrl({type: "way", id: 10})).to.eq("/api/0.6/way/10/full");
18 it("returns a URL for a node", function () {
19 expect(OSM.apiUrl({type: "node", id: 10})).to.eq("/api/0.6/node/10");
22 it("returns a URL for a specific version", function () {
23 expect(OSM.apiUrl({type: "node", id: 10, version: 2})).to.eq("/api/0.6/node/10/2");
27 describe(".params", function () {
28 it("parses params", function () {
29 var params = OSM.params("?foo=a&bar=b");
30 expect(params).to.have.property("foo", "a");
31 expect(params).to.have.property("bar", "b");
35 describe(".mapParams", function () {
36 beforeEach(function () {
39 document.location.hash = "";
40 document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
42 // Test with another cookie set.
43 document.cookie = "_osm_session=deadbeef";
46 it("parses marker params", function () {
47 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
48 expect(params).to.have.property("mlat", 57.6247);
49 expect(params).to.have.property("mlon", -3.6845);
50 expect(params).to.have.property("marker", true);
53 it("parses object params", function () {
54 var params = OSM.mapParams("?node=1");
55 expect(params).to.have.property("object");
56 expect(params.object).to.eql({type: "node", id: 1});
58 params = OSM.mapParams("?way=1");
59 expect(params).to.have.property("object");
60 expect(params.object).to.eql({type: "way", id: 1});
62 params = OSM.mapParams("?relation=1");
63 expect(params).to.have.property("object");
64 expect(params.object).to.eql({type: "relation", id: 1});
67 it("parses bbox params", function () {
68 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
69 var params = OSM.mapParams("?bbox=-3.6845,57.6247,-3.7845,57.7247");
70 expect(params).to.have.property("bounds").deep.equal(expected);
72 params = OSM.mapParams("?minlon=-3.6845&minlat=57.6247&maxlon=-3.7845&maxlat=57.7247");
73 expect(params).to.have.property("bounds").deep.equal(expected);
76 it("parses mlat/mlon/zoom params", function () {
77 var params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
78 expect(params).to.have.property("lat", 57.6247);
79 expect(params).to.have.property("lon", -3.6845);
80 expect(params).to.have.property("zoom", 12);
82 params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
83 expect(params).to.have.property("lat", 57.6247);
84 expect(params).to.have.property("lon", -3.6845);
85 expect(params).to.have.property("zoom", 16);
88 it("parses lat/lon/zoom from the hash", function () {
89 document.location.hash = "#map=16/57.6247/-3.6845";
90 params = OSM.mapParams("?");
91 expect(params).to.have.property("lat", 57.6247);
92 expect(params).to.have.property("lon", -3.6845);
93 expect(params).to.have.property("zoom", 16);
96 it("sets lat/lon from OSM.home", function () {
97 OSM.home = {lat: 57.6247, lon: -3.6845};
98 var params = OSM.mapParams("?");
99 expect(params).to.have.property("lat", 57.6247);
100 expect(params).to.have.property("lon", -3.6845);
103 it("sets bbox from OSM.location", function () {
104 OSM.location = {minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247};
105 var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
106 var params = OSM.mapParams("?");
107 expect(params).to.have.property("bounds").deep.equal(expected);
110 it("parses params from the _osm_location cookie", function () {
111 document.cookie = "_osm_location=-3.6845|57.6247|5|M";
112 var params = OSM.mapParams("?");
113 expect(params).to.have.property("lat", 57.6247);
114 expect(params).to.have.property("lon", -3.6845);
115 expect(params).to.have.property("zoom", 5);
116 expect(params).to.have.property("layers", "M");
119 it("defaults lat/lon to London", function () {
120 var params = OSM.mapParams("?");
121 expect(params).to.have.property("lat", 51.5);
122 expect(params).to.have.property("lon", -0.1);
123 expect(params).to.have.property("zoom", 5);
125 params = OSM.mapParams("?zoom=10");
126 expect(params).to.have.property("lat", 51.5);
127 expect(params).to.have.property("lon", -0.1);
128 expect(params).to.have.property("zoom", 10);
131 it("parses layers param", function () {
132 var params = OSM.mapParams("?");
133 expect(params).to.have.property("layers", "");
135 document.cookie = "_osm_location=-3.6845|57.6247|5|C";
136 params = OSM.mapParams("?");
137 expect(params).to.have.property("layers", "C");
139 document.location.hash = "#map=5/57.6247/-3.6845&layers=M"
140 params = OSM.mapParams("?");
141 expect(params).to.have.property("layers", "M");
145 describe(".parseHash", function () {
146 it("parses lat/lon/zoom params", function () {
147 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
148 expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
149 expect(args).to.have.property("zoom", 5);
152 it("parses layers params", function () {
153 var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
154 expect(args).to.have.property("layers", "M");
158 describe(".formatHash", function () {
159 it("formats lat/lon/zoom params", function () {
160 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
161 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
164 it("respects zoomPrecision", function () {
165 var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
166 expect(OSM.formatHash(args)).to.eq("#map=5/57.625/-3.685");
168 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
169 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
172 it("formats layers params", function () {
173 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
174 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845&layers=C");
177 it("ignores default layers", function () {
178 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
179 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
183 describe(".zoomPrecision", function () {
184 it("suggests 0 digits for z0-1", function () {
185 expect(OSM.zoomPrecision(0)).to.eq(0);
186 expect(OSM.zoomPrecision(1)).to.eq(0);
189 it("suggests 1 digit for z2", function () {
190 expect(OSM.zoomPrecision(2)).to.eq(1);
193 it("suggests 2 digits for z3-4", function () {
194 expect(OSM.zoomPrecision(3)).to.eq(2);
195 expect(OSM.zoomPrecision(4)).to.eq(2);
198 it("suggests 3 digits for z5-8", function () {
199 expect(OSM.zoomPrecision(5)).to.eq(3);
200 expect(OSM.zoomPrecision(6)).to.eq(3);
201 expect(OSM.zoomPrecision(7)).to.eq(3);
202 expect(OSM.zoomPrecision(8)).to.eq(3);
205 it("suggests 4 digits for z9-16", function () {
206 expect(OSM.zoomPrecision(9)).to.eq(4);
207 expect(OSM.zoomPrecision(10)).to.eq(4);
208 expect(OSM.zoomPrecision(11)).to.eq(4);
209 expect(OSM.zoomPrecision(12)).to.eq(4);
210 expect(OSM.zoomPrecision(13)).to.eq(4);
211 expect(OSM.zoomPrecision(14)).to.eq(4);
212 expect(OSM.zoomPrecision(15)).to.eq(4);
213 expect(OSM.zoomPrecision(16)).to.eq(4);
216 it("suggests 5 digits for z17-20", function () {
217 expect(OSM.zoomPrecision(17)).to.eq(5);
218 expect(OSM.zoomPrecision(18)).to.eq(5);
219 expect(OSM.zoomPrecision(19)).to.eq(5);
220 expect(OSM.zoomPrecision(20)).to.eq(5);
224 describe(".locationCookie", function () {
225 it("creates a location cookie value", function () {
226 $("body").html($("<div id='map'>"));
227 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
228 map.updateLayers("");
229 expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
232 it("respects zoomPrecision", 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.6845|57.6247|9|M");
239 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
243 describe(".distance", function () {
244 it("computes distance between points", function () {
245 var latlng1 = L.latLng(51.76712,-0.00484),
246 latlng2 = L.latLng(51.7675159, -0.0078329);
248 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
249 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);