L.Evented.listens() returns false on removed event handlers, #4474 (#4476)

* Fix L.Evented.listens() on removed event handlers, #4474

* Fix bad unit tests for marker events

* Only check for listerners.length if listerners is actually defined
This commit is contained in:
Iván Sánchez Ortega 2016-07-01 11:14:22 +02:00 committed by Per Liedman
parent 2652b63f42
commit 8cb745ce0a
3 changed files with 46 additions and 3 deletions

View File

@ -424,4 +424,42 @@ describe('Events', function () {
expect(spy2.callCount).to.be(2); expect(spy2.callCount).to.be(2);
}); });
}); });
describe('#listens', function () {
it('is false if there is no event handler', function () {
var obj = new L.Evented(),
spy = sinon.spy();
expect(obj.listens('test')).to.be(false);
});
it('is true if there is an event handler', function () {
var obj = new L.Evented(),
spy = sinon.spy();
obj.on('test', spy);
expect(obj.listens('test')).to.be(true);
});
it('is false if event handler has been removed', function () {
var obj = new L.Evented(),
spy = sinon.spy();
obj.on('test', spy);
obj.off('test', spy);
expect(obj.listens('test')).to.be(false);
});
it('changes for a "once" handler', function () {
var obj = new L.Evented(),
spy = sinon.spy();
obj.once('test', spy);
expect(obj.listens('test')).to.be(true);
obj.fire('test');
expect(obj.listens('test')).to.be(false);
});
});
}); });

View File

@ -287,17 +287,19 @@ describe("Marker", function () {
expect(mapSpy.called).not.to.be.ok(); expect(mapSpy.called).not.to.be.ok();
}); });
it("do not catch event if it does not listen to it", function () { it("do not catch event if it does not listen to it", function (done) {
var marker = new L.Marker([55, 37]); var marker = new L.Marker([55, 37]);
map.addLayer(marker); map.addLayer(marker);
marker.once('mousemove', function (e) { marker.once('mousemove', function (e) {
// It should be the marker coordinates // It should be the marker coordinates
expect(e.latlng).to.be.nearLatLng(marker.getLatLng()); expect(e.latlng.equals(marker.getLatLng())).to.be.equal(true);
}); });
happen.mousemove(marker._icon); happen.mousemove(marker._icon);
map.once('mousemove', function (e) { map.once('mousemove', function (e) {
// It should be the mouse coordinates, not the marker ones // It should be the mouse coordinates, not the marker ones
expect(e.latlng).not.to.be.nearLatLng(marker.getLatLng()); expect(e.latlng.equals(marker.getLatLng())).to.be.equal(false);
done();
}); });
happen.mousemove(marker._icon); happen.mousemove(marker._icon);
}); });

View File

@ -169,6 +169,9 @@ L.Evented = L.Class.extend({
break; break;
} }
} }
if (listeners.length === 0) {
delete events[type];
}
} }
} }