Skip to content

Commit

Permalink
Add tests for setInteractive (Leaflet#5442)
Browse files Browse the repository at this point in the history
  • Loading branch information
someonewithpc committed Jan 11, 2024
1 parent 9966b8a commit b8abff6
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 0 deletions.
109 changes: 109 additions & 0 deletions 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;
Expand Down Expand Up @@ -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;
}
});
});
});
60 changes: 60 additions & 0 deletions spec/suites/layer/PopupSpec.js
Expand Up @@ -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);
});
});
});
60 changes: 60 additions & 0 deletions spec/suites/layer/TooltipSpec.js
Expand Up @@ -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);
});
});
});
108 changes: 108 additions & 0 deletions spec/suites/layer/marker/MarkerSpec.js
Expand Up @@ -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', () => {

Expand Down

0 comments on commit b8abff6

Please sign in to comment.