1 describe('Events', function() {
4 beforeEach(function() {
5 Klass
= L
.Class
.extend({
6 includes
: L
.Mixin
.Events
10 describe('#fireEvent', function() {
12 it('should fire all listeners added through #addEventListener', function() {
13 var obj
= new Klass(),
14 spy
= jasmine
.createSpy(),
15 spy2
= jasmine
.createSpy(),
16 spy3
= jasmine
.createSpy();
18 obj
.addEventListener('test', spy
);
19 obj
.addEventListener('test', spy2
);
20 obj
.addEventListener('other', spy3
);
22 expect(spy
).not
.toHaveBeenCalled();
23 expect(spy2
).not
.toHaveBeenCalled();
24 expect(spy3
).not
.toHaveBeenCalled();
26 obj
.fireEvent('test');
28 expect(spy
).toHaveBeenCalled();
29 expect(spy2
).toHaveBeenCalled();
30 expect(spy3
).not
.toHaveBeenCalled();
33 it('should provide event object to listeners and execute them in the right context', function() {
34 var obj
= new Klass(),
38 function listener1(e
) {
39 expect(e
.type
).toEqual('test');
40 expect(e
.target
).toEqual(obj
);
41 expect(this).toEqual(obj
);
42 expect(e
.bar
).toEqual(3);
45 function listener2(e
) {
46 expect(e
.target
).toEqual(obj2
);
47 expect(this).toEqual(foo
);
50 obj
.addEventListener('test', listener1
);
51 obj2
.addEventListener('test', listener2
, foo
);
53 obj
.fireEvent('test', {bar
: 3});
56 it('should not call listeners removed through #removeEventListener', function() {
57 var obj
= new Klass(),
58 spy
= jasmine
.createSpy();
60 obj
.addEventListener('test', spy
);
61 obj
.removeEventListener('test', spy
);
63 obj
.fireEvent('test');
65 expect(spy
).not
.toHaveBeenCalled();
69 describe('#on, #off & #fire', function() {
71 it('should work like #addEventListener && #removeEventListener', function() {
72 var obj
= new Klass(),
73 spy
= jasmine
.createSpy();
78 expect(spy
).toHaveBeenCalled();
81 obj
.fireEvent('test');
83 expect(spy
.callCount
).toBeLessThan(2);
86 it('should not override existing methods with the same name', function() {
87 var spy1
= jasmine
.createSpy(),
88 spy2
= jasmine
.createSpy(),
89 spy3
= jasmine
.createSpy();
91 var Klass2
= L
.Class
.extend({
92 includes
: L
.Mixin
.Events
,
98 var obj
= new Klass2();
101 expect(spy1
).toHaveBeenCalled();
104 expect(spy2
).toHaveBeenCalled();
107 expect(spy3
).toHaveBeenCalled();