3 name: Element.Delegation
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];
19 }).fireEvent('click:relay(a)', [null, a]);
21 expect(result).toEqual(a);
22 expect(self).toEqual(div);
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];
34 }).fireEvent('click', [null, a]);
36 expect(result).toEqual(a);
37 expect(self).toEqual(a);
43 it('Should not fire click events through fireEvent when added as delegated events without an target', function(){
45 var spy = jasmine.createSpy('click');
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).not.toHaveBeenCalled();
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>'
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);