lib/leaflet/spec/suites/core/EventsSpec.js in leaflet-js-0.6.beta4 vs lib/leaflet/spec/suites/core/EventsSpec.js in leaflet-js-0.7.0
- old
+ new
@@ -1,23 +1,23 @@
-describe('Events', function() {
+describe('Events', function () {
var Klass;
- beforeEach(function() {
+ beforeEach(function () {
Klass = L.Class.extend({
includes: L.Mixin.Events
});
});
- describe('#fireEvent', function() {
+ describe('#fireEvent', function () {
- it('fires all listeners added through #addEventListener', function() {
+ it('fires all listeners added through #addEventListener', function () {
var obj = new Klass(),
spy1 = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy(),
spy4 = sinon.spy(),
- spy5 = sinon.spy();
+ spy5 = sinon.spy(),
spy6 = sinon.spy();
obj.addEventListener('test', spy1);
obj.addEventListener('test', spy2);
obj.addEventListener('other', spy3);
@@ -40,11 +40,11 @@
expect(spy5.called).to.be(false);
expect(spy6.called).to.be(true);
expect(spy6.callCount).to.be(1);
});
- it('provides event object to listeners and executes them in the right context', function() {
+ it('provides event object to listeners and executes them in the right context', function () {
var obj = new Klass(),
obj2 = new Klass(),
obj3 = new Klass(),
obj4 = new Klass(),
foo = {};
@@ -86,11 +86,11 @@
obj2.fireEvent('test', {baz: 2});
obj3.fireEvent('test', {baz: 3});
obj4.fireEvent('test', {baz: 4});
});
- it('calls no listeners removed through #removeEventListener', function() {
+ it('calls no listeners removed through #removeEventListener', function () {
var obj = new Klass(),
spy = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy(),
spy4 = sinon.spy(),
@@ -169,11 +169,11 @@
expect(spy1.called).to.be(false);
expect(spy2.called).to.be(true);
});
- it('removes listeners with a stamp originally added without one', function() {
+ it('removes listeners with a stamp originally added without one', function () {
var obj = new Klass(),
spy1 = sinon.spy(),
spy2 = sinon.spy(),
foo = {};
@@ -187,10 +187,34 @@
obj.fireEvent('test');
expect(spy1.called).to.be(false);
expect(spy2.called).to.be(false);
});
+
+ it('removes listeners with context == this and a stamp originally added without one', function () {
+ var obj = new Klass(),
+ obj2 = new Klass(),
+ spy1 = sinon.spy(),
+ spy2 = sinon.spy(),
+ spy3 = sinon.spy();
+
+ obj.addEventListener('test', spy1, obj);
+ L.Util.stamp(obj);
+ obj.addEventListener('test', spy2, obj);
+ obj.addEventListener('test', spy3, obj2); // So that there is a contextId based listener, otherwise removeEventListener will do correct behaviour anyway
+
+ obj.removeEventListener('test', spy1, obj);
+ obj.removeEventListener('test', spy2, obj);
+ obj.removeEventListener('test', spy3, obj2);
+
+ obj.fireEvent('test');
+
+ expect(spy1.called).to.be(false);
+ expect(spy2.called).to.be(false);
+ expect(spy3.called).to.be(false);
+ });
+
it('doesnt lose track of listeners when removing non existent ones', function () {
var obj = new Klass(),
spy = sinon.spy(),
spy2 = sinon.spy(),
foo = {},
@@ -208,15 +232,47 @@
obj.fireEvent('test');
expect(spy.called).to.be(false);
});
+
+ it('correctly removes all listeners if given no fn', function () {
+ var obj = new Klass(),
+ spy = sinon.spy(),
+ foo = {},
+ foo2 = {},
+ foo3 = {};
+
+ obj.addEventListener('test', spy, foo2);
+ obj.addEventListener('test', spy, foo3);
+
+ obj.removeEventListener('test'); // Removes both of the above listeners
+
+ expect(obj.hasEventListeners('test')).to.be(false);
+
+ //Add and remove a listener
+ obj.addEventListener('test', spy, foo2);
+ obj.removeEventListener('test', spy, foo2);
+
+ expect(obj.hasEventListeners('test')).to.be(false);
+ });
+
+ it('makes sure an event is not triggered if a listener is removed during dispatch', function () {
+ var obj = new Klass(),
+ spy = sinon.spy();
+
+ obj.addEventListener('test', function () { obj.removeEventListener('test', spy); });
+ obj.addEventListener('test', spy);
+ obj.fireEvent('test');
+
+ expect(spy.called).to.be(false);
+ });
});
- describe('#on, #off & #fire', function() {
+ describe('#on, #off & #fire', function () {
- it('works like #addEventListener && #removeEventListener', function() {
+ it('works like #addEventListener && #removeEventListener', function () {
var obj = new Klass(),
spy = sinon.spy();
obj.on('test', spy);
obj.fire('test');
@@ -227,11 +283,11 @@
obj.fireEvent('test');
expect(spy.callCount).to.be.lessThan(2);
});
- it('does not override existing methods with the same name', function() {
+ it('does not override existing methods with the same name', function () {
var spy1 = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
var Klass2 = L.Class.extend({
@@ -252,14 +308,14 @@
obj.fire();
expect(spy3.called).to.be(true);
});
});
- describe("#clearEventListeners", function() {
- it("clears all registered listeners on an object", function() {
+ describe("#clearEventListeners", function () {
+ it("clears all registered listeners on an object", function () {
var spy = sinon.spy(),
- obj = new Klass()
+ obj = new Klass(),
otherObj = new Klass();
obj.on('test', spy, obj);
obj.on('testTwo', spy);
obj.on('test', spy, otherObj);
@@ -269,12 +325,12 @@
expect(spy.called).to.be(false);
});
});
- describe('#once', function() {
- it('removes event listeners after first trigger', function() {
+ describe('#once', function () {
+ it('removes event listeners after first trigger', function () {
var obj = new Klass(),
spy = sinon.spy();
obj.once('test', spy, obj);
obj.fire('test');
@@ -284,11 +340,11 @@
obj.fire('test');
expect(spy.callCount).to.be.lessThan(2);
});
- it('works with an object hash', function() {
+ it('works with an object hash', function () {
var obj = new Klass(),
spy = sinon.spy(),
otherSpy = sinon.spy();
obj.once({
@@ -319,10 +375,10 @@
obj.fire('test');
expect(spy.called).to.be(false);
});
- it('works if called from a context that doesnt implement #Events', function() {
+ it('works if called from a context that doesnt implement #Events', function () {
var obj = new Klass(),
spy = sinon.spy(),
foo = {};
obj.once('test', spy, foo);