/ext-4.1.0_b3/src/core/test/unit/spec/EventManager.js
JavaScript | 641 lines | 524 code | 116 blank | 1 comment | 14 complexity | ea23b699462d6880b3063513ee0bc22a MD5 | raw file
- describe("Ext.EventManager", function() {
- var element,
- elementWithId,
- elementWithoutId;
-
- beforeEach(function() {
- // add global variable in whitelist
- addGlobal("id");
- addGlobal("ExtBox1");
- element = document.body;
-
- elementWithId = document.createElement("DIV");
- elementWithId.id = 'element-with-id';
- element.appendChild(elementWithId);
-
- elementWithoutId = document.createElement("DIV");
- element.appendChild(elementWithoutId);
- });
-
- afterEach(function() {
- var el = Ext.get(elementWithId);
- if (el) {
- el.remove();
- }
- el = Ext.get(elementWithoutId);
- if (el) {
- el.remove();
- }
- });
-
- describe("event binding", function() {
- describe("getId", function() {
- describe("if element has an id", function() {
- it("should return element id", function() {
- expect(elementWithId.id).toBe(Ext.EventManager.getId(elementWithId));
- });
- });
-
- describe("if element hasn't an id", function() {
- var id, result;
- beforeEach(function() {
- id = 'nf-42';
- spyOn(Ext, "id").andCallFake(function(dom) {
- return (dom.id = id);
- });
- result = Ext.EventManager.getId(elementWithoutId);
- });
-
- it("should add an id to element with Ext.id", function() {
- expect(elementWithoutId.id).toBe(id);
- });
-
- it("should return the element id", function() {
- expect(result).toBe(id);
- });
-
- it("should add element to Ext.cache", function() {
- expect(Ext.cache[id].el.dom).toBe(elementWithoutId);
- });
- });
-
- describe("document and window", function() {
- describe("document", function() {
- var result;
- beforeEach(function() {
- result = Ext.EventManager.getId(document);
- });
- it("should add document Ext.Element to cache", function() {
- expect(Ext.cache[Ext.documentId].el.dom).toBe(document);
- });
-
- it("should enable skipGarbageCollection flag", function() {
- expect(Ext.cache[Ext.documentId].skipGarbageCollection).toBe(true);
- });
-
- it("should return Ext.documentId", function() {
- expect(result).toBe(Ext.documentId);
- });
- });
-
- describe("window", function() {
- var result;
- beforeEach(function() {
- result = Ext.EventManager.getId(window);
- });
-
- it("should add window Ext.Element to cache", function() {
- expect(Ext.cache[Ext.windowId].el.dom).toBe(window);
- });
-
- it("should enable skipGarbageCollection flag", function() {
- expect(Ext.cache[Ext.windowId].skipGarbageCollection).toBe(true);
- });
-
- it("should return Ext.windowId", function() {
- expect(result).toBe(Ext.windowId);
- });
- });
- });
- });
-
- describe("prepareListenerConfig", function() {
- var config, configWithFn;
-
- beforeEach(function() {
- config = {
- click: Ext.emptyFn,
- scope: fakeScope
- };
-
- configWithFn = {
- click: {
- fn: Ext.emptyFn,
- scope: fakeScope
- }
- };
- spyOn(Ext.EventManager, "removeListener");
- spyOn(Ext.EventManager, "addListener");
- });
-
- describe("if remove", function() {
- describe("with an object like click: function(){}, scope: this", function() {
- it("should call removeListener", function() {
- Ext.EventManager.prepareListenerConfig(element, config, true);
- expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
- });
- });
-
- describe("with an object like click: {fn: function(){}, scope: this}", function() {
- it("should call removeListener", function() {
- Ext.EventManager.prepareListenerConfig(element, configWithFn, true);
- expect(Ext.EventManager.removeListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
- });
- });
- });
-
- describe("if add", function() {
- describe("with an object like click: function(){}, scope: this", function() {
- it("should call addListener", function() {
- Ext.EventManager.prepareListenerConfig(element, config);
- expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, config);
- });
- });
-
- describe("with an object like click: {fn: function(){}, scope: this}", function() {
- it("should call addListener", function() {
- Ext.EventManager.prepareListenerConfig(element, configWithFn);
- expect(Ext.EventManager.addListener).toHaveBeenCalledWith(element, 'click', Ext.emptyFn, fakeScope, configWithFn.click);
- });
- });
- });
- });
-
- describe("addListener", function() {
- describe("if eventName is an object", function() {
- var eventName;
-
- beforeEach(function() {
- eventName = {};
- });
-
- it("should call prepareListenerConfig", function() {
- spyOn(Ext.EventManager, "prepareListenerConfig");
- Ext.EventManager.addListener(element, eventName);
- expect(Ext.EventManager.prepareListenerConfig).toHaveBeenCalledWith(element, eventName);
- });
- });
-
- describe("event firing", function() {
- var config;
-
- beforeEach(function() {
- config = {
- click: {
- fn: jasmine.createSpy(),
- scope: fakeScope
- }
- };
-
- Ext.EventManager.addListener(element, config);
- });
-
- afterEach(function() {
- Ext.EventManager.removeListener(element, config);
- });
-
- it("should call the listener", function() {
- jasmine.fireMouseEvent(element, "click", 1, 1);
-
- expect(config.click.fn).toHaveBeenCalled();
- });
-
- });
-
- describe("options", function() {
- var config;
-
- afterEach(function() {
- Ext.EventManager.removeListener(element, config);
- });
-
- describe("scope", function() {
- it("should call the listener with the correct scope", function() {
- config = {
- click: {
- fn: jasmine.createSpy(),
- scope: fakeScope
- }
- };
-
- Ext.EventManager.addListener(element, config);
- jasmine.fireMouseEvent(element, "click", 1, 1);
- expect(config.click.fn.calls[0].object).toBe(fakeScope);
- });
- });
-
- describe("delegate", function() {
- var child;
- beforeEach(function() {
- config = {
- click: {
- fn: jasmine.createSpy(),
- delegate: ".something",
- scope: fakeScope
- }
- };
-
- child = Ext.get(element).createChild({
- cls: "child"
- }).dom;
- });
-
- afterEach(function() {
- var elChild = Ext.get(child);
- if (elChild) {
- elChild.remove();
- }
- });
-
- describe("if filter found nothing", function() {
- it("should not call the listener", function() {
- Ext.EventManager.addListener(element, config);
- jasmine.fireMouseEvent(child, "click", 1, 1);
-