Tests/Specs: Use Chai for assertions, instead of Jasmine's assertions.
[mootools.git] / Specs / Element / Element.Delegation.js
blobbd8300fac5b8f8b810b1fbce495f133775e2a648
1 /*
2 ---
3 name: Element.Delegation
4 requires: ~
5 provides: ~
6 ...
7 */
9 describe('Element.Delegation', function(){
11         describe('fireEvent', function(){
13                 it('should fire the added `click:relay(a)` function with fireEvent', function(){
15                         var a = new Element('a[text=Hello World]'), result, self;
16                         var div = new Element('div').inject(document.body).adopt(a).addEvent('click:relay(a)', function(){
17                                 result = arguments[1];
18                                 self = this;
19                         }).fireEvent('click:relay(a)', [null, a]);
21                         expect(result).to.equal(a);
22                         expect(self).to.equal(div);
24                         div.destroy();
26                 });
28                 it('Should fire click events through fireEvent and delegate when a target is passed as argument', function(){
30                         var a = new Element('a[text="Hello World"]'), result, self;
31                         var div = new Element('div').inject(document.body).adopt(a).addEvent('click:relay(a)', function(){
32                                 result = arguments[1];
33                                 self = this;
34                         }).fireEvent('click', [null, a]);
36                         expect(result).to.equal(a);
37                         expect(self).to.equal(a);
39                         div.destroy();
41                 });
43                 it('Should not fire click events through fireEvent when added as delegated events without an target', function(){
45                         var spy = sinon.spy();
46                         var a = new Element('a[text="Hello World"]');
47                         var div = new Element('div').inject(document.body).adopt(a).addEvent('click:relay(a)', spy).fireEvent('click');
49                         expect(spy.called).to.equal(false);
51                         div.destroy();
53                 });
55         });
57         describe('removeEvent', function(){
59                 describe('submit', function(){
61                         it('should remove nicely', function(){
62                                 var element = new Element('div', {
63                                         html: '<div><form><input type="text"></form></div>'
64                                 });
66                                 var input = element.getElement('input');
67                                 var listener = function(){};
69                                 element.addEvent('submit:relay(form)', listener);
71                                 // IE8, fireEvent on the observer element. This adds the
72                                 // submit event to the <form> element.
73                                 element.fireEvent('focusin', [{target: input}, input]);
75                                 // remove element, which also removes the form
76                                 element.getElement('div').destroy();
78                                 // now removing the event, should remove the submit event from the
79                                 // form, but it's not there anymore, so it may not throw an error.
80                                 element.removeEvent('submit:relay(form)', listener);
82                         });
84                 });
86         });
88 });