Skip to content

Commit

Permalink
Add missing handler tests (#8182)
Browse files Browse the repository at this point in the history
* Add Map.ScrollWheelZoomSpec

* Add Map.DoubleClickZoomSpec

* Don't test center in Firefox because of #7403

* Remove center check
  • Loading branch information
Falke-Design authored Apr 25, 2022
1 parent a8ff4df commit ebe3433
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
4 changes: 3 additions & 1 deletion spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@

<!-- /map/handler -->
<script src="suites/map/handler/Map.BoxZoom.js"></script>
<script src="suites/map/handler/Map.DoubleClickZoomSpec.js"></script>
<script src="suites/map/handler/Map.DragSpec.js"></script>
<script src="suites/map/handler/Map.TouchZoomSpec.js"></script>
<script src="suites/map/handler/Map.KeyboardSpec.js"></script>
<script src="suites/map/handler/Map.ScrollWheelZoomSpec.js"></script>
<script src="suites/map/handler/Map.TapHoldSpec.js"></script>
<script src="suites/map/handler/Map.TouchZoomSpec.js"></script>

<script class="mocha-exec">
window.mocha
Expand Down
3 changes: 3 additions & 0 deletions spec/suites/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ happen.makeEvent = (function (makeEvent) {
if (o.type.substring(0, 7) === 'pointer') {
evt.pointerId = o.pointerId;
evt.pointerType = o.pointerType;
} else if (o.type.indexOf('wheel') > -1) {
evt.deltaY = evt.deltaY || o.deltaY;
evt.deltaMode = evt.deltaMode || o.deltaMode;
}
return evt;
};
Expand Down
52 changes: 52 additions & 0 deletions spec/suites/map/handler/Map.DoubleClickZoomSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe("Map.DoubleClickZoom", function () {
var container, map;

beforeEach(function () {
container = createContainer();
map = L.map(container, {
center: [0, 0],
zoom: 3
});
});

afterEach(function () {
removeMapContainer(map, container);
});

it("zooms in while dblclick", function (done) {
var zoom = map.getZoom();
happen.dblclick(container);

map.on('zoomend', function () {
expect(map.getCenter()).to.be.nearLatLng([17.308687886770034, -17.578125000000004]);
expect(map.getZoom()).to.be.greaterThan(zoom);
done();
});
});

it("zooms out while dblclick and holding shift", function (done) {
var zoom = map.getZoom();
happen.dblclick(container, {shiftKey: true});

map.on('zoomend', function () {
expect(map.getCenter()).to.be.nearLatLng([-33.137551192346145, 35.15625000000001]);
expect(map.getZoom()).to.be.lessThan(zoom);
done();
});
});

it("doubleClickZoom: 'center'", function (done) {
var doubleClickZoomBefore = map.options.doubleClickZoom;
map.options.doubleClickZoom = 'center';
var zoom = map.getZoom();
happen.dblclick(container);

map.on('zoomend', function () {
expect(map.getCenter()).to.be.nearLatLng([0, 0]);
expect(map.getZoom()).to.be.greaterThan(zoom);
map.options.doubleClickZoom = doubleClickZoomBefore;
done();
});
});

});
118 changes: 118 additions & 0 deletions spec/suites/map/handler/Map.ScrollWheelZoomSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
describe("Map.ScrollWheelZoom", function () {
var container, map;
var wheel = 'onwheel' in window ? 'wheel' : 'mousewheel';
var scrollIn = {
type: wheel,
deltaY: -120,
deltaMode: 0
};
var scrollOut = {
type: wheel,
deltaY: 120,
deltaMode: 0
};

beforeEach(function () {
container = createContainer();
map = L.map(container, {
center: [0, 0],
zoom: 3
});
});

afterEach(function () {
removeMapContainer(map, container);
});

it("zooms out while firing 'wheel' event", function (done) {
var zoom = map.getZoom();
happen.once(container, scrollOut);

map.on('zoomend', function () {
// Bug 1.8.0: Firefox wheel zoom makes 2 steps #7403
// expect(map.getCenter()).to.be.nearLatLng([-33.137551192346145, 35.15625000000001]);
expect(map.getZoom()).to.be.lessThan(zoom);
done();
});
});

it("zooms in while firing 'wheel' event", function (done) {
var zoom = map.getZoom();
happen.once(container, scrollIn);

map.on('zoomend', function () {
// Bug 1.8.0: Firefox wheel zoom makes 2 steps #7403
// expect(map.getCenter()).to.be.nearLatLng([17.308687886770034, -17.578125000000004]);
expect(map.getZoom()).to.be.greaterThan(zoom);
done();
});
});

it("scrollWheelZoom: 'center'", function (done) {
var scrollWheelZoomBefore = map.options.scrollWheelZoom;
map.options.scrollWheelZoom = 'center';
var zoom = map.getZoom();
happen.once(container, scrollIn);

map.on('zoomend', function () {
expect(map.getCenter()).to.be.nearLatLng([0, 0]);
expect(map.getZoom()).to.be.greaterThan(zoom);
map.options.scrollWheelZoom = scrollWheelZoomBefore;
done();
});
});

it("changes the option 'wheelDebounceTime'", function (done) {
var wheelDebounceTimeBefore = map.options.wheelDebounceTime;
map.options.wheelDebounceTime = 100;
var zoom = map.getZoom();

var spy = sinon.spy();
map.on('zoomend', spy);

happen.once(container, scrollIn);
setTimeout(function () {
happen.once(container, scrollIn);

expect(spy.notCalled).to.be.ok();
}, 50);

map.on('zoomend', function () {
expect(spy.calledOnce).to.be.ok();
// Bug 1.8.0: Firefox wheel zoom makes 2 steps #7403
// expect(map.getCenter()).to.be.nearLatLng([25.48295117535531, -26.367187500000004]);
expect(map.getZoom()).to.be.greaterThan(zoom);
map.options.wheelDebounceTime = wheelDebounceTimeBefore;
done();
});
});

it("changes the option 'wheelPxPerZoomLevel'", function (done) {
var wheelPxPerZoomLevelBefore = map.options.wheelPxPerZoomLevel;
map.setZoom(15, {animate: false});

var zoom = map.getZoom();
happen.once(container, scrollIn);

map.once('zoomend', function () {
expect(map.getZoom()).to.be.greaterThan(zoom);
var zoomDiff = map.getZoom() - zoom;

// Bug 1.8.0: .once('zoomend') is called twice if call `setZoom` in its function
setTimeout(function () {
map.setZoom(zoom, {animate: false});
expect(map.getZoom()).to.be(zoom);

map.options.wheelPxPerZoomLevel = 30;
happen.once(container, scrollIn);

map.once('zoomend', function () {
expect(map.getZoom()).to.be.greaterThan(zoom);
expect(map.getZoom() - zoom).to.be.greaterThan(zoomDiff);
map.options.wheelPxPerZoomLevel = wheelPxPerZoomLevelBefore;
done();
});
}, 10);
});
});
});

0 comments on commit ebe3433

Please sign in to comment.