]> git.openstreetmap.org Git - rails.git/blob - test/javascripts/osm_test.js
Update to i18n-js 4.x
[rails.git] / test / javascripts / osm_test.js
1 //= require jquery
2 //= require js-cookie/dist/js.cookie
3 //= require osm
4 //= require leaflet/dist/leaflet-src
5 //= require leaflet.osm
6 //= require leaflet.map
7
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");
12     });
13
14     it("returns a URL for a node", function () {
15       expect(OSM.apiUrl({ type: "node", id: 10 })).to.eq("/api/0.6/node/10");
16     });
17
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");
20     });
21   });
22
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");
28     });
29   });
30
31   describe(".mapParams", function () {
32     beforeEach(function () {
33       delete OSM.home;
34       delete OSM.location;
35       location.hash = "";
36       document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
37
38       // Test with another cookie set.
39       document.cookie = "_osm_session=deadbeef";
40     });
41
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);
47     });
48
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 });
53
54       params = OSM.mapParams("?way=1");
55       expect(params).to.have.property("object");
56       expect(params.object).to.eql({ type: "way", id: 1 });
57
58       params = OSM.mapParams("?relation=1");
59       expect(params).to.have.property("object");
60       expect(params.object).to.eql({ type: "relation", id: 1 });
61     });
62
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);
67
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);
70     });
71
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);
77
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);
82     });
83
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);
90     });
91
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);
97     });
98
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);
104     });
105
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");
113     });
114
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);
120
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);
125     });
126
127     it("parses layers param", function () {
128       let params = OSM.mapParams("?");
129       expect(params).to.have.property("layers", "");
130
131       document.cookie = "_osm_location=-3.6845|57.6247|5|C";
132       params = OSM.mapParams("?");
133       expect(params).to.have.property("layers", "C");
134
135       location.hash = "#map=5/57.6247/-3.6845&layers=M";
136       params = OSM.mapParams("?");
137       expect(params).to.have.property("layers", "M");
138     });
139   });
140
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);
146     });
147
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");
151     });
152   });
153
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");
158     });
159
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");
163
164
165       args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
166       expect(OSM.formatHash(args)).to.eq("#map=9/57.625/-3.685");
167
168
169       args = { center: L.latLng(57.6247, -3.6845), zoom: 12 };
170       expect(OSM.formatHash(args)).to.eq("#map=12/57.6247/-3.6845");
171     });
172
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");
176     });
177
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");
181     });
182   });
183
184
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);
190     });
191
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);
197     });
198
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);
203     });
204
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);
209     });
210
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);
216     });
217
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);
222     });
223
224     it("suggests 7 digits for z20", function () {
225       expect(OSM.zoomPrecision(20)).to.eq(7);
226     });
227   });
228
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");
235     });
236
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
244       map._zoom = 5;
245       expect(OSM.locationCookie(map)).to.eq("-3.68|57.62|5|M");
246     });
247   });
248
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);
253
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);
256     });
257   });
258 });