1
1
describe ( 'spiderfy' , function ( ) {
2
- var map , div , clock ;
3
- beforeEach ( function ( ) {
4
- clock = sinon . useFakeTimers ( ) ;
5
2
6
- div = document . createElement ( 'div' ) ;
7
- div . style . width = '200px' ;
8
- div . style . height = '200px' ;
9
- document . body . appendChild ( div ) ;
3
+ /**
4
+ * Avoid as much as possible creating and destroying objects for each test.
5
+ * Instead, try re-using them, except for the ones under test of course.
6
+ * PhantomJS does not perform garbage collection for the life of the page,
7
+ * i.e. during the entire test process (Karma runs all tests in a single page).
8
+ * http://stackoverflow.com/questions/27239708/how-to-get-around-memory-error-with-karma-phantomjs
9
+ *
10
+ * The `beforeEach` and `afterEach do not seem to cause much issue.
11
+ * => they can still be used to initialize some setup between each test.
12
+ * Using them keeps a readable spec/index.
13
+ *
14
+ * But refrain from re-creating div and map every time. Re-use those objects.
15
+ */
16
+
17
+ /////////////////////////////
18
+ // SETUP FOR EACH TEST
19
+ /////////////////////////////
20
+
21
+ beforeEach ( function ( ) {
10
22
11
- map = L . map ( div , { maxZoom : 18 } ) ;
23
+ clock = sinon . useFakeTimers ( ) ;
12
24
13
- map . fitBounds ( new L . LatLngBounds ( [
14
- [ 1 , 1 ] ,
15
- [ 2 , 2 ]
16
- ] ) ) ;
17
25
} ) ;
26
+
18
27
afterEach ( function ( ) {
28
+
29
+ if ( group instanceof L . MarkerClusterGroup ) {
30
+ group . clearLayers ( ) ;
31
+ map . removeLayer ( group ) ;
32
+ }
33
+
34
+ // group must be thrown away since we are testing it with a potentially
35
+ // different configuration at each test.
36
+ group = null ;
37
+
19
38
clock . restore ( ) ;
20
- document . body . removeChild ( div ) ;
39
+ clock = null ;
40
+
21
41
} ) ;
22
42
43
+
44
+ /////////////////////////////
45
+ // PREPARATION CODE
46
+ /////////////////////////////
47
+
48
+ var div , map , group , clock ;
49
+
50
+ div = document . createElement ( 'div' ) ;
51
+ div . style . width = '200px' ;
52
+ div . style . height = '200px' ;
53
+ document . body . appendChild ( div ) ;
54
+
55
+ map = L . map ( div , { maxZoom : 18 } ) ;
56
+
57
+ // Corresponds to zoom level 8 for the above div dimensions.
58
+ map . fitBounds ( new L . LatLngBounds ( [
59
+ [ 1 , 1 ] ,
60
+ [ 2 , 2 ]
61
+ ] ) ) ;
62
+
63
+
64
+ /////////////////////////////
65
+ // TESTS
66
+ /////////////////////////////
67
+
23
68
it ( 'Spiderfies 2 Markers' , function ( ) {
24
69
25
- var group = new L . MarkerClusterGroup ( ) ;
70
+ group = new L . MarkerClusterGroup ( ) ;
71
+
26
72
var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
27
73
var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
28
74
38
84
39
85
it ( 'Spiderfies 2 CircleMarkers' , function ( ) {
40
86
41
- var group = new L . MarkerClusterGroup ( ) ;
87
+ group = new L . MarkerClusterGroup ( ) ;
88
+
42
89
var marker = new L . CircleMarker ( [ 1.5 , 1.5 ] ) ;
43
90
var marker2 = new L . CircleMarker ( [ 1.5 , 1.5 ] ) ;
44
91
54
101
55
102
it ( 'Spiderfies 2 Circles' , function ( ) {
56
103
57
- var group = new L . MarkerClusterGroup ( ) ;
104
+ group = new L . MarkerClusterGroup ( ) ;
105
+
58
106
var marker = new L . Circle ( [ 1.5 , 1.5 ] , 10 ) ;
59
107
var marker2 = new L . Circle ( [ 1.5 , 1.5 ] , 10 ) ;
60
108
70
118
71
119
it ( 'Spiderfies at current zoom if all child markers are at the exact same position' , function ( ) {
72
120
73
- var group = new L . MarkerClusterGroup ( ) ;
121
+ group = new L . MarkerClusterGroup ( ) ;
122
+
74
123
var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
75
124
var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
76
125
100
149
101
150
it ( 'Spiderfies at current zoom if all child markers are still within a single cluster at map maxZoom' , function ( ) {
102
151
103
- var group = new L . MarkerClusterGroup ( ) ;
152
+ group = new L . MarkerClusterGroup ( ) ;
153
+
104
154
var marker = new L . Marker ( [ 1.5 , 1.50001 ] ) ;
105
155
var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
106
156
130
180
131
181
} ) ;
132
182
183
+ it ( 'removes all markers and spider legs when group is removed from map' , function ( ) {
184
+
185
+ group = new L . MarkerClusterGroup ( ) ;
186
+
187
+ var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
188
+ var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
189
+
190
+ group . addLayers ( [ marker , marker2 ] ) ;
191
+ map . addLayer ( group ) ;
192
+
193
+ marker . __parent . spiderfy ( ) ;
194
+
195
+ expect ( map . _panes . markerPane . childNodes . length ) . to . be ( 3 ) ; // The 2 markers + semi-transparent cluster.
196
+ expect ( map . _pathRoot . childNodes . length ) . to . be ( 2 ) ; // The 2 spider legs.
197
+
198
+ } ) ;
199
+
200
+ it ( 'adds then removes class "leaflet-cluster-anim" from mapPane on spiderfy' , function ( ) {
201
+
202
+ group = new L . MarkerClusterGroup ( ) ;
203
+
204
+ var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
205
+ var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
206
+
207
+ group . addLayers ( [ marker , marker2 ] ) ;
208
+ map . addLayer ( group ) ;
209
+
210
+ marker . __parent . spiderfy ( ) ;
211
+
212
+ expect ( map . _panes . mapPane . className ) . to . contain ( 'leaflet-cluster-anim' ) ;
213
+
214
+ clock . tick ( 1000 ) ;
215
+
216
+ expect ( map . _panes . mapPane . className ) . to . not . contain ( 'leaflet-cluster-anim' ) ;
217
+
218
+ } ) ;
219
+
220
+ it ( 'adds then removes class "leaflet-cluster-anim" from mapPane on unspiderfy' , function ( ) {
221
+
222
+ group = new L . MarkerClusterGroup ( ) ;
223
+
224
+ var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
225
+ var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
226
+
227
+ group . addLayers ( [ marker , marker2 ] ) ;
228
+ map . addLayer ( group ) ;
229
+
230
+ marker . __parent . spiderfy ( ) ;
231
+
232
+ clock . tick ( 1000 ) ;
233
+
234
+ marker . __parent . unspiderfy ( ) ;
235
+
236
+ expect ( map . _panes . mapPane . className ) . to . contain ( 'leaflet-cluster-anim' ) ;
237
+
238
+ clock . tick ( 1000 ) ;
239
+
240
+ expect ( map . _panes . mapPane . className ) . to . not . contain ( 'leaflet-cluster-anim' ) ;
241
+
242
+ } ) ;
243
+
244
+ it ( 'does not leave class "leaflet-cluster-anim" on mapPane when group is removed while spiderfied' , function ( ) {
245
+
246
+ group = new L . MarkerClusterGroup ( ) ;
247
+
248
+ var marker = new L . Marker ( [ 1.5 , 1.5 ] ) ;
249
+ var marker2 = new L . Marker ( [ 1.5 , 1.5 ] ) ;
250
+
251
+ group . addLayers ( [ marker , marker2 ] ) ;
252
+ map . addLayer ( group ) ;
253
+
254
+ marker . __parent . spiderfy ( ) ;
255
+
256
+ clock . tick ( 1000 ) ;
257
+
258
+ map . removeLayer ( group ) ;
259
+
260
+ expect ( map . _panes . mapPane . className ) . to . not . contain ( 'leaflet-cluster-anim' ) ;
261
+
262
+ } ) ;
263
+
133
264
describe ( 'zoomend event listener' , function ( ) {
265
+
134
266
it ( 'unspiderfies correctly' , function ( ) {
135
267
136
- var group = new L . MarkerClusterGroup ( ) ;
268
+ group = new L . MarkerClusterGroup ( ) ;
269
+
137
270
var marker = new L . Circle ( [ 1.5 , 1.5 ] , 10 ) ;
138
271
var marker2 = new L . Circle ( [ 1.5 , 1.5 ] , 10 ) ;
139
272
150
283
//We should unspiderfy with no animation, so this should be null
151
284
expect ( group . _spiderfied ) . to . be ( null ) ;
152
285
} ) ;
286
+
153
287
} ) ;
154
- } ) ;
288
+
289
+
290
+ /////////////////////////////
291
+ // CLEAN UP CODE
292
+ /////////////////////////////
293
+
294
+ map . remove ( ) ;
295
+ document . body . removeChild ( div ) ;
296
+
297
+ } ) ;
0 commit comments