-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
MapSpec.js
2640 lines (2144 loc) 路 76.8 KB
/
MapSpec.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {Map, latLng, Point, LatLngBounds, TileLayer, Handler, DomEvent, Bounds, Layer, control as lControl, GridLayer, Util, Marker, Polygon, DivIcon, CircleMarker, Canvas} from 'leaflet';
import UIEventSimulator from 'ui-event-simulator';
import {createContainer, removeMapContainer} from '../SpecHelper.js';
describe('Map', () => {
let container,
map;
beforeEach(() => {
container = container = createContainer();
map = new Map(container);
});
afterEach(() => {
removeMapContainer(map, container);
});
describe('#remove', () => {
let spy;
beforeEach(() => {
spy = sinon.spy();
});
it('fires an unload event if loaded', () => {
map.setView([0, 0], 0);
map.on('unload', spy);
map.remove();
map = null;
expect(spy.called).to.be.true;
});
it('fires no unload event if not loaded', () => {
map.on('unload', spy);
map.remove();
map = null;
expect(spy.called).not.to.be.true;
});
describe('corner case checking', () => {
it('throws an exception upon reinitialization', () => {
expect(() => new Map(container))
.to.throw('Map container is already initialized.');
});
it('throws an exception if a container is not found', () => {
expect(() => new Map('nonexistentdivelement'))
.to.throw('Map container not found.');
});
});
it('undefines container._leaflet_id', () => {
expect(container._leaflet_id).to.be.ok;
map.remove();
map = null;
expect(container._leaflet_id).to.equal(undefined);
});
it('unbinds events', () => {
// before actual test: make sure that events are ok
map.setView([0, 0], 0);
map.on('click', spy);
UIEventSimulator.fire('click', container);
expect(spy.called).to.be.true;
// actual test
spy = sinon.spy();
map.on('click dblclick mousedown mouseup mousemove', spy);
map.remove();
map = null;
UIEventSimulator.fire('click', container);
UIEventSimulator.fire('dblclick', container);
UIEventSimulator.fire('mousedown', container);
UIEventSimulator.fire('mouseup', container);
UIEventSimulator.fire('mousemove', container);
expect(spy.called).to.be.false;
});
it('does not throw if removed during animation', () => {
map.setView([0, 0], 1).setMaxBounds([[0, 1], [2, 3]]);
// Force creation of animation proxy,
// otherwise browser checks disable it
map._createAnimProxy();
// #6775 Remove the map in the middle of the animation
map.on('zoom', map.remove.bind(map));
map.setZoom(2);
});
it('throws error if container is reused by other instance', () => {
map.remove();
const map2 = new Map(container);
expect(() => {
map.remove();
}).to.throw();
map2.remove(); // clean up
map = null;
});
});
describe('#getCenter', () => {
it('throws if not set before', () => {
expect(() => {
map.getCenter();
}).to.throw();
});
it('returns a precise center when zoomed in after being set (#426)', () => {
const center = latLng(10, 10);
map.setView(center, 1);
map.setZoom(19);
expect(map.getCenter()).to.eql(center);
});
it('returns correct center after invalidateSize (#1919)', () => {
const center = latLng(10, 10);
map.setView(center, 1);
map.invalidateSize();
expect(map.getCenter()).not.to.eql(center);
});
it('returns a new object that can be mutated without affecting the map', () => {
map.setView([10, 10], 1);
const center = map.getCenter();
center.lat += 10;
expect(map.getCenter()).to.eql(latLng(10, 10));
});
});
describe('#whenReady', () => {
describe('when the map has not yet been loaded', () => {
it('calls the callback when the map is loaded', () => {
const spy = sinon.spy();
map.whenReady(spy);
expect(spy.called).to.be.false;
map.setView([0, 0], 1);
expect(spy.called).to.be.true;
});
});
describe('when the map has already been loaded', () => {
it('calls the callback immediately', () => {
const spy = sinon.spy();
map.setView([0, 0], 1);
map.whenReady(spy);
expect(spy.called).to.be.true;
});
});
});
describe('#setView', () => {
it('sets the view of the map', () => {
expect(map.setView([51.505, -0.09], 13)).to.equal(map);
expect(map.getZoom()).to.equal(13);
expect(map.getCenter().distanceTo([51.505, -0.09])).to.be.lessThan(5);
});
it('can be passed without a zoom specified', () => {
map.setZoom(13);
expect(map.setView([51.605, -0.11])).to.equal(map);
expect(map.getZoom()).to.equal(13);
expect(map.getCenter().distanceTo([51.605, -0.11])).to.be.lessThan(5);
});
it('limits initial zoom when no zoom specified', () => {
map.options.maxZoom = 20;
map.setZoom(100);
expect(map.setView([51.605, -0.11])).to.equal(map);
expect(map.getZoom()).to.equal(20);
expect(map.getCenter().distanceTo([51.605, -0.11])).to.be.lessThan(5);
});
it('defaults to zoom passed as map option', () => {
const map = new Map(document.createElement('div'), {zoom: 13});
const zoom = map.setView([51.605, -0.11]).getZoom();
map.remove(); // clean up
expect(zoom).to.equal(13);
});
it('passes duration option to panBy', () => {
const map = new Map(document.createElement('div'), {zoom: 13, center: [0, 0]});
map.panBy = sinon.spy();
map.setView([51.605, -0.11], 13, {animate: true, duration: 13});
map.remove(); // clean up
expect(map.panBy.callCount).to.eql(1);
expect(map.panBy.args[0][1].duration).to.eql(13);
});
it('prevents firing movestart noMoveStart', (done) => {
const movestartSpy = sinon.spy();
map.on('movestart', movestartSpy);
const moveendSpy = sinon.spy();
map.on('moveend', moveendSpy);
map.setView([51.505, -0.09], 13, {pan: {noMoveStart: true}});
setTimeout(() => {
expect(movestartSpy.notCalled).to.eql(true);
expect(moveendSpy.calledOnce).to.eql(true);
done();
}, 100);
});
});
describe('#setZoom', () => {
describe('when the map has not yet been loaded', () => {
it('set zoom level is not limited by max zoom', () => {
map.options.maxZoom = 10;
map.setZoom(15);
expect(map.getZoom()).to.equal(15);
});
it('overwrites zoom passed as map option', () => {
const map2 = new Map(document.createElement('div'), {zoom: 13});
map2.setZoom(15);
const zoom = map2.getZoom();
map2.remove(); // clean up
expect(zoom).to.equal(15);
});
});
describe('when the map has been loaded', () => {
beforeEach(() => {
map.setView([0, 0], 0); // loads map
});
it('set zoom level is limited by max zoom', () => {
map.options.maxZoom = 10;
map.setZoom(15);
expect(map.getZoom()).to.equal(10);
});
it('does not overwrite zoom passed as map option', () => {
const map2 = new Map(document.createElement('div'), {zoom: 13});
map2.setView([0, 0]);
map2.setZoom(15);
const zoom = map2.getZoom();
map2.remove(); // clean up
expect(zoom).to.equal(13);
});
});
it('changes previous zoom level', () => {
map.zoom = 10;
map.setZoom(15);
expect(map.getZoom()).to.equal(15);
});
it('can be passed without a zoom specified and keep previous zoom', () => {
const prevZoom = map.getZoom();
map.setZoom();
expect(map.getZoom()).to.equal(prevZoom);
});
it('can be passed with a zoom level of undefined and keep previous zoom', () => {
const prevZoom = map.getZoom();
map.setZoom(undefined);
expect(map.getZoom()).to.equal(prevZoom);
});
it('can be passed with a zoom level of infinity', () => {
map.setZoom(Infinity);
expect(map.getZoom()).to.equal(Infinity);
});
});
describe('#stop', () => {
it('does not try to stop the animation if it wasn\'t set before', () => {
map.setView([50, 50], 10);
map.stop = sinon.spy();
map.panTo([10, 10], 10);
expect(map.stop.called).to.be.false;
});
it('stops the execution of the flyTo animation', () => {
map.setView([0, 0]);
map.stop = sinon.spy();
map.flyTo([51.505, -0.09]);
map.stop();
expect(map.stop.calledOnce).to.be.true;
});
it('stops the execution of the panTo animation', () => {
map.setView([0, 0]);
map.stop = sinon.spy();
map.panTo([51.505, -0.09]);
map.stop();
expect(map.stop.calledOnce).to.be.true;
});
});
describe('#setZoomAround', () => {
beforeEach(() => {
map.setView([0, 0], 0); // loads map
});
it('pass Point and change pixel in view', () => {
const point = new Point(5, 5);
map.setZoomAround(point, 5);
expect(map.getBounds().contains(map.options.crs.pointToLatLng(point, 5))).to.be.false;
});
it('pass Point and change pixel in view at high zoom', () => {
const point = new Point(5, 5);
map.setZoomAround(point, 18);
expect(map.getBounds().contains(map.options.crs.pointToLatLng(point, 18))).to.be.false;
});
it('pass latLng and keep specified latLng in view', () => {
map.setZoomAround([5, 5], 5);
expect(map.getBounds().contains([5, 5])).to.be.true;
});
it('pass latLng and keep specified latLng in view at high zoom fails', () => {
map.setZoomAround([5, 5], 12); // usually fails around 9 zoom level
expect(map.getBounds().contains([5, 5])).to.be.false;
});
it('throws if map is not loaded', () => {
const unloadedMap = new Map(document.createElement('div'));
expect(() => unloadedMap.setZoomAround([5, 5], 4)).to.throw();
});
it('throws if zoom is empty', () => {
expect(() => map.setZoomAround([5, 5])).to.throw();
});
it('throws if zoom is undefined', () => {
expect(() => map.setZoomAround([5, 5], undefined)).to.throw();
});
it('throws if latLng is undefined', () => {
expect(() => map.setZoomAround([undefined, undefined], 4)).to.throw();
});
it('does not throw if latLng is infinity', () => {
map.setView([5, 5]);
map.setZoomAround([Infinity, Infinity], 4);
expect(map.getCenter()).to.be.ok;
});
});
describe('#getBounds', () => {
it('is safe to call from within a moveend callback during initial load (#1027)', () => {
const map = new Map(document.createElement('div'));
map.on('moveend', () => {
map.getBounds();
});
map.setView([51.505, -0.09], 13);
map.remove(); // clean up
});
});
describe('#getBoundsZoom', () => {
const halfLength = 0.00025;
const bounds = [[-halfLength, -halfLength], [halfLength, halfLength]];
const wideBounds = [[-halfLength, -halfLength * 10], [halfLength, halfLength * 10]];
const padding = [100, 100];
const height = '400px';
it('returns high levels of zoom with small areas and big padding', () => {
container.style.height = height;
expect(map.getBoundsZoom(bounds, false, padding)).to.be.equal(19);
});
it('returns multiples of zoomSnap when zoomSnap > 0', () => {
container.style.height = height;
map.options.zoomSnap = 0.5;
expect(map.getBoundsZoom(bounds, false, padding)).to.be.equal(19.5);
map.options.zoomSnap = 0.2;
expect(map.getBoundsZoom(bounds, false, padding)).to.be.equal(19.6);
map.options.zoomSnap = 0;
expect(map.getBoundsZoom(bounds, false, padding)).to.be.within(19.6864560, 19.6864561);
});
it('getBoundsZoom does not return Infinity when projected SE - NW has negative components', () => {
container.style.height = '';
container.style.width = '';
map.setZoom(16);
const bounds = new LatLngBounds(
[62.18475569507688, 6.926335173954951],
[62.140483526511694, 6.923933370740089]);
const padding = new Point(-50, -50);
// control case: default crs
let boundsZoom = map.getBoundsZoom(bounds, false, padding);
expect(boundsZoom).to.eql(9);
// test case: EPSG:25833 (mocked, for simplicity)
// The following coordinates are bounds projected with proj4leaflet crs = EPSG:25833', '+proj=utm +zone=33 +ellps=GRS80 +units=m +no_defs
const crsMock = sinon.mock(map.options.crs);
crsMock.expects('latLngToPoint')
.withExactArgs(bounds.getNorthWest(), 16)
.returns(new Point(7800503.059925064, 6440062.353052008));
crsMock.expects('latLngToPoint')
.withExactArgs(bounds.getSouthEast(), 16)
.returns(new Point(7801987.203481699, 6425186.447901004));
boundsZoom = map.getBoundsZoom(bounds, false, padding);
crsMock.restore();
crsMock.verify(); // ensure that latLngToPoint was called with expected args
expect(boundsZoom).to.eql(7); // result expected for EPSG:25833
});
it('respects the \'inside\' parameter', () => {
container.style.height = height;
container.style.width = '1024px'; // Make sure the width is defined
expect(map.getBoundsZoom(wideBounds, false, padding)).to.be.equal(17);
expect(map.getBoundsZoom(wideBounds, true, padding)).to.be.equal(20);
});
});
describe('#setMaxBounds', () => {
it('aligns pixel-wise map view center with maxBounds center if it cannot move view bounds inside maxBounds (#1908)', () => {
// large view, cannot fit within maxBounds
container.style.width = container.style.height = '1000px';
// maxBounds
const bounds = new LatLngBounds([51.5, -0.05], [51.55, 0.05]);
map.setMaxBounds(bounds, {animate: false});
// set view outside
map.setView(latLng([53.0, 0.15]), 12, {animate: false});
// get center of bounds in pixels
const boundsCenter = map.project(bounds.getCenter()).round();
expect(map.project(map.getCenter()).round()).to.eql(boundsCenter);
});
it('moves map view within maxBounds by changing one coordinate', () => {
// small view, can fit within maxBounds
container.style.width = container.style.height = '200px';
// maxBounds
const bounds = new LatLngBounds([51, -0.2], [52, 0.2]);
map.setMaxBounds(bounds, {animate: false});
// set view outside maxBounds on one direction only
// leaves untouched the other coordinate (that is not already centered)
const initCenter = [53.0, 0.1];
map.setView(latLng(initCenter), 16, {animate: false});
// one pixel coordinate hasn't changed, the other has
const pixelCenter = map.project(map.getCenter()).round();
const pixelInit = map.project(initCenter).round();
expect(pixelCenter.x).to.eql(pixelInit.x);
expect(pixelCenter.y).not.to.eql(pixelInit.y);
// the view is inside the bounds
expect(bounds.contains(map.getBounds())).to.be.true;
});
it('remove listeners when called without arguments', (done) => {
new TileLayer('', {minZoom: 0, maxZoom: 20}).addTo(map);
container.style.width = container.style.height = '500px';
const bounds = new LatLngBounds([51.5, -0.05], [51.55, 0.05]);
map.setMaxBounds(bounds, {animate: false});
map.setMaxBounds();
// set view outside
const center = latLng([0, 0]);
map.once('moveend', () => {
expect(center.equals(map.getCenter())).to.be.true;
done();
});
map.setView(center, 18, {animate: false});
});
it('does not try to remove listeners if it wasn\'t set before', () => {
new TileLayer('', {minZoom: 0, maxZoom: 20}).addTo(map);
container.style.width = container.style.height = '500px';
const bounds = new LatLngBounds([51.5, -0.05], [51.55, 0.05]);
map.off = sinon.spy();
map.setMaxBounds(bounds, {animate: false});
expect(map.off.called).not.to.be.true;
});
it('avoid subpixel / floating point related wobble (#8532)', (done) => {
map.setView([50.450036, 30.5241361], 13);
const spy = sinon.spy();
map.on('moveend', spy);
map.setMaxBounds(map.getBounds());
// Unfortunately this is one of those tests where we need to allow at least one animation tick
setTimeout(() => {
expect(spy.called).to.be.false;
done();
}, 300);
});
});
describe('#setMinZoom and #setMaxZoom', () => {
describe('when map is not loaded', () => {
it('change min and max zoom but not zoom', () => {
map.setZoom(2);
map.setMinZoom(3);
expect(map.getZoom()).to.eql(2);
expect(map.getMinZoom()).to.eql(3);
map.setMaxZoom(7);
expect(map.getZoom()).to.eql(2);
expect(map.getMaxZoom()).to.eql(7);
});
it('do not fire \'zoomlevelschange\'', () => {
const spy = sinon.spy();
map.on('zoomlevelschange', spy);
map.setZoom(5);
map.setMinZoom(3);
map.setMaxZoom(7);
expect(map.getZoom()).to.eql(5);
expect(map.getMinZoom()).to.eql(3);
expect(map.getMaxZoom()).to.eql(7);
expect(spy.called).to.be.false;
});
});
describe('when map is loaded', () => {
let spy;
beforeEach(() => {
map.setView([0, 0], 4); // loads map
spy = sinon.spy();
map.on('zoomlevelschange', spy);
});
it('do not fire \'zoomlevelschange\' if zoom level did not change', () => {
map.setMinZoom(2);
map.setMaxZoom(7);
expect(map.getZoom()).to.eql(4);
expect(map.getMinZoom()).to.eql(2);
expect(map.getMaxZoom()).to.eql(7);
expect(spy.calledTwice).to.be.true;
const postSpy = sinon.spy();
map.on('zoomlevelschange', postSpy);
map.setMinZoom(2);
map.setMaxZoom(7);
expect(postSpy.called).to.be.false;
});
it('fire \'zoomlevelschange\' but do not change zoom if max/min zoom is less/more current zoom', () => {
map.setMinZoom(2);
map.setMaxZoom(7);
expect(map.getZoom()).to.eql(4);
expect(map.getMinZoom()).to.eql(2);
expect(map.getMaxZoom()).to.eql(7);
expect(spy.calledTwice).to.be.true;
});
});
it('reset min/max zoom if set to undefined or missing param', () => {
map.setMinZoom(undefined);
map.setMaxZoom();
expect(map.options.minZoom).to.equal(undefined);
expect(map.options.maxZoom).to.equal(undefined);
expect(map.getMinZoom()).to.equal(0); // min layer zoom used instead
expect(map.getMaxZoom()).to.equal(Infinity); // max layer zoom used instead
});
it('allow infinity to be passed', () => {
map.setMinZoom(Infinity);
map.setMaxZoom(Infinity);
expect(map.getMinZoom()).to.equal(Infinity);
expect(map.getMaxZoom()).to.equal(Infinity);
});
});
describe('#getMinZoom and #getMaxZoom', () => {
describe('#getMinZoom', () => {
it('returns 0 if not set by Map options or TileLayer options', () => {
expect(map.getMinZoom()).to.equal(0);
});
});
it('minZoom and maxZoom options overrides any minZoom and maxZoom set on layers', () => {
removeMapContainer(map, container);
container = createContainer();
map = new Map(container, {minZoom: 2, maxZoom: 20});
new TileLayer('', {minZoom: 4, maxZoom: 10}).addTo(map);
new TileLayer('', {minZoom: 6, maxZoom: 17}).addTo(map);
new TileLayer('', {minZoom: 0, maxZoom: 22}).addTo(map);
expect(map.getMinZoom()).to.equal(2);
expect(map.getMaxZoom()).to.equal(20);
});
it('layer minZoom overrides map zoom if map has no minZoom set and layer minZoom is bigger than map zoom', () => {
removeMapContainer(map, container);
container = createContainer();
map = new Map(container, {zoom: 10});
new TileLayer('', {minZoom: 15}).addTo(map);
expect(map.getMinZoom()).to.equal(15);
});
it('layer maxZoom overrides map zoom if map has no maxZoom set and layer maxZoom is smaller than map zoom', () => {
removeMapContainer(map, container);
container = createContainer();
map = new Map(container, {zoom: 20});
new TileLayer('', {maxZoom: 15}).addTo(map);
expect(map.getMaxZoom()).to.equal(15);
});
it('map\'s zoom is adjusted to layer\'s minZoom even if initialized with smaller value', () => {
removeMapContainer(map, container);
container = createContainer();
map = new Map(container, {zoom: 10});
new TileLayer('', {minZoom: 15}).addTo(map);
expect(map.getZoom()).to.equal(15);
});
it('map\'s zoom is adjusted to layer\'s maxZoom even if initialized with larger value', () => {
removeMapContainer(map, container);
container = createContainer();
map = new Map(container, {zoom: 20});
new TileLayer('', {maxZoom: 15}).addTo(map);
expect(map.getZoom()).to.equal(15);
});
});
describe('#addHandler', () => {
function getHandler(callback = () => {}) {
return Handler.extend({
addHooks() {
DomEvent.on(window, 'click', this.handleClick, this);
},
removeHooks() {
DomEvent.off(window, 'click', this.handleClick, this);
},
handleClick: callback
});
}
it('checking enabled method', () => {
map.addHandler('clickHandler', getHandler());
expect(map.clickHandler.enabled()).to.eql(false);
map.clickHandler.enable();
expect(map.clickHandler.enabled()).to.eql(true);
map.clickHandler.disable();
expect(map.clickHandler.enabled()).to.eql(false);
});
it('automatically enabled, when has a property named the same as the handler', () => {
map.remove();
map = new Map(container, {clickHandler: true});
map.addHandler('clickHandler', getHandler());
expect(map.clickHandler.enabled()).to.eql(true);
});
it('checking handling events when enabled/disabled', () => {
const spy = sinon.spy();
map.addHandler('clickHandler', getHandler(spy));
UIEventSimulator.fire('click', window);
UIEventSimulator.fire('click', window);
expect(spy.called).not.to.be.true;
map.clickHandler.enable();
UIEventSimulator.fire('click', window);
expect(spy.called).to.be.true;
map.clickHandler.disable();
UIEventSimulator.fire('click', window);
expect(spy.callCount).to.eql(1);
});
});
describe('createPane', () => {
it('create a new pane to mapPane when container not specified', () => {
map.createPane('controlPane');
expect(map.getPane('controlPane').className).to.eql('leaflet-pane leaflet-control-pane');
});
it('create a new pane to container specified', () => {
map.createPane('controlPane', map.getPane('tooltipPane'));
expect(map.getPane('controlPane').parentElement.className).to.eql(
'leaflet-pane leaflet-tooltip-pane');
});
it('create a new pane to mapPane when container is invalid', () => {
map.createPane('controlPane', undefined);
expect(map.getPane('controlPane').parentElement.className).to.eql(
'leaflet-pane leaflet-map-pane');
});
it('replace same named pane', () => {
const overlayPane = map.getPane('overlayPane');
map.createPane('overlayPane');
expect(map.getPane('overlayPane')).to.not.equal(overlayPane);
});
});
describe('#getPane', () => {
it('return pane by String', () => {
expect(map.getPane('tilePane').className).to.eql('leaflet-pane leaflet-tile-pane');
});
it('return pane by pane', () => {
expect(map.getPane(map.getPanes()['shadowPane']).className).to.eql(
'leaflet-pane leaflet-shadow-pane');
});
it('return empty pane when not found', () => {
expect(map.getPane('foo bar')).to.eql(undefined);
});
});
describe('#getPanes', () => {
it('return all default panes', () => {
const keys = Object.keys(map.getPanes());
expect(keys).to.eql(
['mapPane', 'tilePane', 'overlayPane', 'shadowPane', 'markerPane', 'tooltipPane', 'popupPane']);
});
it('return empty pane when map deleted', () => {
const map2 = new Map(document.createElement('div'));
map2.remove();
expect(map2.getPanes()).to.eql({});
});
});
describe('#getContainer', () => {
it('return container object', () => {
expect(map.getContainer()._leaflet_id).to.be.ok;
});
it('return undefined on empty container id', () => {
const container2 = createContainer();
const map2 = new Map(container2);
map2.remove(); // clean up
expect(map2.getContainer()._leaflet_id).to.eql(undefined);
});
});
describe('#getSize', () => {
it('return map size in pixels', () => {
expect(map.getSize()).to.eql(new Point(400, 400));
});
it('return map size if not specified', () => {
const map2 = new Map(document.createElement('div'));
expect(map2.getSize()).to.eql(new Point(0, 0));
map2.remove(); // clean up
});
it('return map size if 0x0 pixels', () => {
container.style.width = '0px';
container.style.height = '0px';
expect(map.getSize()).to.eql(new Point(0, 0));
});
it('return new pixels on change', () => {
container.style.width = '300px';
expect(map.getSize()).to.eql(new Point(300, 400));
});
it('return clone of size object from map', () => {
expect(map.getSize()).to.not.equal(map._size);
});
it('return previous size on empty map', () => {
const container2 = createContainer();
const map2 = new Map(container2);
map2.remove(); // clean up
expect(map2.getSize()).to.eql(new Point(400, 400));
});
});
describe('#getPixelBounds', () => {
beforeEach(() => {
map.setView([0, 0], 0); // load map
});
it('return map bounds in pixels', () => {
expect(map.getPixelBounds()).to.eql(new Bounds([-72, -72], [328, 328]));
});
it('return changed map bounds if really zoomed in', () => {
map.setZoom(20);
expect(map.getPixelBounds()).to.eql(new Bounds([134217528, 134217528], [134217928, 134217928]));
});
it('return new pixels on view change', () => {
map.setView([50, 50], 5);
expect(map.getPixelBounds()).to.eql(new Bounds([5034, 2578], [5434, 2978]));
});
it('throw error if center and zoom were not set / map not loaded', () => {
const container2 = createContainer();
const map2 = new Map(container2);
expect(map2.getPixelBounds).to.throw();
map2.remove(); // clean up
});
});
describe('#getPixelOrigin', () => {
beforeEach(() => {
map.setView([0, 0], 0); // load map
});
it('return pixel origin', () => {
expect(map.getPixelOrigin()).to.eql(new Point(-72, -72));
});
it('return new pixels on view change', () => {
map.setView([50, 50], 5);
expect(map.getPixelOrigin()).to.eql(new Point(5034, 2578));
});
it('return changed map bounds if really zoomed in', () => {
map.setZoom(20);
expect(map.getPixelOrigin()).to.eql(new Point(134217528, 134217528));
});
it('throw error if center and zoom were not set / map not loaded', () => {
const container2 = createContainer();
const map2 = new Map(container2);
expect(map2.getPixelOrigin).to.throw();
map2.remove(); // clean up
});
});
describe('#getPixelWorldBounds', () => {
it('return map bounds in pixels', () => {
expect(map.getPixelWorldBounds()).to.eql(new Bounds(
[5.551115123125783e-17, 5.551115123125783e-17], [1, 1]));
});
it('return changed map bounds if really zoomed in', () => {
map.setZoom(20);
expect(map.getPixelWorldBounds()).to.eql(new Bounds(
[1.4901161193847656e-8, 1.4901161193847656e-8], [268435456, 268435456]));
});
it('return new pixels on zoom change', () => {
map.setZoom(5);
expect(map.getPixelWorldBounds()).to.eql(new Bounds(
[4.547473508864641e-13, 4.547473508864641e-13], [8192, 8192]));
map.setView([0, 0]);
// view does not change pixel world bounds
expect(map.getPixelWorldBounds()).to.eql(new Bounds(
[4.547473508864641e-13, 4.547473508864641e-13], [8192, 8192]));
});
it('return infinity bounds on infinity zoom', () => {
map.setZoom(Infinity);
expect(map.getPixelWorldBounds()).to.eql(new Bounds(
[Infinity, Infinity], [Infinity, Infinity]));
});
});
describe('#hasLayer', () => {
it('throws when called without proper argument', () => {
const hasLayer = map.hasLayer.bind(map);
expect(() => hasLayer(new Layer())).to.not.throw(); // control case
expect(() => hasLayer(undefined)).to.throw();
expect(() => hasLayer(null)).to.throw();
expect(() => hasLayer(false)).to.throw();
expect(() => hasLayer()).to.throw();
});
});
function layerSpy() {
const layer = new Layer();
layer.onAdd = sinon.spy();
layer.onRemove = sinon.spy();
return layer;
}
describe('#addLayer', () => {
it('calls layer.onAdd immediately if the map is ready', () => {
const layer = layerSpy();
map.setView([0, 0], 0);
map.addLayer(layer);
expect(layer.onAdd.called).to.be.true;
});
it('calls layer.onAdd when the map becomes ready', () => {
const layer = layerSpy();
map.addLayer(layer);
expect(layer.onAdd.called).not.to.be.true;
map.setView([0, 0], 0);
expect(layer.onAdd.called).to.be.true;
});
it('does not call layer.onAdd if the layer is removed before the map becomes ready', () => {
const layer = layerSpy();
map.addLayer(layer);
map.removeLayer(layer);
map.setView([0, 0], 0);
expect(layer.onAdd.called).not.to.be.true;
});
it('fires a layeradd event immediately if the map is ready', () => {
const layer = layerSpy(),
spy = sinon.spy();
map.on('layeradd', spy);
map.setView([0, 0], 0);
map.addLayer(layer);
expect(spy.called).to.be.true;
});
it('fires a layeradd event when the map becomes ready', () => {
const layer = layerSpy(),
spy = sinon.spy();
map.on('layeradd', spy);
map.addLayer(layer);
expect(spy.called).not.to.be.true;
map.setView([0, 0], 0);
expect(spy.called).to.be.true;
});
it('does not fire a layeradd event if the layer is removed before the map becomes ready', () => {
const layer = layerSpy(),
spy = sinon.spy();
map.on('layeradd', spy);
map.addLayer(layer);
map.removeLayer(layer);
map.setView([0, 0], 0);
expect(spy.called).not.to.be.true;
});
it('adds the layer before firing layeradd', (done) => {
const layer = layerSpy();
map.on('layeradd', () => {
expect(map.hasLayer(layer)).to.be.true;