]> git.openstreetmap.org Git - rails.git/blob - test/javascripts/osm_test.js
Update to iD v1.5.1
[rails.git] / test / javascripts / osm_test.js
1 //= require jquery
2 //= require jquery.cookie
3 //= require osm
4 //= require leaflet
5 //= require leaflet.osm
6 //= require leaflet.map
7 //= require i18n/translations
8 //= require querystring
9
10 var querystring = require('querystring-component');
11
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");
16     });
17
18     it("returns a URL for a node", function () {
19       expect(OSM.apiUrl({type: "node", id: 10})).to.eq("/api/0.6/node/10");
20     });
21
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");
24     });
25   });
26
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");
32     });
33   });
34
35   describe(".mapParams", function () {
36     beforeEach(function () {
37       delete OSM.home;
38       delete OSM.location;
39       document.location.hash = "";
40       document.cookie = "_osm_location=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
41
42       // Test with another cookie set.
43       document.cookie = "_osm_session=deadbeef";
44     });
45
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);
51     });
52
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});
57
58       params = OSM.mapParams("?way=1");
59       expect(params).to.have.property("object");
60       expect(params.object).to.eql({type: "way", id: 1});
61
62       params = OSM.mapParams("?relation=1");
63       expect(params).to.have.property("object");
64       expect(params.object).to.eql({type: "relation", id: 1});
65     });
66
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);
71
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);
74     });
75
76     it("parses lat/lon/zoom params", function () {
77       var params = OSM.mapParams("?lat=57.6247&lon=-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", 5);
81
82       params = OSM.mapParams("?lat=57.6247&lon=-3.6845&zoom=10");
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", 10);
86
87       params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845");
88       expect(params).to.have.property("lat", 57.6247);
89       expect(params).to.have.property("lon", -3.6845);
90       expect(params).to.have.property("zoom", 12);
91
92       params = OSM.mapParams("?mlat=57.6247&mlon=-3.6845&zoom=16");
93       expect(params).to.have.property("lat", 57.6247);
94       expect(params).to.have.property("lon", -3.6845);
95       expect(params).to.have.property("zoom", 16);
96     });
97
98     it("parses lat/lon/zoom from the hash", function () {
99       document.location.hash = "#map=16/57.6247/-3.6845";
100       params = OSM.mapParams("?");
101       expect(params).to.have.property("lat", 57.6247);
102       expect(params).to.have.property("lon", -3.6845);
103       expect(params).to.have.property("zoom", 16);
104     });
105
106     it("sets lat/lon from OSM.home", function () {
107       OSM.home = {lat: 57.6247, lon: -3.6845};
108       var params = OSM.mapParams("?");
109       expect(params).to.have.property("lat", 57.6247);
110       expect(params).to.have.property("lon", -3.6845);
111     });
112
113     it("sets bbox from OSM.location", function () {
114       OSM.location = {minlon: -3.6845, minlat: 57.6247, maxlon: -3.7845, maxlat: 57.7247};
115       var expected = L.latLngBounds([57.6247, -3.6845], [57.7247, -3.7845]);
116       var params = OSM.mapParams("?");
117       expect(params).to.have.property("bounds").deep.equal(expected);
118     });
119
120     it("parses params from the _osm_location cookie", function () {
121       document.cookie = "_osm_location=-3.6845|57.6247|5|M";
122       var params = OSM.mapParams("?");
123       expect(params).to.have.property("lat", 57.6247);
124       expect(params).to.have.property("lon", -3.6845);
125       expect(params).to.have.property("zoom", 5);
126       expect(params).to.have.property("layers", "M");
127     });
128
129     it("defaults lat/lon to London", function () {
130       var params = OSM.mapParams("?");
131       expect(params).to.have.property("lat", 51.5);
132       expect(params).to.have.property("lon", -0.1);
133       expect(params).to.have.property("zoom", 5);
134
135       params = OSM.mapParams("?zoom=10");
136       expect(params).to.have.property("lat", 51.5);
137       expect(params).to.have.property("lon", -0.1);
138       expect(params).to.have.property("zoom", 10);
139     });
140
141     it("parses layers param", function () {
142       var params = OSM.mapParams("?");
143       expect(params).to.have.property("layers", "");
144
145       document.cookie = "_osm_location=-3.6845|57.6247|5|C";
146       params = OSM.mapParams("?");
147       expect(params).to.have.property("layers", "C");
148
149       document.location.hash = "#map=5/57.6247/-3.6845&layers=M"
150       params = OSM.mapParams("?");
151       expect(params).to.have.property("layers", "M");
152     });
153   });
154
155   describe(".parseHash", function () {
156     it("parses lat/lon/zoom params", function () {
157       var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
158       expect(args).to.have.property("center").deep.equal(L.latLng(57.6247, -3.6845));
159       expect(args).to.have.property("zoom", 5);
160     });
161
162     it("parses layers params", function () {
163       var args = OSM.parseHash("#map=5/57.6247/-3.6845&layers=M");
164       expect(args).to.have.property("layers", "M");
165     });
166   });
167
168   describe(".formatHash", function () {
169     it("formats lat/lon/zoom params", function () {
170       var args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
171       expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
172     });
173
174     it("respects zoomPrecision", function () {
175       var args = { center: L.latLng(57.6247, -3.6845), zoom: 5 };
176       expect(OSM.formatHash(args)).to.eq("#map=5/57.625/-3.685");
177
178       args = { center: L.latLng(57.6247, -3.6845), zoom: 9 };
179       expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
180     });
181
182     it("formats layers params", function () {
183       var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "C" };
184       expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845&layers=C");
185     });
186
187     it("ignores default layers", function () {
188       var args = { center: L.latLng(57.6247, -3.6845), zoom: 9, layers: "M" };
189       expect(OSM.formatHash(args)).to.eq("#map=9/57.6247/-3.6845");
190     });
191   });
192
193   describe(".zoomPrecision", function () {
194     it("suggests 0 digits for z0-1", function () {
195       expect(OSM.zoomPrecision(0)).to.eq(0);
196       expect(OSM.zoomPrecision(1)).to.eq(0);
197     });
198
199     it("suggests 1 digit for z2", function () {
200       expect(OSM.zoomPrecision(2)).to.eq(1);
201     });
202
203     it("suggests 2 digits for z3-4", function () {
204       expect(OSM.zoomPrecision(3)).to.eq(2);
205       expect(OSM.zoomPrecision(4)).to.eq(2);
206     });
207
208     it("suggests 3 digits for z5-8", function () {
209       expect(OSM.zoomPrecision(5)).to.eq(3);
210       expect(OSM.zoomPrecision(6)).to.eq(3);
211       expect(OSM.zoomPrecision(7)).to.eq(3);
212       expect(OSM.zoomPrecision(8)).to.eq(3);
213     });
214
215     it("suggests 4 digits for z9-16", function () {
216       expect(OSM.zoomPrecision(9)).to.eq(4);
217       expect(OSM.zoomPrecision(10)).to.eq(4);
218       expect(OSM.zoomPrecision(11)).to.eq(4);
219       expect(OSM.zoomPrecision(12)).to.eq(4);
220       expect(OSM.zoomPrecision(13)).to.eq(4);
221       expect(OSM.zoomPrecision(14)).to.eq(4);
222       expect(OSM.zoomPrecision(15)).to.eq(4);
223       expect(OSM.zoomPrecision(16)).to.eq(4);
224     });
225
226     it("suggests 5 digits for z17-20", function () {
227       expect(OSM.zoomPrecision(17)).to.eq(5);
228       expect(OSM.zoomPrecision(18)).to.eq(5);
229       expect(OSM.zoomPrecision(19)).to.eq(5);
230       expect(OSM.zoomPrecision(20)).to.eq(5);
231     });
232   });
233
234   describe(".locationCookie", function () {
235     it("creates a location cookie value", function () {
236       $("body").html($("<div id='map'>"));
237       var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
238       map.updateLayers("");
239       expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
240     });
241
242     it("respects zoomPrecision", function () {
243       $("body").html($("<div id='map'>"));
244       var map = new L.OSM.Map("map", { center: [57.6247, -3.6845], zoom: 9 });
245       map.updateLayers("");
246       expect(OSM.locationCookie(map)).to.eq("-3.6845|57.6247|9|M");
247
248       map.setZoom(5);
249       expect(OSM.locationCookie(map)).to.eq("-3.685|57.625|5|M");
250     });
251   });
252 });