diff --git a/spec/suites/layer/ImageOverlaySpec.js b/spec/suites/layer/ImageOverlaySpec.js index 409516ab862..ddfd6e67312 100644 --- a/spec/suites/layer/ImageOverlaySpec.js +++ b/spec/suites/layer/ImageOverlaySpec.js @@ -1,5 +1,6 @@ import {Map, imageOverlay, LatLngBounds} from 'leaflet'; import {createContainer, removeMapContainer} from '../SpecHelper.js'; +import UIEventSimulator from 'ui-event-simulator'; describe('ImageOverlay', () => { let container, map; @@ -167,4 +168,112 @@ describe('ImageOverlay', () => { }); } }); + + describe('#setInteractive', () => { + it('cannot be called before adding to map', () => { + const overlay = imageOverlay(); + + expect(() => overlay.setInteractive(true)).to.throw(); + }); + + it('can set interactive if unset initially', () => { + const overlay = imageOverlay('', imageBounds, {interactive: false}); + map.addLayer(overlay); + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.false; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.false; + } + + overlay.setInteractive(true); + + expect(overlay.options.interactive).to.be.true; + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.true; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.true; + } + }); + + it('can unset interactive if set initially', () => { + const overlay = imageOverlay('', imageBounds, {interactive: true}); + map.addLayer(overlay); + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.true; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.true; + } + + overlay.setInteractive(false); + + expect(overlay.options.interactive).to.be.false; + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.false; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.false; + } + }); + + it('can set interactive if set initially', () => { + const overlay = imageOverlay('', imageBounds, {interactive: true}); + map.addLayer(overlay); + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.true; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.true; + } + + overlay.setInteractive(true); + + expect(overlay.options.interactive).to.be.true; + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.true; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.true; + } + }); + + it('can unset interactive if unset initially', () => { + const overlay = imageOverlay('', imageBounds, {interactive: false}); + map.addLayer(overlay); + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.false; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.false; + } + + overlay.setInteractive(false); + + expect(overlay.options.interactive).to.be.false; + + { + const spy = sinon.spy(); + expect(overlay._image.classList.contains('leaflet-interactive')).to.be.false; + overlay.on('click', spy); + UIEventSimulator.fire('click', overlay._image); + expect(spy.called).to.be.false; + } + }); + }); }); diff --git a/spec/suites/layer/PopupSpec.js b/spec/suites/layer/PopupSpec.js index e662469a99a..4f574634f21 100644 --- a/spec/suites/layer/PopupSpec.js +++ b/spec/suites/layer/PopupSpec.js @@ -636,4 +636,64 @@ describe('Popup', () => { expect(popup._source).to.equal(marker2); }); }); + + describe('#setInteractive', () => { + it('cannot be called before adding to map', () => { + const popup = new Popup().setLatLng(center); + + expect(() => popup.setInteractive(true)).to.throw(); + }); + + it('can set interactive if unset initially', () => { + const popup = new Popup({interactive: false}) + .setLatLng(center) + .openOn(map); + + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(false); + + popup.setInteractive(true); + + expect(popup.options.interactive).to.equal(true); + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(true); + }); + + it('can unset interactive if set initially', () => { + const popup = new Popup({interactive: true}) + .setLatLng(center) + .openOn(map); + + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(true); + + popup.setInteractive(false); + + expect(popup.options.interactive).to.equal(false); + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can unset interactive if unset initially', () => { + const popup = new Popup({interactive: false}) + .setLatLng(center) + .openOn(map); + + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(false); + + popup.setInteractive(false); + + expect(popup.options.interactive).to.equal(false); + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can set interactive if set initially', () => { + const popup = new Popup({interactive: true}) + .setLatLng(center) + .openOn(map); + + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(true); + + popup.setInteractive(true); + + expect(popup.options.interactive).to.equal(true); + expect(popup._container.classList.contains('leaflet-interactive')).to.equal(true); + }); + }); }); diff --git a/spec/suites/layer/TooltipSpec.js b/spec/suites/layer/TooltipSpec.js index f6606ac51bb..d56838477d1 100644 --- a/spec/suites/layer/TooltipSpec.js +++ b/spec/suites/layer/TooltipSpec.js @@ -578,4 +578,64 @@ describe('Tooltip', () => { layer2.openTooltip(); expect(spy2.called).to.be.true; }); + + describe('#setInteractive', () => { + it('cannot be called before adding to map', () => { + const tooltip = new Tooltip().setLatLng(center); + + expect(() => tooltip.setInteractive(true)).to.throw(); + }); + + it('can set interactive if unset initially', () => { + const tooltip = new Tooltip({interactive: false}) + .setLatLng(center) + .openOn(map); + + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(false); + + tooltip.setInteractive(true); + + expect(tooltip.options.interactive).to.equal(true); + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(true); + }); + + it('can unset interactive if set initially', () => { + const tooltip = new Tooltip({interactive: true}) + .setLatLng(center) + .openOn(map); + + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(true); + + tooltip.setInteractive(false); + + expect(tooltip.options.interactive).to.equal(false); + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can unset interactive if unset initially', () => { + const tooltip = new Tooltip({interactive: false}) + .setLatLng(center) + .openOn(map); + + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(false); + + tooltip.setInteractive(false); + + expect(tooltip.options.interactive).to.equal(false); + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can set interactive if set initially', () => { + const tooltip = new Tooltip({interactive: true}) + .setLatLng(center) + .openOn(map); + + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(true); + + tooltip.setInteractive(true); + + expect(tooltip.options.interactive).to.equal(true); + expect(tooltip._container.classList.contains('leaflet-interactive')).to.equal(true); + }); + }); }); diff --git a/spec/suites/layer/marker/MarkerSpec.js b/spec/suites/layer/marker/MarkerSpec.js index 48eecb67e55..21046be974c 100644 --- a/spec/suites/layer/marker/MarkerSpec.js +++ b/spec/suites/layer/marker/MarkerSpec.js @@ -226,6 +226,114 @@ describe('Marker', () => { }); }); + describe('#setInteractive', () => { + it('cannot be called before adding to map', () => { + const marker = new Marker([0, 0]); + + expect(() => marker.setInteractive(true)).to.throw(); + }); + + it('can set interactive if unset initially', () => { + const marker = new Marker([0, 0], {interactive: false, icon: icon1}); + map.addLayer(marker); + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.false; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.false; + } + + marker.setInteractive(true); + + expect(marker.options.interactive).to.be.true; + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.true; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.true; + } + }); + + it('can unset interactive if set initially', () => { + const marker = new Marker([0, 0], {interactive: true, icon: icon1}); + map.addLayer(marker); + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.true; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.true; + } + + marker.setInteractive(false); + + expect(marker.options.interactive).to.be.false; + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.false; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.false; + } + }); + + it('can unset interactive if unset initially', () => { + const marker = new Marker([0, 0], {interactive: false, icon: icon1}); + map.addLayer(marker); + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.false; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.false; + } + + marker.setInteractive(false); + + expect(marker.options.interactive).to.be.false; + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.false; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.false; + } + }); + + it('can set interactive if set initially', () => { + const marker = new Marker([0, 0], {interactive: true, icon: icon1}); + map.addLayer(marker); + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.true; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.true; + } + + marker.setInteractive(true); + + expect(marker.options.interactive).to.be.true; + + { + const spy = sinon.spy(); + expect(marker._icon.classList.contains('leaflet-interactive')).to.be.true; + marker.on('click', spy); + UIEventSimulator.fire('click', marker._icon); + expect(spy.called).to.be.true; + } + }); + }); + describe('#setLatLng', () => { it('fires a move event', () => { diff --git a/spec/suites/layer/vector/PathSpec.js b/spec/suites/layer/vector/PathSpec.js index 95cbcdc9a41..b17a3162e71 100644 --- a/spec/suites/layer/vector/PathSpec.js +++ b/spec/suites/layer/vector/PathSpec.js @@ -49,6 +49,62 @@ describe('Path', () => { }); }); + describe('#setInteractive', () => { + it('cannot be called before adding to map', () => { + const path = new Polyline([[1, 2], [3, 4], [5, 6]]); + + expect(() => path.setInteractive(true)).to.throw(); + }); + + it('can set interactive if unset initially', () => { + const group = new LayerGroup().addTo(map); + const path = new Polyline([[1, 2], [3, 4], [5, 6]], {interactive: false}).addTo(group); + + expect(path._path.classList.contains('leaflet-interactive')).to.equal(false); + + path.setInteractive(true); + + expect(path.options.interactive).to.equal(true); + expect(path._path.classList.contains('leaflet-interactive')).to.equal(true); + }); + + it('can unset interactive if set initially', () => { + const group = new LayerGroup().addTo(map); + const path = new Polyline([[1, 2], [3, 4], [5, 6]], {interactive: true}).addTo(group); + + expect(path._path.classList.contains('leaflet-interactive')).to.equal(true); + + path.setInteractive(false); + + expect(path.options.interactive).to.equal(false); + expect(path._path.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can unset interactive if unset initially', () => { + const group = new LayerGroup().addTo(map); + const path = new Polyline([[1, 2], [3, 4], [5, 6]], {interactive: false}).addTo(group); + + expect(path._path.classList.contains('leaflet-interactive')).to.equal(false); + + path.setInteractive(false); + + expect(path.options.interactive).to.equal(false); + expect(path._path.classList.contains('leaflet-interactive')).to.equal(false); + }); + + it('can set interactive if set initially', () => { + const group = new LayerGroup().addTo(map); + const path = new Polyline([[1, 2], [3, 4], [5, 6]], {interactive: true}).addTo(group); + + expect(path._path.classList.contains('leaflet-interactive')).to.equal(true); + + path.setInteractive(true); + + expect(path.options.interactive).to.equal(true); + expect(path._path.classList.contains('leaflet-interactive')).to.equal(true); + }); + }); + describe('#events', () => { it('fires click event', () => { const spy = sinon.spy();