1 describe("Ext.EventManager", function() {
6 beforeEach(function() {
7 // add global variable in whitelist
9 addGlobal("ExtSandbox1");
10 element = document.body;
12 elementWithId = document.createElement("DIV");
13 elementWithId.id = 'element-with-id';
14 element.appendChild(elementWithId);
16 elementWithoutId = document.createElement("DIV");
17 element.appendChild(elementWithoutId);
20 afterEach(function() {
21 var el = Ext.get(elementWithId);
25 el = Ext.get(elementWithoutId);
31 describe("event binding", function() {
32 describe("getId", function() {
33 describe("if element has an id", function() {
34 it("should return element id", function() {
35 expect(elementWithId.id).toBe(Ext.EventManager.getId(elementWithId));
39 describe("if element hasn't an id", function() {
41 beforeEach(function() {
43 spyOn(Ext, "id").andCallFake(function(dom) {
46 result = Ext.EventManager.getId(elementWithoutId);
50 it("should add an id to element with Ext.id", function() {
51 expect(elementWithoutId.id).toBe(id);
54 it("should return the element id", function() {
55 expect(result).toBe(id);
58 it("should add element to Ext.cache", function() {
59 expect(Ext.cache[id].el.dom).toBe(elementWithoutId);
63 describe("document and window", function() {
64 describe("document", function() {
66 beforeEach(function() {
67 result = Ext.EventManager.getId(document);
70 afterEach(function() {
71 delete Ext.cache[Ext.documentId];
73 it("should add document Ext.core.Element to cache", function() {
74 expect(Ext.cache[Ext.documentId].el.dom).toBe(document);
77 it("should enable skipGarbageCollection flag", function() {
78 expect(Ext.cache[Ext.documentId].skipGarbageCollection).toBe(true);
81 it("should return Ext.documentId", function() {
82 expect(result).toBe(Ext.documentId);
86 describe("window", function() {
88 beforeEach(function() {
89 result = Ext.EventManager.getId(window);
92 afterEach(function() {
93 delete Ext.cache[Ext.windowId];
95 it("should add window Ext.core.Element to cache", function() {
96 expect(Ext.cache[Ext.windowId].el.dom).toBe(window);
99 it("should enable skipGarbageCollection flag", function() {
100 expect(Ext.cache[Ext.windowId].skipGarbageCollection).toBe(true);
103 it("should return Ext.windowId", function() {
104 expect(result).toBe(Ext.windowId);
110 describe("prepareListenerConfig", function() {
111 var config, configWithFn;
113 beforeEach(function() {
125 spyOn(Ext.EventManager, "removeListener");
126 spyOn(Ext.EventManager, "addListener");
129 describe("if remove", function() {
130 describe("with an object like click: function(){}, scope: this", function() {
131 it("should call removeListener", function() {
132 Ext.EventManager.prepareListenerConfig(element, config, true);
133 expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
137 describe("with an object like click: {fn: function(){}, scope: this}", function() {
138 it("should call removeListener", function() {
139 Ext.EventManager.prepareListenerConfig(element, configWithFn, true);
140 expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
145 describe("if add", function() {
146 describe("with an object like click: function(){}, scope: this", function() {
147 it("should call addListener", function() {
148 Ext.EventManager.prepareListenerConfig(element, config);
149 expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
153 describe("with an object like click: {fn: function(){}, scope: this}", function() {
154 it("should call addListener", function() {
155 Ext.EventManager.prepareListenerConfig(element, configWithFn);
156 expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
162 describe("addListener", function() {
163 describe("if eventName is an object", function() {
166 beforeEach(function() {
170 it("should call prepareListenerConfig", function() {
171 spyOn(Ext.EventManager, "prepareListenerConfig");
172 Ext.EventManager.addListener(element, eventName);
173 expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName);
176 it("should throw an error if the element doesn't exist", function() {
178 Ext.EventManager.addListener(undefined, "click");
179 }).toRaiseExtError();
183 it("should throw an error if the element doesn't exist", function() {
185 Ext.EventManager.addListener(undefined, "click");
186 }).toRaiseExtError();
189 describe("event firing", function() {
192 beforeEach(function() {
195 fn: jasmine.createSpy(),
200 Ext.EventManager.addListener(element, config);
203 afterEach(function() {
204 Ext.EventManager.removeListener(element, config);
207 it("should call the listener", function() {
208 jasmine.fireMouseEvent(element, "click", 1, 1);
210 expect(config.click.fn).toHaveBeenCalled();
216 describe("options", function() {
219 afterEach(function() {
220 Ext.EventManager.removeListener(element, config);
223 describe("scope", function() {
224 it("should call the listener with the correct scope", function() {
227 fn: jasmine.createSpy(),
232 Ext.EventManager.addListener(element, config);
234 jasmine.fireMouseEvent(element, "click", 1, 1);
235 expect(config.click.fn.calls[0].object).toBe(fakeScope);
239 describe("delegate", function() {
241 beforeEach(function() {
244 fn: jasmine.createSpy(),
245 delegate: ".something",
250 child = Ext.get(element).createChild({
255 afterEach(function() {
256 var elChild = Ext.get(child);
262 describe("if filter found nothing", function() {
263 it("should not call the listener", function() {
264 Ext.EventManager.addListener(element, config);
265 jasmine.fireMouseEvent(child, "click", 1, 1);
267 expect(config.click.fn).not.toHaveBeenCalled();
271 describe("if filter found something", function() {
272 it("should call the listener", function() {
273 Ext.get(child).addCls("something");
274 Ext.EventManager.addListener(element, config);
275 jasmine.fireMouseEvent(child, "click", 1, 1);
277 expect(config.click.fn).toHaveBeenCalled();
281 describe("stopevent", function() {
284 beforeEach(function() {
287 fn: jasmine.createSpy(),
293 checkbox = Ext.get(element).createChild({
298 Ext.EventManager.addListener(element, config);
299 Ext.EventManager.addListener(checkbox, config);
300 if (jasmine.browser.isIE) {
303 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
307 afterEach(function() {
308 var checkBoxEl = Ext.get(checkbox);
312 Ext.EventManager.removeListener(checkbox, config);
315 it("should stop propagation to parent elements", function() {
316 expect(config.click.fn.calls.length).toEqual(1);
319 it("should prevent default browser handling of the event", function() {
320 expect(checkbox.checked).toBe(false);
324 describe("preventDefault", function() {
327 beforeEach(function() {
330 fn: jasmine.createSpy(),
331 preventDefault: true,
336 checkbox = Ext.get(element).createChild({
341 Ext.EventManager.addListener(element, config);
342 Ext.EventManager.addListener(checkbox, config);
343 if (jasmine.browser.isIE) {
346 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
350 afterEach(function() {
351 var checkBoxEl = Ext.get(checkbox);
355 Ext.EventManager.removeListener(checkbox, config);
358 it("should prevent default browser handling of the event", function() {
359 expect(checkbox.checked).toBe(false);
362 it("should not stop the propagation of the event", function() {
363 expect(config.click.fn.calls.length).toEqual(2);
367 describe("stopPropagation", function() {
370 beforeEach(function() {
373 fn: jasmine.createSpy(),
374 stopPropagation: true,
379 checkbox = Ext.getBody().createChild({
384 Ext.EventManager.addListener(element, config);
385 Ext.EventManager.addListener(checkbox, config);
386 if (jasmine.browser.isIE) {
389 jasmine.fireMouseEvent(checkbox, "click", 1, 1);
394 afterEach(function() {
395 var checkBoxEl = Ext.get(checkbox);
399 Ext.EventManager.removeListener(checkbox, config);
402 it("should stop propagation to parent elements", function() {
403 expect(config.click.fn.calls.length).toEqual(1);
406 it("should not prevent default browser handling of the event", function() {
407 expect(checkbox.checked).toBe(true);
411 describe("normalized", function() {
413 beforeEach(function() {
416 fn: jasmine.createSpy(),
422 Ext.EventManager.addListener(element, config);
424 event = jasmine.fireMouseEvent(element, "click", 1, 1);
427 it("should pass a browser event instead of an Ext.EventObject", function() {
428 expect(config.click.fn).toHaveBeenCalledWith(event, element, config.click);
432 describe("delay", function() {
433 beforeEach(function() {
436 fn: jasmine.createSpy(),
442 Ext.EventManager.addListener(element, config);
445 it("should not call listener before 1 ms", function() {
446 jasmine.fireMouseEvent(element, "click", 1, 1);
447 expect(config.click.fn).not.toHaveBeenCalled();
450 it("should call listener after 1 ms", function() {
452 jasmine.fireMouseEvent(element, "click", 1, 1);
458 expect(config.click.fn).toHaveBeenCalled();
463 describe("single", function() {
464 beforeEach(function() {
467 fn: jasmine.createSpy(),
473 Ext.EventManager.addListener(element, config);
474 jasmine.fireMouseEvent(element, "click", 1, 1);
475 jasmine.fireMouseEvent(element, "click", 1, 1);
478 it("should call listener only one time", function() {
479 expect(config.click.fn.calls.length).toEqual(1);
483 describe("buffer", function() {
484 beforeEach(function() {
487 fn: jasmine.createSpy(),
493 Ext.EventManager.addListener(element, config);
494 jasmine.fireMouseEvent(element, "click", 1, 1);
495 jasmine.fireMouseEvent(element, "click", 1, 1);
498 it("should call listener only one time", function() {
501 expect(config.click.fn.calls.length).toEqual(1);
506 describe("target", function() {
508 beforeEach(function() {
509 child = Ext.get(element).createChild({
514 fn: jasmine.createSpy(),
520 Ext.EventManager.addListener(element, config);
523 afterEach(function() {
524 var childEl = Ext.get(child);
530 it("should call listener if element event is fired", function() {
531 jasmine.fireMouseEvent(element, "click", 1, 1);
532 expect(config.click.fn).toHaveBeenCalled();
535 it("should not call listener if child event is fired (no bubbling)", function() {
536 jasmine.fireMouseEvent(child, "click", 1, 1);
537 expect(config.click.fn).not.toHaveBeenCalled();
544 describe("removeListener", function() {
545 describe("if eventName is an object", function() {
546 it("should call prepareListenerConfig", function() {
548 spyOn(Ext.EventManager, "prepareListenerConfig");
549 Ext.EventManager.removeListener(element, eventName);
550 expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName, true);
554 describe("event firing", function() {
557 beforeEach(function() {
560 fn: jasmine.createSpy(),
565 Ext.EventManager.addListener(element, config);
566 Ext.EventManager.removeListener(element, config);
569 it("should not call the listener", function() {
570 jasmine.fireMouseEvent(element, "click", 1, 1);
572 expect(config.click.fn).not.toHaveBeenCalled();
577 describe("removeAll", function() {
580 beforeEach(function() {
583 fn: jasmine.createSpy(),
587 fn: jasmine.createSpy(),
592 Ext.EventManager.addListener(element, config);
593 Ext.EventManager.removeAll(element, config);
596 it("should should not call click listener", function() {
597 jasmine.fireMouseEvent(element, "click", 1, 1);
599 expect(config.click.fn).not.toHaveBeenCalled();
602 it("should not call mouseover listener", function() {
603 jasmine.fireMouseEvent(element, "mouseover", 1, 1);
605 expect(config.mouseover.fn).not.toHaveBeenCalled();
609 describe("purgeElement", function() {
612 beforeEach(function() {
613 child = Ext.get(element).createChild({
619 fn: jasmine.createSpy(),
624 Ext.EventManager.addListener(element, config);
625 Ext.EventManager.addListener(child, config);
626 Ext.EventManager.purgeElement(element);
629 afterEach(function() {
630 var childEl = Ext.get(child);
636 it("should remove all listeners from element", function() {
637 jasmine.fireMouseEvent(element, "mouseover", 1, 1);
639 expect(config.mouseover.fn).not.toHaveBeenCalled();
642 it("should remove all listeners from element children", function() {
643 jasmine.fireMouseEvent(child, "mouseover", 1, 1);
645 expect(config.mouseover.fn).not.toHaveBeenCalled();
649 describe("methods alias", function() {
650 it("should alias addListener with on", function() {
651 expect(Ext.EventManager.on).toBe(Ext.EventManager.addListener);
654 it("should alias removeListener with un", function() {
655 expect(Ext.EventManager.un).toBe(Ext.EventManager.removeListener);