3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.EventManager", function() {
20 beforeEach(function() {
21 // add global variable in whitelist
24 element = document.body;
26 elementWithId = document.createElement("DIV");
27 elementWithId.id = 'element-with-id';
28 element.appendChild(elementWithId);
30 elementWithoutId = document.createElement("DIV");
31 element.appendChild(elementWithoutId);
34 afterEach(function() {
35 var el = Ext.get(elementWithId);
39 el = Ext.get(elementWithoutId);
45 describe("event binding", function() {
46 describe("getId", function() {
47 describe("if element has an id", function() {
48 it("should return element id", function() {
49 expect(elementWithId.id).toBe(Ext.EventManager.getId(elementWithId));
53 describe("if element hasn't an id", function() {
55 beforeEach(function() {
57 spyOn(Ext, "id").andCallFake(function(dom) {
60 result = Ext.EventManager.getId(elementWithoutId);
64 it("should add an id to element with Ext.id", function() {
65 expect(elementWithoutId.id).toBe(id);
68 it("should return the element id", function() {
69 expect(result).toBe(id);
72 it("should add element to Ext.cache", function() {
73 expect(Ext.cache[id].el.dom).toBe(elementWithoutId);
77 describe("document and window", function() {
78 describe("document", function() {
80 beforeEach(function() {
81 result = Ext.EventManager.getId(document);
84 afterEach(function() {
85 delete Ext.cache[Ext.documentId];
87 it("should add document Ext.Element to cache", function() {
88 expect(Ext.cache[Ext.documentId].el.dom).toBe(document);
91 it("should enable skipGarbageCollection flag", function() {
92 expect(Ext.cache[Ext.documentId].skipGarbageCollection).toBe(true);
95 it("should return Ext.documentId", function() {
96 expect(result).toBe(Ext.documentId);
100 describe("window", function() {
102 beforeEach(function() {
103 result = Ext.EventManager.getId(window);
106 afterEach(function() {
107 delete Ext.cache[Ext.windowId];
109 it("should add window Ext.Element to cache", function() {
110 expect(Ext.cache[Ext.windowId].el.dom).toBe(window);
113 it("should enable skipGarbageCollection flag", function() {
114 expect(Ext.cache[Ext.windowId].skipGarbageCollection).toBe(true);
117 it("should return Ext.windowId", function() {
118 expect(result).toBe(Ext.windowId);
124 describe("prepareListenerConfig", function() {
125 var config, configWithFn;
127 beforeEach(function() {
139 spyOn(Ext.EventManager, "removeListener");
140 spyOn(Ext.EventManager, "addListener");
143 describe("if remove", function() {
144 describe("with an object like click: function(){}, scope: this", function() {
145 it("should call removeListener", function() {
146 Ext.EventManager.prepareListenerConfig(element, config, true);
147 expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
151 describe("with an object like click: {fn: function(){}, scope: this}", function() {
152 it("should call removeListener", function() {
153 Ext.EventManager.prepareListenerConfig(element, configWithFn, true);
154 expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
159 describe("if add", function() {
160 describe("with an object like click: function(){}, scope: this", function() {
161 it("should call addListener", function() {
162 Ext.EventManager.prepareListenerConfig(element, config);
163 expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
167 describe("with an object like click: {fn: function(){}, scope: this}", function() {
168 it("should call addListener", function() {
169 Ext.EventManager.prepareListenerConfig(element, configWithFn);
170 expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
176 describe("addListener", function() {
177 describe("if eventName is an object", function() {
180 beforeEach(function() {
184 it("should call prepareListenerConfig", function() {
185 spyOn(Ext.EventManager, "prepareListenerConfig");
186 Ext.EventManager.addListener(element, eventName);
187 expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName);
190 it("should throw an error if the element doesn't exist", function() {
192 Ext.EventManager.addListener(undefined, "click");
193 }).toRaiseExtError();
197 it("should throw an error if the element doesn't exist", function() {
199 Ext.EventManager.addListener(undefined, "click");
200 }).toRaiseExtError();
203 describe("event firing", function() {
206 beforeEach(function() {
209 fn: jasmine.createSpy(),
214 Ext.EventManager.addListener(element, config);
217 afterEach(function() {
218 Ext.EventManager.removeListener(element, config);
221 it("should call the listener", function() {
222 jasmine.fireMouseEvent(element, "click", 1, 1);
224 expect(config.click.fn).toHaveBeenCalled();
230 describe("options", function() {
233 afterEach(function() {
234 Ext.EventManager.removeListener(element, config);
237 describe("scope", function() {
238 it("should call the listener with the correct scope", function() {
241 fn: jasmine.createSpy(),
246 Ext.EventManager.addListener(element, config);
248 jasmine.fireMouseEvent(element, "click", 1, 1);
249 expect(config.click.fn.calls[0].object).toBe(fakeScope);
253 describe("delegate", function() {
255 beforeEach(function() {
258 fn: jasmine.createSpy(),
259 delegate: ".something",
264 child = Ext.get(element).createChild({
269 afterEach(function() {
270 var elChild = Ext.get(child);
276 describe("if filter found nothing", function() {
277 it("should not call the listener", function() {
278 Ext.EventManager.addListener(element, config);
279 jasmine.fireMouseEvent(child, "click", 1, 1);
281 expect(config.click.fn).not.toHaveBeenCalled();
285 describe("if filter found something", function() {
286 it("should call the listener", function() {
287 Ext.get(child).addCls("something");
288 Ext.EventManager.addListener(element, config);
289 jasmine.fireMouseEvent(child, "click", 1, 1);
291 expect(config.click.fn).toHaveBeenCalled();
295 describe("stopevent", function() {
298 beforeEach(function() {
301 fn: jasmine.createSpy(),
307 checkbox = Ext.get(element).createChild({
312 Ext.EventManager.addListener(element, config);
313 Ext.EventManager.addListener(checkbox, config);
314 if (jasmine.browser.isIE) {
317 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
321 afterEach(function() {
322 var checkBoxEl = Ext.get(checkbox);
326 Ext.EventManager.removeListener(checkbox, config);
329 it("should stop propagation to parent elements", function() {
330 expect(config.click.fn.calls.length).toEqual(1);
333 it("should prevent default browser handling of the event", function() {
334 expect(checkbox.checked).toBe(false);
338 describe("preventDefault", function() {
341 beforeEach(function() {
344 fn: jasmine.createSpy(),
345 preventDefault: true,
350 checkbox = Ext.get(element).createChild({
355 Ext.EventManager.addListener(element, config);
356 Ext.EventManager.addListener(checkbox, config);
357 if (jasmine.browser.isIE) {
360 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
364 afterEach(function() {
365 var checkBoxEl = Ext.get(checkbox);
369 Ext.EventManager.removeListener(checkbox, config);
372 it("should prevent default browser handling of the event", function() {
373 expect(checkbox.checked).toBe(false);
376 it("should not stop the propagation of the event", function() {
377 expect(config.click.fn.calls.length).toEqual(2);
381 describe("stopPropagation", function() {
384 beforeEach(function() {
387 fn: jasmine.createSpy(),
388 stopPropagation: true,
393 checkbox = Ext.getBody().createChild({
398 Ext.EventManager.addListener(element, config);
399 Ext.EventManager.addListener(checkbox, config);
400 if (jasmine.browser.isIE) {
403 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
408 afterEach(function() {
409 var checkBoxEl = Ext.get(checkbox);
413 Ext.EventManager.removeListener(checkbox, config);
416 it("should stop propagation to parent elements", function() {
417 expect(config.click.fn.calls.length).toEqual(1);
420 it("should not prevent default browser handling of the event", function() {
421 expect(checkbox.checked).toBe(true);
425 describe("normalized", function() {
427 beforeEach(function() {
430 fn: jasmine.createSpy(),
436 Ext.EventManager.addListener(element, config);
438 event = jasmine.fireMouseEvent(element, "click", 1, 1);
441 it("should pass a browser event instead of an Ext.EventObject", function() {
442 expect(config.click.fn).toHaveBeenCalledWith(event, element, config.click);
446 describe("delay", function() {
447 beforeEach(function() {
450 fn: jasmine.createSpy(),
456 Ext.EventManager.addListener(element, config);
459 it("should not call listener before 1 ms", function() {
460 jasmine.fireMouseEvent(element, "click", 1, 1);
461 expect(config.click.fn).not.toHaveBeenCalled();
464 it("should call listener after 1 ms", function() {
466 jasmine.fireMouseEvent(element, "click", 1, 1);
472 expect(config.click.fn).toHaveBeenCalled();
477 describe("single", function() {
478 beforeEach(function() {
481 fn: jasmine.createSpy(),
487 Ext.EventManager.addListener(element, config);
488 jasmine.fireMouseEvent(element, "click", 1, 1);
489 jasmine.fireMouseEvent(element, "click", 1, 1);
492 it("should call listener only one time", function() {
493 expect(config.click.fn.calls.length).toEqual(1);
497 describe("buffer", function() {
498 beforeEach(function() {
501 fn: jasmine.createSpy(),
507 Ext.EventManager.addListener(element, config);
508 jasmine.fireMouseEvent(element, "click", 1, 1);
509 jasmine.fireMouseEvent(element, "click", 1, 1);
512 it("should call listener only one time", function() {
515 expect(config.click.fn.calls.length).toEqual(1);
520 describe("target", function() {
522 beforeEach(function() {
523 child = Ext.get(element).createChild({
528 fn: jasmine.createSpy(),
534 Ext.EventManager.addListener(element, config);
537 afterEach(function() {
538 var childEl = Ext.get(child);
544 it("should call listener if element event is fired", function() {
545 jasmine.fireMouseEvent(element, "click", 1, 1);
546 expect(config.click.fn).toHaveBeenCalled();
549 it("should not call listener if child event is fired (no bubbling)", function() {
550 jasmine.fireMouseEvent(child, "click", 1, 1);
551 expect(config.click.fn).not.toHaveBeenCalled();
558 describe("removeListener", function() {
559 describe("if eventName is an object", function() {
560 it("should call prepareListenerConfig", function() {
562 spyOn(Ext.EventManager, "prepareListenerConfig");
563 Ext.EventManager.removeListener(element, eventName);
564 expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName, true);
568 describe("event firing", function() {
571 beforeEach(function() {
574 fn: jasmine.createSpy(),
579 Ext.EventManager.addListener(element, config);
580 Ext.EventManager.removeListener(element, config);
583 it("should not call the listener", function() {
584 jasmine.fireMouseEvent(element, "click", 1, 1);
586 expect(config.click.fn).not.toHaveBeenCalled();
591 describe("removeAll", function() {
594 beforeEach(function() {
597 fn: jasmine.createSpy(),
601 fn: jasmine.createSpy(),
606 Ext.EventManager.addListener(element, config);
607 Ext.EventManager.removeAll(element, config);
610 it("should should not call click listener", function() {
611 jasmine.fireMouseEvent(element, "click", 1, 1);
613 expect(config.click.fn).not.toHaveBeenCalled();
616 it("should not call mouseover listener", function() {
617 jasmine.fireMouseEvent(element, "mouseover", 1, 1);
619 expect(config.mouseover.fn).not.toHaveBeenCalled();
623 describe("purgeElement", function() {
626 beforeEach(function() {
627 child = Ext.get(element).createChild({
633 fn: jasmine.createSpy(),
638 Ext.EventManager.addListener(element, config);
639 Ext.EventManager.addListener(child, config);
640 Ext.EventManager.purgeElement(element);
643 afterEach(function() {
644 var childEl = Ext.get(child);
650 it("should remove all listeners from element", function() {
651 jasmine.fireMouseEvent(element, "mouseover", 1, 1);
653 expect(config.mouseover.fn).not.toHaveBeenCalled();
656 it("should remove all listeners from element children", function() {
657 jasmine.fireMouseEvent(child, "mouseover", 1, 1);
659 expect(config.mouseover.fn).not.toHaveBeenCalled();
663 describe("methods alias", function() {
664 it("should alias addListener with on", function() {
665 expect(Ext.EventManager.on).toBe(Ext.EventManager.addListener);
668 it("should alias removeListener with un", function() {
669 expect(Ext.EventManager.un).toBe(Ext.EventManager.removeListener);