Add tests for interaction event handlers

This commit is contained in:
Kevin Schmidt
2018-10-03 06:59:18 -06:00
parent 909869b8b9
commit 07349de59b
2 changed files with 122 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ import Map from '../../../../src/ol/Map.js';
import View from '../../../../src/ol/View.js';
import EventTarget from '../../../../src/ol/events/Target.js';
import Interaction, {zoomByDelta} from '../../../../src/ol/interaction/Interaction.js';
import {FALSE} from '../../../../src/ol/functions.js';
describe('ol.interaction.Interaction', function() {
@@ -56,6 +57,36 @@ describe('ol.interaction.Interaction', function() {
});
describe('#handleEvent()', function() {
class MockInteraction extends Interaction {
constructor() {
super(...arguments);
}
handleEvent(mapBrowserEvent) {
return false;
}
}
it('has a default event handler', function() {
const interaction = new Interaction({});
expect(interaction.handleEvent()).to.be(true);
});
it('allows event handler overrides via options', function() {
const interaction = new Interaction({
handleEvent: FALSE
});
expect(interaction.handleEvent()).to.be(false);
});
it('allows event handler overrides via class extension', function() {
const interaction = new MockInteraction({});
expect(interaction.handleEvent()).to.be(false);
});
});
describe('zoomByDelta()', function() {
it('changes view resolution', function() {

View File

@@ -44,4 +44,95 @@ describe('ol.interaction.Pointer', function() {
});
describe('event handlers', function() {
let handleDownCalled, handleDragCalled, handleMoveCalled, handleUpCalled;
const flagHandleDown = function() {
handleDownCalled = true;
};
const flagHandleDrag = function() {
handleDragCalled = true;
};
const flagHandleMove = function() {
handleMoveCalled = true;
};
const flagHandleUp = function() {
handleUpCalled = true;
};
class MockPointerInteraction extends PointerInteraction {
constructor() {
super(...arguments);
}
handleDownEvent(mapBrowserEvent) {
flagHandleDown();
return super.handleDownEvent(mapBrowserEvent);
}
handleDragEvent(mapBrowserEvent) {
flagHandleDrag();
}
handleMoveEvent(mapBrowserEvent) {
flagHandleMove();
}
handleUpEvent(mapBrowserEvent) {
flagHandleUp();
return super.handleUpEvent(mapBrowserEvent);
}
}
beforeEach(function() {
handleDownCalled = false;
handleDragCalled = false;
handleMoveCalled = false;
handleUpCalled = false;
});
it('has default event handlers', function() {
const interaction = new PointerInteraction({});
expect(interaction.handleDownEvent()).to.be(false);
expect(interaction.handleUpEvent()).to.be(false);
});
it('allows event handler overrides via options', function() {
const interaction = new PointerInteraction({
handleDownEvent: flagHandleDown,
handleDragEvent: flagHandleDrag,
handleMoveEvent: flagHandleMove,
handleUpEvent: flagHandleUp
});
interaction.handleDownEvent();
expect(handleDownCalled).to.be(true);
interaction.handleDragEvent();
expect(handleDragCalled).to.be(true);
interaction.handleMoveEvent();
expect(handleMoveCalled).to.be(true);
interaction.handleUpEvent();
expect(handleUpCalled).to.be(true);
});
it('allows event handler overrides via class extension', function() {
const interaction = new MockPointerInteraction({});
interaction.handleDownEvent();
expect(handleDownCalled).to.be(true);
interaction.handleDragEvent();
expect(handleDragCalled).to.be(true);
interaction.handleMoveEvent();
expect(handleMoveCalled).to.be(true);
interaction.handleUpEvent();
expect(handleUpCalled).to.be(true);
});
});
});