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