2013-11-08 05:54:33 +08:00
|
|
|
describe('Events', function () {
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
describe('#fireEvent', function () {
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('fires all listeners added through #addEventListener', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
spy3 = sinon.spy(),
|
|
|
|
spy4 = sinon.spy(),
|
|
|
|
spy5 = sinon.spy(),
|
|
|
|
spy6 = sinon.spy();
|
2013-03-02 05:49:20 +08:00
|
|
|
|
|
|
|
obj.addEventListener('test', spy1);
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.addEventListener('test', spy2);
|
|
|
|
obj.addEventListener('other', spy3);
|
2015-01-29 01:32:27 +08:00
|
|
|
obj.addEventListener({test: spy4, other: spy5});
|
2013-12-19 12:07:39 +08:00
|
|
|
// obj.addEventListener({'test other': spy6 });
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy1.called).to.be(false);
|
|
|
|
expect(spy2.called).to.be(false);
|
|
|
|
expect(spy3.called).to.be(false);
|
|
|
|
expect(spy4.called).to.be(false);
|
|
|
|
expect(spy5.called).to.be(false);
|
2013-12-19 12:07:39 +08:00
|
|
|
// expect(spy6.called).to.be(false);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.fireEvent('test');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy1.called).to.be(true);
|
|
|
|
expect(spy2.called).to.be(true);
|
|
|
|
expect(spy3.called).to.be(false);
|
|
|
|
expect(spy4.called).to.be(true);
|
|
|
|
expect(spy5.called).to.be(false);
|
2013-12-19 12:07:39 +08:00
|
|
|
// expect(spy6.called).to.be(true);
|
|
|
|
// expect(spy6.callCount).to.be(1);
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
|
|
|
|
2016-08-02 22:53:06 +08:00
|
|
|
it('fires all listeners in the order they are added', function () {
|
|
|
|
var obj = new L.Evented(),
|
|
|
|
ctx1 = new L.Class(),
|
|
|
|
ctx2 = new L.Class(),
|
|
|
|
count = {one: 0, two: 0, three: 0, four: 0};
|
|
|
|
|
|
|
|
function listener1(e) {
|
|
|
|
count.one++;
|
|
|
|
expect(count.two).to.eql(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function listener2(e) {
|
|
|
|
count.two++;
|
|
|
|
expect(count.one).to.eql(1);
|
|
|
|
expect(count.three).to.eql(0);
|
|
|
|
if (count.two === 1) {
|
|
|
|
expect(this).to.eql(ctx2);
|
|
|
|
} else if (count.two === 2) {
|
|
|
|
expect(this).to.eql(ctx1);
|
|
|
|
} else {
|
|
|
|
expect(this).to.eql(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listener3(e) {
|
|
|
|
count.three++;
|
|
|
|
expect(count.two).to.eql(3);
|
|
|
|
expect(count.four).to.eql(0);
|
|
|
|
if (count.three === 1) {
|
|
|
|
expect(this).to.eql(ctx1);
|
|
|
|
} else if (count.three === 2) {
|
|
|
|
expect(this).to.eql(ctx2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listener4(e) {
|
|
|
|
count.four++;
|
|
|
|
expect(count.three).to.eql(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.on('test', listener1, ctx1);
|
|
|
|
obj.on('test', listener2, ctx2);
|
|
|
|
obj.on('test', listener2, ctx1); // Same listener but with different context.
|
|
|
|
obj.on('test', listener2); // Same listener but without context.
|
|
|
|
obj.on('test', listener3, ctx1);
|
|
|
|
obj.on('test', listener3, ctx2);
|
|
|
|
obj.on('test', listener4, ctx2);
|
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
|
|
|
expect(count.one).to.be(1);
|
|
|
|
expect(count.two).to.be(3);
|
|
|
|
expect(count.three).to.be(2);
|
|
|
|
expect(count.four).to.be(1);
|
|
|
|
});
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('provides event object to listeners and executes them in the right context', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
obj2 = new L.Evented(),
|
|
|
|
obj3 = new L.Evented(),
|
|
|
|
obj4 = new L.Evented(),
|
|
|
|
foo = {};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
function listener1(e) {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(e.type).to.eql('test');
|
|
|
|
expect(e.target).to.eql(obj);
|
|
|
|
expect(this).to.eql(obj);
|
|
|
|
expect(e.baz).to.eql(1);
|
2012-06-30 07:14:41 +08:00
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
function listener2(e) {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(e.type).to.eql('test');
|
|
|
|
expect(e.target).to.eql(obj2);
|
|
|
|
expect(this).to.eql(foo);
|
|
|
|
expect(e.baz).to.eql(2);
|
2012-06-30 07:14:41 +08:00
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
function listener3(e) {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(e.type).to.eql('test');
|
|
|
|
expect(e.target).to.eql(obj3);
|
|
|
|
expect(this).to.eql(obj3);
|
|
|
|
expect(e.baz).to.eql(3);
|
2012-06-30 07:14:41 +08:00
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
function listener4(e) {
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(e.type).to.eql('test');
|
|
|
|
expect(e.target).to.eql(obj4);
|
|
|
|
expect(this).to.eql(foo);
|
|
|
|
expect(e.baz).to.eql(4);
|
2012-06-30 07:14:41 +08:00
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.addEventListener('test', listener1);
|
|
|
|
obj2.addEventListener('test', listener2, foo);
|
2015-01-29 01:32:27 +08:00
|
|
|
obj3.addEventListener({test: listener3});
|
|
|
|
obj4.addEventListener({test: listener4}, foo);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.fireEvent('test', {baz: 1});
|
|
|
|
obj2.fireEvent('test', {baz: 2});
|
|
|
|
obj3.fireEvent('test', {baz: 3});
|
|
|
|
obj4.fireEvent('test', {baz: 4});
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('calls no listeners removed through #removeEventListener', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
spy3 = sinon.spy(),
|
|
|
|
spy4 = sinon.spy(),
|
|
|
|
spy5 = sinon.spy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.removeEventListener('test', spy);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.fireEvent('test');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy.called).to.be(false);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.addEventListener('test2', spy2);
|
|
|
|
obj.addEventListener('test2', spy3);
|
|
|
|
obj.removeEventListener('test2');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.fireEvent('test2');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy2.called).to.be(false);
|
|
|
|
expect(spy3.called).to.be(false);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.addEventListener('test3', spy4);
|
|
|
|
obj.addEventListener('test4', spy5);
|
|
|
|
obj.removeEventListener({
|
|
|
|
test3: spy4,
|
|
|
|
test4: spy5
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.fireEvent('test3');
|
|
|
|
obj.fireEvent('test4');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy4.called).to.be(false);
|
|
|
|
expect(spy5.called).to.be(false);
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-04-29 17:53:16 +08:00
|
|
|
it('can handle calls to #removeEventListener on objects with no registered event listeners', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented();
|
2013-04-29 19:03:05 +08:00
|
|
|
var removeNonExistentListener = function () {
|
|
|
|
obj.removeEventListener('test');
|
|
|
|
};
|
|
|
|
expect(removeNonExistentListener).to.not.throwException();
|
2013-04-29 17:53:16 +08:00
|
|
|
});
|
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
// added due to context-sensitive removeListener optimization
|
2013-02-20 04:41:48 +08:00
|
|
|
it('fires multiple listeners with the same context with id', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
foo = {};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
L.Util.stamp(foo);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
obj.addEventListener('test', spy1, foo);
|
2013-02-05 00:31:14 +08:00
|
|
|
obj.addEventListener('test', spy2, foo);
|
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy1.called).to.be(true);
|
|
|
|
expect(spy2.called).to.be(true);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('removes listeners with stamped contexts', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
foo = {};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
L.Util.stamp(foo);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
obj.addEventListener('test', spy1, foo);
|
2013-02-05 00:31:14 +08:00
|
|
|
obj.addEventListener('test', spy2, foo);
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
obj.removeEventListener('test', spy1, foo);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy1.called).to.be(false);
|
|
|
|
expect(spy2.called).to.be(true);
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|
2013-03-13 05:08:04 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('removes listeners with a stamp originally added without one', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
foo = {};
|
2013-03-13 05:08:04 +08:00
|
|
|
|
|
|
|
obj.addEventListener('test', spy1, foo);
|
|
|
|
L.Util.stamp(foo);
|
|
|
|
obj.addEventListener('test', spy2, foo);
|
|
|
|
|
|
|
|
obj.removeEventListener('test', spy1, foo);
|
|
|
|
obj.removeEventListener('test', spy2, foo);
|
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
2013-04-04 06:22:45 +08:00
|
|
|
expect(spy1.called).to.be(false);
|
|
|
|
expect(spy2.called).to.be(false);
|
2013-03-13 05:08:04 +08:00
|
|
|
});
|
2013-05-08 17:58:50 +08:00
|
|
|
|
2013-11-04 04:47:55 +08:00
|
|
|
it('removes listeners with context == this and a stamp originally added without one', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
obj2 = new L.Evented(),
|
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
|
|
|
spy3 = sinon.spy();
|
2013-11-04 04:47:55 +08:00
|
|
|
|
|
|
|
obj.addEventListener('test', spy1, obj);
|
|
|
|
L.Util.stamp(obj);
|
|
|
|
obj.addEventListener('test', spy2, obj);
|
2013-11-05 05:29:52 +08:00
|
|
|
obj.addEventListener('test', spy3, obj2); // So that there is a contextId based listener, otherwise removeEventListener will do correct behaviour anyway
|
2013-11-04 04:47:55 +08:00
|
|
|
|
|
|
|
obj.removeEventListener('test', spy1, obj);
|
|
|
|
obj.removeEventListener('test', spy2, obj);
|
2013-11-05 05:29:52 +08:00
|
|
|
obj.removeEventListener('test', spy3, obj2);
|
2013-11-04 04:47:55 +08:00
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
|
|
|
expect(spy1.called).to.be(false);
|
|
|
|
expect(spy2.called).to.be(false);
|
2013-11-05 05:29:52 +08:00
|
|
|
expect(spy3.called).to.be(false);
|
2013-11-04 04:47:55 +08:00
|
|
|
});
|
|
|
|
|
2013-03-15 08:53:03 +08:00
|
|
|
it('doesnt lose track of listeners when removing non existent ones', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2013-04-04 06:22:45 +08:00
|
|
|
spy = sinon.spy(),
|
|
|
|
spy2 = sinon.spy(),
|
2013-03-15 08:53:03 +08:00
|
|
|
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');
|
|
|
|
|
2013-04-04 06:22:45 +08:00
|
|
|
expect(spy.called).to.be(false);
|
2013-03-15 08:53:03 +08:00
|
|
|
});
|
2013-05-08 17:58:50 +08:00
|
|
|
|
2013-11-05 05:21:32 +08:00
|
|
|
it('correctly removes all listeners if given no fn', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2013-11-05 05:21:32 +08:00
|
|
|
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
|
|
|
|
|
2013-12-20 06:09:32 +08:00
|
|
|
expect(obj.listens('test')).to.be(false);
|
2013-11-05 05:21:32 +08:00
|
|
|
|
2015-09-25 20:37:07 +08:00
|
|
|
// Add and remove a listener
|
2013-11-05 05:21:32 +08:00
|
|
|
obj.addEventListener('test', spy, foo2);
|
|
|
|
obj.removeEventListener('test', spy, foo2);
|
2013-11-08 05:54:33 +08:00
|
|
|
|
2013-12-20 06:09:32 +08:00
|
|
|
expect(obj.listens('test')).to.be(false);
|
2016-07-01 22:16:52 +08:00
|
|
|
|
|
|
|
// Add and remove a listener without context
|
2014-06-22 00:26:16 +08:00
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.removeEventListener('test', spy);
|
|
|
|
|
|
|
|
expect(obj.listens('test')).to.be(false);
|
2013-11-05 05:21:32 +08:00
|
|
|
});
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('makes sure an event is not triggered if a listener is removed during dispatch', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2014-06-22 00:26:16 +08:00
|
|
|
spy = sinon.spy(),
|
2016-07-01 22:16:52 +08:00
|
|
|
spy2 = sinon.spy(),
|
|
|
|
spy3 = sinon.spy(),
|
|
|
|
foo = {};
|
2013-11-08 05:54:33 +08:00
|
|
|
|
2014-06-22 00:26:16 +08:00
|
|
|
/* without context */
|
2013-11-08 05:54:33 +08:00
|
|
|
obj.addEventListener('test', function () { obj.removeEventListener('test', spy); });
|
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.fireEvent('test');
|
2013-05-08 17:58:50 +08:00
|
|
|
|
|
|
|
expect(spy.called).to.be(false);
|
2016-07-01 22:16:52 +08:00
|
|
|
|
2014-06-22 00:26:16 +08:00
|
|
|
/* with context */
|
|
|
|
obj.addEventListener('test2', function () { obj.removeEventListener('test2', spy2, foo); }, foo);
|
|
|
|
obj.addEventListener('test2', spy2, foo);
|
|
|
|
obj.fireEvent('test2');
|
2013-05-08 17:58:50 +08:00
|
|
|
});
|
2016-05-12 16:22:36 +08:00
|
|
|
|
|
|
|
it('makes sure an event is not triggered if all listeners are removed during dispatch', function () {
|
|
|
|
var obj = new L.Evented(),
|
|
|
|
spy = sinon.spy();
|
|
|
|
|
|
|
|
obj.addEventListener('test', function () { obj.removeEventListener('test'); });
|
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(false);
|
|
|
|
});
|
2016-08-24 15:22:29 +08:00
|
|
|
|
|
|
|
it('handles reentrant event firing', function () {
|
|
|
|
var obj = new L.Evented(),
|
2016-08-24 15:52:29 +08:00
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy();
|
2016-08-24 15:22:29 +08:00
|
|
|
|
|
|
|
obj
|
|
|
|
.addEventListener('test1', function () {
|
|
|
|
obj.fire('test2');
|
|
|
|
})
|
2016-08-24 15:52:29 +08:00
|
|
|
.addEventListener('test2', spy1)
|
2016-08-24 15:22:29 +08:00
|
|
|
.addEventListener('test1', function () {
|
2016-08-24 15:52:29 +08:00
|
|
|
obj.removeEventListener('test1', spy2);
|
2016-08-24 15:22:29 +08:00
|
|
|
})
|
2016-08-24 15:52:29 +08:00
|
|
|
.addEventListener('test1', spy2);
|
2016-08-24 15:22:29 +08:00
|
|
|
|
|
|
|
obj.fireEvent('test1');
|
2016-08-24 15:52:29 +08:00
|
|
|
expect(spy1.called).to.be(true);
|
|
|
|
expect(spy2.called).to.be(false);
|
2016-08-24 15:22:29 +08:00
|
|
|
});
|
2016-08-25 21:20:51 +08:00
|
|
|
|
|
|
|
it('can remove an event listener while firing', function () {
|
|
|
|
var obj = new L.Evented(),
|
|
|
|
spy = sinon.spy();
|
|
|
|
|
|
|
|
var removeSpy = function () {
|
|
|
|
obj.removeEventListener('test', spy);
|
|
|
|
};
|
|
|
|
|
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.addEventListener('test', removeSpy);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
obj.removeEventListener('test', removeSpy);
|
|
|
|
|
2016-08-25 23:23:58 +08:00
|
|
|
expect(obj.listens('test')).to.be(false);
|
2016-08-25 21:20:51 +08:00
|
|
|
});
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
describe('#on, #off & #fire', function () {
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('works like #addEventListener && #removeEventListener', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.on('test', spy);
|
2010-09-10 21:54:20 +08:00
|
|
|
obj.fire('test');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy.called).to.be(true);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.off('test', spy);
|
|
|
|
obj.fireEvent('test');
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy.callCount).to.be.lessThan(2);
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('does not override existing methods with the same name', function () {
|
2013-03-02 05:49:20 +08:00
|
|
|
var spy1 = sinon.spy(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy2 = sinon.spy(),
|
|
|
|
spy3 = sinon.spy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-12-18 03:07:21 +08:00
|
|
|
var Klass = L.Evented.extend({
|
2010-09-04 00:16:29 +08:00
|
|
|
on: spy1,
|
2010-09-10 21:54:20 +08:00
|
|
|
off: spy2,
|
|
|
|
fire: spy3
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new Klass();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.on();
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy1.called).to.be(true);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.off();
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy2.called).to.be(true);
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-10 21:54:20 +08:00
|
|
|
obj.fire();
|
2013-03-02 05:49:20 +08:00
|
|
|
expect(spy3.called).to.be(true);
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2016-07-01 22:16:52 +08:00
|
|
|
|
|
|
|
it('does not add twice the same function', function () {
|
|
|
|
var obj = new L.Evented(),
|
|
|
|
spy = sinon.spy();
|
|
|
|
|
|
|
|
/* register without context */
|
|
|
|
obj.on("test", spy);
|
|
|
|
obj.on("test", spy);
|
|
|
|
|
|
|
|
/* Should be called once */
|
|
|
|
obj.fire("test");
|
|
|
|
|
|
|
|
expect(spy.callCount).to.eql(1);
|
|
|
|
});
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-04-19 22:23:45 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
describe("#clearEventListeners", function () {
|
|
|
|
it("clears all registered listeners on an object", function () {
|
2013-04-15 12:20:10 +08:00
|
|
|
var spy = sinon.spy(),
|
2015-09-25 18:55:37 +08:00
|
|
|
obj = new L.Evented(),
|
|
|
|
otherObj = new L.Evented();
|
2013-04-15 12:20:10 +08:00
|
|
|
|
|
|
|
obj.on('test', spy, obj);
|
|
|
|
obj.on('testTwo', spy);
|
2013-04-16 01:57:35 +08:00
|
|
|
obj.on('test', spy, otherObj);
|
|
|
|
obj.off();
|
2013-04-15 12:20:10 +08:00
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(false);
|
|
|
|
});
|
|
|
|
});
|
2013-04-19 22:23:45 +08:00
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
describe('#once', function () {
|
|
|
|
it('removes event listeners after first trigger', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy();
|
2013-04-19 22:23:45 +08:00
|
|
|
|
|
|
|
obj.once('test', spy, obj);
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(true);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.callCount).to.be.lessThan(2);
|
|
|
|
});
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('works with an object hash', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy(),
|
|
|
|
otherSpy = sinon.spy();
|
2013-04-19 22:23:45 +08:00
|
|
|
|
|
|
|
obj.once({
|
|
|
|
'test': spy,
|
|
|
|
otherTest: otherSpy
|
|
|
|
}, obj);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
obj.fire('otherTest');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(true);
|
|
|
|
expect(otherSpy.called).to.be(true);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
obj.fire('otherTest');
|
|
|
|
|
|
|
|
expect(spy.callCount).to.be.lessThan(2);
|
|
|
|
expect(otherSpy.callCount).to.be.lessThan(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't call listeners to events that have been removed", function () {
|
2015-03-02 04:51:04 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy();
|
2013-04-19 22:23:45 +08:00
|
|
|
|
2015-03-02 04:51:04 +08:00
|
|
|
obj.once('test', spy, obj);
|
|
|
|
obj.off('test', spy, obj);
|
2013-04-19 22:23:45 +08:00
|
|
|
|
2015-03-02 04:51:04 +08:00
|
|
|
obj.fire('test');
|
2013-04-19 22:23:45 +08:00
|
|
|
|
2015-03-02 04:51:04 +08:00
|
|
|
expect(spy.called).to.be(false);
|
2013-04-19 22:23:45 +08:00
|
|
|
});
|
|
|
|
|
2013-11-08 05:54:33 +08:00
|
|
|
it('works if called from a context that doesnt implement #Events', function () {
|
2013-12-18 03:07:21 +08:00
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
spy = sinon.spy(),
|
|
|
|
foo = {};
|
2013-04-19 22:23:45 +08:00
|
|
|
|
|
|
|
obj.once('test', spy, foo);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(true);
|
|
|
|
});
|
|
|
|
});
|
2013-12-19 12:23:22 +08:00
|
|
|
|
|
|
|
describe('addEventParent && removeEventParent', function () {
|
|
|
|
it('makes the object propagate events with to the given one if fired with propagate=true', function () {
|
|
|
|
var obj = new L.Evented(),
|
2015-09-25 18:55:37 +08:00
|
|
|
parent1 = new L.Evented(),
|
|
|
|
parent2 = new L.Evented(),
|
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy();
|
2013-12-19 12:23:22 +08:00
|
|
|
|
|
|
|
parent1.on('test', spy1);
|
|
|
|
parent2.on('test', spy2);
|
|
|
|
|
|
|
|
obj.addEventParent(parent1).addEventParent(parent2);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy1.called).to.be(false);
|
|
|
|
expect(spy2.called).to.be(false);
|
|
|
|
|
|
|
|
obj.fire('test', null, true);
|
|
|
|
|
|
|
|
expect(spy1.called).to.be(true);
|
|
|
|
expect(spy2.called).to.be(true);
|
|
|
|
|
|
|
|
obj.removeEventParent(parent1);
|
|
|
|
|
|
|
|
obj.fire('test', null, true);
|
|
|
|
|
|
|
|
expect(spy1.callCount).to.be(1);
|
|
|
|
expect(spy2.callCount).to.be(2);
|
|
|
|
});
|
2016-07-01 22:16:52 +08:00
|
|
|
|
|
|
|
it('can fire event where child has no listeners', function () {
|
|
|
|
var obj = new L.Evented(),
|
|
|
|
parent = new L.Evented(),
|
|
|
|
spy1 = sinon.spy(),
|
|
|
|
spy2 = sinon.spy();
|
|
|
|
|
|
|
|
/* register without context */
|
|
|
|
obj.on('test', spy1);
|
|
|
|
parent.on('test2', spy2);
|
|
|
|
|
|
|
|
obj.addEventParent(parent);
|
|
|
|
|
|
|
|
/* Should be called once */
|
|
|
|
obj.fire('test2', null, true);
|
|
|
|
|
|
|
|
expect(spy1.callCount).to.eql(0);
|
|
|
|
expect(spy2.callCount).to.eql(1);
|
|
|
|
});
|
2013-12-19 12:23:22 +08:00
|
|
|
});
|
2016-07-01 17:14:22 +08:00
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-07 16:53:24 +08:00
|
|
|
describe('#L.Mixin.Events', function () {
|
|
|
|
it('can be used from includes', function () {
|
|
|
|
var EventClass = L.Class.extend({
|
|
|
|
includes: L.Mixin.Events
|
|
|
|
});
|
|
|
|
var obj = new EventClass();
|
|
|
|
var spy = sinon.spy();
|
|
|
|
|
|
|
|
obj.on('test', spy);
|
|
|
|
|
|
|
|
obj.fire('test');
|
|
|
|
|
|
|
|
expect(spy.called).to.be(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|