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