2010-09-04 00:16:29 +08:00
|
|
|
describe('Events', function() {
|
|
|
|
var Klass;
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
Klass = L.Class.extend({
|
|
|
|
includes: L.Mixin.Events
|
|
|
|
});
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
describe('#fireEvent', function() {
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('fires all listeners added through #addEventListener', function() {
|
2010-09-04 00:16:29 +08:00
|
|
|
var obj = new Klass(),
|
|
|
|
spy = jasmine.createSpy(),
|
|
|
|
spy2 = jasmine.createSpy(),
|
2012-06-30 07:14:41 +08:00
|
|
|
spy3 = jasmine.createSpy(),
|
|
|
|
spy4 = jasmine.createSpy(),
|
|
|
|
spy5 = jasmine.createSpy();
|
|
|
|
spy6 = jasmine.createSpy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.addEventListener('test', spy);
|
|
|
|
obj.addEventListener('test', spy2);
|
|
|
|
obj.addEventListener('other', spy3);
|
2012-06-30 07:14:41 +08:00
|
|
|
obj.addEventListener({ test: spy4, other: spy5 });
|
2013-02-20 00:18:15 +08:00
|
|
|
obj.addEventListener({'test other': spy6 });
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
expect(spy2).not.toHaveBeenCalled();
|
|
|
|
expect(spy3).not.toHaveBeenCalled();
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(spy4).not.toHaveBeenCalled();
|
|
|
|
expect(spy5).not.toHaveBeenCalled();
|
|
|
|
expect(spy6).not.toHaveBeenCalled();
|
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
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
expect(spy2).toHaveBeenCalled();
|
|
|
|
expect(spy3).not.toHaveBeenCalled();
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(spy4).toHaveBeenCalled();
|
|
|
|
expect(spy5).not.toHaveBeenCalled();
|
|
|
|
expect(spy6).toHaveBeenCalled();
|
|
|
|
expect(spy6.calls.length).toEqual(1);
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('provides event object to listeners and executes them in the right context', function() {
|
2010-09-04 00:16:29 +08:00
|
|
|
var obj = new Klass(),
|
|
|
|
obj2 = new Klass(),
|
2012-06-30 07:14:41 +08:00
|
|
|
obj3 = new Klass(),
|
|
|
|
obj4 = new Klass(),
|
2010-09-04 00:16:29 +08:00
|
|
|
foo = {};
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
function listener1(e) {
|
|
|
|
expect(e.type).toEqual('test');
|
|
|
|
expect(e.target).toEqual(obj);
|
|
|
|
expect(this).toEqual(obj);
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(e.baz).toEqual(1);
|
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
function listener2(e) {
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(e.type).toEqual('test');
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(e.target).toEqual(obj2);
|
|
|
|
expect(this).toEqual(foo);
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(e.baz).toEqual(2);
|
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
function listener3(e) {
|
|
|
|
expect(e.type).toEqual('test');
|
|
|
|
expect(e.target).toEqual(obj3);
|
|
|
|
expect(this).toEqual(obj3);
|
|
|
|
expect(e.baz).toEqual(3);
|
|
|
|
}
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
function listener4(e) {
|
|
|
|
expect(e.type).toEqual('test');
|
|
|
|
expect(e.target).toEqual(obj4);
|
|
|
|
expect(this).toEqual(foo);
|
|
|
|
expect(e.baz).toEqual(4);
|
|
|
|
}
|
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);
|
2012-06-30 07:14:41 +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-02-20 04:41:48 +08:00
|
|
|
it('calls no listeners removed through #removeEventListener', function() {
|
2010-09-04 00:16:29 +08:00
|
|
|
var obj = new Klass(),
|
2012-06-30 07:14:41 +08:00
|
|
|
spy = jasmine.createSpy(),
|
|
|
|
spy2 = jasmine.createSpy(),
|
|
|
|
spy3 = jasmine.createSpy(),
|
|
|
|
spy4 = jasmine.createSpy(),
|
|
|
|
spy5 = jasmine.createSpy();
|
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
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(spy).not.toHaveBeenCalled();
|
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
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(spy2).not.toHaveBeenCalled();
|
|
|
|
expect(spy3).not.toHaveBeenCalled();
|
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
|
|
|
|
2012-06-30 07:14:41 +08:00
|
|
|
expect(spy4).not.toHaveBeenCalled();
|
|
|
|
expect(spy5).not.toHaveBeenCalled();
|
2010-09-04 00:16:29 +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-02-05 00:31:14 +08:00
|
|
|
var obj = new Klass(),
|
|
|
|
spy = jasmine.createSpy(),
|
|
|
|
spy2 = jasmine.createSpy(),
|
|
|
|
foo = {};
|
|
|
|
|
|
|
|
L.Util.stamp(foo);
|
|
|
|
|
|
|
|
obj.addEventListener('test', spy, foo);
|
|
|
|
obj.addEventListener('test', spy2, foo);
|
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalled();
|
|
|
|
expect(spy2).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('removes listeners with stamped contexts', function () {
|
2013-02-05 00:31:14 +08:00
|
|
|
var obj = new Klass(),
|
|
|
|
spy = jasmine.createSpy(),
|
|
|
|
spy2 = jasmine.createSpy(),
|
|
|
|
foo = {};
|
|
|
|
|
|
|
|
L.Util.stamp(foo);
|
|
|
|
|
|
|
|
obj.addEventListener('test', spy, foo);
|
|
|
|
obj.addEventListener('test', spy2, foo);
|
|
|
|
|
|
|
|
obj.removeEventListener('test', spy, foo);
|
|
|
|
|
|
|
|
obj.fireEvent('test');
|
|
|
|
|
|
|
|
expect(spy).not.toHaveBeenCalled();
|
|
|
|
expect(spy2).toHaveBeenCalled();
|
|
|
|
});
|
2013-03-13 05:08:04 +08:00
|
|
|
|
|
|
|
it('removes listeners with a stamp originally added without one', function() {
|
|
|
|
var obj = new Klass(),
|
|
|
|
spy1 = jasmine.createSpy('unstamped'),
|
|
|
|
spy2 = jasmine.createSpy('stamped'),
|
|
|
|
foo = {};
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
|
|
|
expect(spy1).not.toHaveBeenCalled();
|
|
|
|
expect(spy2).not.toHaveBeenCalled();
|
|
|
|
});
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-10 21:54:20 +08:00
|
|
|
describe('#on, #off & #fire', function() {
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('works like #addEventListener && #removeEventListener', function() {
|
2010-09-04 00:16:29 +08:00
|
|
|
var obj = new Klass(),
|
|
|
|
spy = jasmine.createSpy();
|
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
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(spy).toHaveBeenCalled();
|
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
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
expect(spy.callCount).toBeLessThan(2);
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2013-02-20 04:41:48 +08:00
|
|
|
it('does not override existing methods with the same name', function() {
|
2010-09-04 00:16:29 +08:00
|
|
|
var spy1 = jasmine.createSpy(),
|
2010-09-10 21:54:20 +08:00
|
|
|
spy2 = jasmine.createSpy(),
|
|
|
|
spy3 = jasmine.createSpy();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
var Klass2 = L.Class.extend({
|
|
|
|
includes: L.Mixin.Events,
|
|
|
|
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
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
var obj = new Klass2();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.on();
|
|
|
|
expect(spy1).toHaveBeenCalled();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-04 00:16:29 +08:00
|
|
|
obj.off();
|
|
|
|
expect(spy2).toHaveBeenCalled();
|
2013-02-05 00:31:14 +08:00
|
|
|
|
2010-09-10 21:54:20 +08:00
|
|
|
obj.fire();
|
|
|
|
expect(spy3).toHaveBeenCalled();
|
2010-09-04 00:16:29 +08:00
|
|
|
});
|
|
|
|
});
|
2013-02-05 00:31:14 +08:00
|
|
|
});
|