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 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.6247/-3.6845");
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.625/-3.685");
166 args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
167 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
170 it("formats layers params", function () {
171 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
172 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845&layers=C");
175 it("ignores default layers", function () {
176 var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
177 expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
181 describe(".zoomPrecision", function () {
182 it("suggests 0 digits for z0-1", function () {
183 expect(OSM.zoomPrecision(0)).to.eq(0);
184 expect(OSM.zoomPrecision(1)).to.eq(0);
187 it("suggests 1 digit for z2", function () {
188 expect(OSM.zoomPrecision(2)).to.eq(1);
191 it("suggests 2 digits for z3-4", function () {
192 expect(OSM.zoomPrecision(3)).to.eq(2);
193 expect(OSM.zoomPrecision(4)).to.eq(2);
196 it("suggests 3 digits for z5-8", function () {
197 expect(OSM.zoomPrecision(5)).to.eq(3);
198 expect(OSM.zoomPrecision(6)).to.eq(3);
199 expect(OSM.zoomPrecision(7)).to.eq(3);
200 expect(OSM.zoomPrecision(8)).to.eq(3);
203 it("suggests 4 digits for z9-16", function () {
204 expect(OSM.zoomPrecision(9)).to.eq(4);
205 expect(OSM.zoomPrecision(10)).to.eq(4);
206 expect(OSM.zoomPrecision(11)).to.eq(4);
207 expect(OSM.zoomPrecision(12)).to.eq(4);
208 expect(OSM.zoomPrecision(13)).to.eq(4);
209 expect(OSM.zoomPrecision(14)).to.eq(4);
210 expect(OSM.zoomPrecision(15)).to.eq(4);
211 expect(OSM.zoomPrecision(16)).to.eq(4);
214 it("suggests 5 digits for z17-20", function () {
215 expect(OSM.zoomPrecision(17)).to.eq(5);
216 expect(OSM.zoomPrecision(18)).to.eq(5);
217 expect(OSM.zoomPrecision(19)).to.eq(5);
218 expect(OSM.zoomPrecision(20)).to.eq(5);
222 describe(".locationCookie", function () {
223 it("creates a location cookie value", function () {
224 $("body").html($("<div id='map'>"));
225 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
226 map.updateLayers("");
227 expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
230 it("respects zoomPrecision", function () {
231 $("body").html($("<div id='map'>"));
232 var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
233 map.updateLayers("");
234 expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
237 expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
241 describe(".distance", function () {
242 it("computes distance between points", function () {
243 var latlng1 = L.latLng(51.76712,-0.00484),
244 latlng2 = L.latLng(51.7675159, -0.0078329);
246 expect(OSM.distance(latlng1, latlng2)).to.be.closeTo(210.664, 0.005);
247 expect(OSM.distance(latlng2, latlng1)).to.be.closeTo(210.664, 0.005);