Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 describe("Creating a map", function() {
A 2   var basic_map, advanced_map, map_with_events, map_with_custom_controls;
3
4   it("should throw an error if element is not defined", function() {
5     expect(function() { new GMaps({}); }).toThrow(new Error('No element defined.'));
6   });
7
8   describe("With basic options", function() {
9     beforeEach(function() {
10       basic_map = basic_map || new GMaps({
11         el : '#basic-map',
12         lat: -12.0433,
13         lng: -77.0283,
14         zoom: 12
15       });
16     });
17
18     it("should create a GMaps object", function() {
19       expect(basic_map).toBeDefined();
20     });
21
22     it("should have centered the map at the initial coordinates", function() {
23       var lat = basic_map.getCenter().lat();
24       var lng = basic_map.getCenter().lng();
25
26       expect(lat).toEqual(-12.0433);
27       expect(lng).toEqual(-77.0283);
28     });
29
30     it("should have the correct zoom", function() {
31       expect(basic_map.getZoom()).toEqual(12);
32     });
33   });
34
35   describe("With advanced controls", function() {
36     beforeEach(function() {
37       advanced_map = advanced_map || new GMaps({
38         el : '#advanced-map',
39         lat: -12.0433,
40         lng: -77.0283,
41         zoomControl : true,
42         panControl : false,
43         streetViewControl : false,
44         mapTypeControl: false,
45         overviewMapControl: false
46       });
47     });
48
49     it("should show the defined controls", function() {
50       expect(advanced_map.map.zoomControl).toBeTruthy();
51       expect(advanced_map.map.panControl).toBeFalsy();
52       expect(advanced_map.map.streetViewControl).toBeFalsy();
53       expect(advanced_map.map.mapTypeControl).toBeFalsy();
54       expect(advanced_map.map.overviewMapControl).toBeFalsy();
55     });
56   });
57
58   describe("With events", function() {
59     var callbacks, current_zoom = 0, current_center = null;
60
61     beforeEach(function() {
62       callbacks = {
63         onclick : function(e) {
64           var lat = e.latLng.lat();
65           var lng = e.latLng.lng();
66
67           map_with_events.addMarker({
68             lat : lat,
69             lng : lng,
70             title : 'New Marker'
71           });
72         },
73         onzoomchanged : function() {
74           console.log('onzoomchanged');
75           current_zoom = this.getZoom();
76         },
77         oncenterchanged : function() {
78           console.log('oncenterchanged');
79           current_center = this.getCenter();
80         }
81       };
82
83       spyOn(callbacks, 'onclick').andCallThrough();
84       spyOn(callbacks, 'onzoomchanged').andCallThrough();
85       spyOn(callbacks, 'oncenterchanged').andCallThrough();
86
87       map_with_events = map_with_events || new GMaps({
88         el : '#map-with-events',
89         lat : -12.0433,
90         lng : -77.0283,
91         click : callbacks.onclick,
92         zoom_changed : callbacks.onzoomchanged,
93         center_changed : callbacks.oncenterchanged
94       });
95     });
96
97     it("should respond to zoom_changed event", function() {
98       map_with_events.map.setZoom(16);
99
100       expect(callbacks.onzoomchanged).toHaveBeenCalled();
101       expect(current_zoom).toEqual(16);
102     });
103
104     it("should respond to center_changed event", function() {
105       map_with_events.map.setCenter(new google.maps.LatLng(-12.0907, -77.0227));
106
107       // Fix for floating-point bug
108       var lat = parseFloat(current_center.lat().toFixed(4));
109       var lng = parseFloat(current_center.lng().toFixed(4));
110
111       expect(callbacks.oncenterchanged).toHaveBeenCalled();
112       expect(lat).toEqual(-12.0907);
113       expect(lng).toEqual(-77.0227);
114     });
115
116     it("should respond to click event", function() {
117       google.maps.event.trigger(map_with_events.map, 'click', {
118         latLng : new google.maps.LatLng(-12.0433, -77.0283)
119       });
120
121       expect(callbacks.onclick).toHaveBeenCalled();
122       expect(map_with_events.markers.length).toEqual(1);
123     });
124
125     afterEach(function() {
126       document.getElementById('map-with-events').innerHTML = '';
127       map_with_events = null;
128     });
129   });
130
131   describe("With custom controls", function() {
132     var callbacks, markers_in_map = 0;
133
134     beforeEach(function() {
135       callbacks = {
136         onclick : function() {
137           map_with_custom_controls.addMarker({
138             lat : map_with_custom_controls.getCenter().lat(),
139             lng : map_with_custom_controls.getCenter().lng()
140           });
141         }
142       }
143
144       spyOn(callbacks, 'onclick').andCallThrough();
145
146       map_with_custom_controls = new GMaps({
147         el : '#map-with-custom-controls',
148         lat : -12.0433,
149         lng : -77.0283
150       });
151
152       map_with_custom_controls.addControl({
153         position : 'top_right',
154         content : 'Add marker at the center',
155         style : {
156           margin: '5px',
157           padding: '1px 6px',
158           border: 'solid 1px #717B87',
159           background: '#fff'
160         },
161         events : {
162           click: callbacks.onclick
163         }
164       });
165     });
166
167     it("should add the control to the controls collection", function() {
168       expect(map_with_custom_controls.controls.length).toEqual(1);
169     });
170
171     it("should respond to click event attached to the custom control", function() {
172       google.maps.event.trigger(map_with_custom_controls.controls[0], 'click');
173
174       expect(callbacks.onclick).toHaveBeenCalled();
175       expect(map_with_custom_controls.markers.length).toEqual(1);
176     });
177   });
178 });