Add another events test and fix from @iirvine

This commit is contained in:
danzel 2013-03-15 13:53:03 +13:00
parent 5bbfbd9573
commit 9d4d894450
2 changed files with 22 additions and 1 deletions

View File

@ -181,6 +181,27 @@ describe('Events', function() {
expect(spy1).not.toHaveBeenCalled(); expect(spy1).not.toHaveBeenCalled();
expect(spy2).not.toHaveBeenCalled(); expect(spy2).not.toHaveBeenCalled();
}); });
it('doesnt lose track of listeners when removing non existent ones', function () {
var obj = new Klass(),
spy = jasmine.createSpy(),
spy2 = jasmine.createSpy(),
foo = {},
foo2 = {};
L.Util.stamp(foo);
L.Util.stamp(foo2);
obj.addEventListener('test', spy, foo2);
obj.removeEventListener('test', spy, foo); // Decrements test_idx to 0, even though event listener isn't registered with foo's _leaflet_id
obj.removeEventListener('test', spy, foo2); // Doesn't get removed
obj.addEventListener('test', spy2, foo);
obj.fireEvent('test');
expect(spy).not.toHaveBeenCalled();
});
}); });
describe('#on, #off & #fire', function() { describe('#on, #off & #fire', function() {

View File

@ -99,7 +99,7 @@ L.Mixin.Events = {
} }
} }
if (contextId && listeners.length === 0 && events[objKey]) { if (contextId && listeners.length === 0 && events[objKey] && events[objKey][contextId]) {
objLenKey = objKey + '_len'; objLenKey = objKey + '_len';
delete events[objKey][contextId]; delete events[objKey][contextId];
events[objLenKey] = (events[objLenKey] || 1) - 1; events[objLenKey] = (events[objLenKey] || 1) - 1;