CK-240: fix multiple select interactions on map

event handlers have to be (de)activated when the interaction is added or removed to the map, not when constructed

added unit test
This commit is contained in:
Geert Premereur
2020-01-08 10:53:00 +01:00
committed by Andreas Hocevar
parent 10c7f08fa4
commit a30a92a963
2 changed files with 57 additions and 5 deletions

View File

@@ -9,6 +9,7 @@ import Interaction from '../../../../src/ol/interaction/Interaction.js';
import Select from '../../../../src/ol/interaction/Select.js';
import VectorLayer from '../../../../src/ol/layer/Vector.js';
import VectorSource from '../../../../src/ol/source/Vector.js';
import Style from '../../../../src/ol/style/Style.js';
describe('ol.interaction.Select', function() {
@@ -406,4 +407,53 @@ describe('ol.interaction.Select', function() {
});
});
describe('clear event listeners on interaction removal', function() {
let firstInteraction, secondInteraction, feature;
beforeEach(function() {
feature = source.getFeatures()[3]; // top feature is selected
const style = new Style({});
const features = new Collection();
firstInteraction = new Select({ style, features });
secondInteraction = new Select({ style, features });
});
afterEach(function() {
map.removeInteraction(secondInteraction);
map.removeInteraction(firstInteraction);
});
// The base case
describe('with a single interaction added', function() {
it('changes the selected feature once', function() {
map.addInteraction(firstInteraction);
const listenerSpy = sinon.spy();
feature.on('change', listenerSpy);
simulateEvent('singleclick', 10, -20, false);
expect(listenerSpy.callCount).to.be(1);
});
});
// The "difficult" case. To prevent regression
describe('with a replaced interaction', function() {
it('changes the selected feature once', function() {
map.addInteraction(firstInteraction);
map.removeInteraction(firstInteraction);
map.addInteraction(secondInteraction);
const listenerSpy = sinon.spy();
feature.on('change', listenerSpy);
simulateEvent('singleclick', 10, -20, false);
expect(listenerSpy.callCount).to.be(1);
});
});
});
});