]> git.openstreetmap.org Git - rails.git/blob - test/javascripts/osm_test.js
Replace querystring parser with qs yarn module
[rails.git] / test / javascripts / osm_test.js
1 //= require jquery
2 //= require jquery.cookie/jquery.cookie
3 //= require osm
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require leaflet.map
7 //= require i18n/translations
8 //= require qs/dist/qs
9
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");
14     });
15
16     it("returns a URL for a node", function () {
17       expect(OSM.apiUrl({type: "node", id: 10})).to.eq("/api/0.6/node/10");
18     });
19
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");
22     });
23   });
24
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");
30     });
31   });
32
33   describe(".mapParams", function () {
34     beforeEach(function () {
35       delete OSM.home;
36       delete OSM.location;
37       document.location.hash = "";
38       document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
39
40       // Test with another cookie set.
41       document.cookie = "_osm_session=deadbeef";
42     });
43
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);
49     });
50
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});
55
56       params = OSM.mapParams("?way=1");
57       expect(params).to.have.property("object");
58       expect(params.object).to.eql({type: "way", id: 1});
59
60       params = OSM.mapParams("?relation=1");
61       expect(params).to.have.property("object");
62       expect(params.object).to.eql({type: "relation", id: 1});
63     });
64
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);
69
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);
72     });
73
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);
79
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);
84     });
85
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);
92     });
93
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);
99     });
100
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);
106     });
107
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");
115     });
116
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);
122
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);
127     });
128
129     it("parses layers param", function () {
130       var params = OSM.mapParams("?");
131       expect(params).to.have.property("layers", "");
132
133       document.cookie = "_osm_location=-3.6845|57.6247|5|C";
134       params = OSM.mapParams("?");
135       expect(params).to.have.property("layers", "C");
136
137       document.location.hash = "#map=5/57.6247/-3.6845&layers=M"
138       params = OSM.mapParams("?");
139       expect(params).to.have.property("layers", "M");
140     });
141   });
142
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);
148     });
149
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");
153     });
154   });
155
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");
160     });
161
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");
165
166       args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
167       expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
168     });
169
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");
173     });
174
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");
178     });
179   });
180
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);
185     });
186
187     it("suggests 1 digit for z2", function () {
188       expect(OSM.zoomPrecision(2)).to.eq(1);
189     });
190
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);
194     });
195
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);
201     });
202
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);
212     });
213
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);
219     });
220   });
221
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");
228     });
229
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");
235
236       map.setZoom(5);
237       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
238     });
239   });
240
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);
245
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);
248     });
249   });
250 });