1 describe("Ext.core.Element", function() {
10 if (domEl && domEl.parentNode === document.body) {
11 document.body.removeChild(domEl);
15 describe("instantiation", function() {
16 beforeEach(function() {
17 domEl = document.createElement("div");
18 Ext.getBody().appendChild(domEl);
23 it("should set dom element id if it hasn't already one", function() {
24 element = new Ext.core.Element(domEl);
26 expect(domEl.id).toBeDefined();
29 it("should not set dom element id if it has already one", function() {
33 element = new Ext.core.Element(domEl);
35 expect(domEl.id).toEqual(id);
38 it("should set dom property to dom element", function() {
39 element = new Ext.core.Element(domEl);
41 expect(element.dom).toBe(domEl);
44 it("should set id property to dom id", function() {
48 element = new Ext.core.Element(domEl);
50 expect(element.id).toEqual(id);
53 it("should not set dom or id property if first argument is undefined", function() {
54 element = new Ext.core.Element();
56 expect(element.id).toBeUndefined();
57 expect(element.dom).toBeUndefined();
60 it("should find a dom element if a string corresponding to it's id is passed as first argument", function() {
65 element = new Ext.core.Element(id);
67 expect(element.dom).toBe(domEl);
71 describe("methods", function() {
72 beforeEach(function() {
73 element = Ext.getBody().createChild({tag: "div"});
77 describe("set", function() {
78 it("should call Ext.core.DomHelper.applyStyles if object passed as first argument has style property", function() {
79 var style = {width:'100px'};
81 spyOn(Ext.core.DomHelper, "applyStyles");
83 element.set({style: style});
85 expect(Ext.core.DomHelper.applyStyles).toHaveBeenCalledWith(element.dom, style);
88 it("should set dom element className if object passed as first argument has cls property", function() {
89 var cls = "x-test-class";
91 element.set({cls: cls});
93 expect(element.dom.className).toEqual(cls);
96 it("should use setAttribute by default", function() {
97 spyOn(element.dom, "setAttribute");
99 element.set({align: "center"});
101 expect(element.dom.setAttribute).toHaveBeenCalledWith("align", "center");
104 it("should be able to use expandos", function() {
105 spyOn(element.dom, "setAttribute");
107 element.set({align: "center"}, false);
110 expect(element.dom.align).toEqual("center");
115 describe("is", function() {
116 it("Returns true if this element matches the passed simple selector", function() {
117 element.set({cls: "x-test-class"});
119 expect(element.is("div.x-test-class")).toBe(true);
123 describe("focus", function() {
124 it("should focus dom element", function() {
125 spyOn(element.dom, "focus");
129 expect(element.dom.focus).toHaveBeenCalled();
132 it("should be able to defer dom element focus", function() {
133 spyOn(element.dom, "focus");
137 return element.dom.focus.calls.length === 1;
138 }, "element.dom.focus was never called");
141 expect(element.dom.focus).toHaveBeenCalled();
145 it("should ignore any exception", function() {
146 element.dom.focus = function() {
150 expect(element.focus).not.toThrow("error");
154 describe("blur", function() {
155 it("should blur dom element", function() {
156 spyOn(element.dom, "blur");
160 expect(element.dom.blur).toHaveBeenCalled();
164 it("should ignore any exception", function() {
165 element.dom.blur = function() {
169 expect(element.blur).not.toThrow("error");
173 describe("getValue", function() {
174 beforeEach(function() {
175 element.dom.value = "10";
178 it("should return the dom value", function() {
179 expect(element.getValue()).toEqual("10");
182 it("should return the dom value as Number", function() {
183 expect(element.getValue(true)).toEqual(10);
187 describe("listeners", function() {
190 beforeEach(function() {
191 options = {delay: 10};
194 describe("addListener", function() {
195 it("should call Ext.EventManager.on", function() {
196 spyOn(Ext.EventManager, "on");
198 element.addListener("click", Ext.emptyFn, fakeScope, options);
200 expect(Ext.EventManager.on).toHaveBeenCalledWith(element.dom, "click", Ext.emptyFn, fakeScope, options);
204 describe("removeListener", function() {
205 it("should call Ext.EventManager.un", function() {
206 spyOn(Ext.EventManager, "un");
208 element.removeListener("click", Ext.emptyFn, fakeScope);
210 expect(Ext.EventManager.un).toHaveBeenCalledWith(element.dom, "click", Ext.emptyFn, fakeScope);
214 describe("removeAllListener", function() {
215 it("should call Ext.EventManager.removeAll", function() {
216 spyOn(Ext.EventManager, "removeAll");
218 element.removeAllListeners();
220 expect(Ext.EventManager.removeAll).toHaveBeenCalledWith(element.dom);
224 describe("purgeAllListener", function() {
225 it("should call Ext.EventManager.purgeElement", function() {
226 spyOn(Ext.EventManager, "purgeElement");
228 element.purgeAllListeners();
230 expect(Ext.EventManager.purgeElement).toHaveBeenCalledWith(element);
235 describe("addUnits", function() {
236 it("should return an empty string if size passed is an empty string", function() {
237 expect(element.addUnits("")).toEqual("");
240 it("should return auto if size passed is 'auto' string", function() {
241 expect(element.addUnits("auto")).toEqual("auto");
244 it("should return an empty string if size passed is undefined", function() {
245 expect(element.addUnits(undefined)).toEqual("");
248 it("should return an empty string if size passed is null", function() {
249 expect(element.addUnits(null)).toEqual("");
253 describe("remove", function() {
254 beforeEach(function() {
255 spyOn(Ext, "removeNode").andCallThrough();
259 it("should remove dom property", function() {
260 expect(element.dom).toBeUndefined();
263 it("should call Ext.removeNode", function() {
264 expect(Ext.removeNode).toHaveBeenCalledWith(domEl);
268 describe("hover", function() {
269 var overFn, outFn, options;
270 beforeEach(function() {
271 overFn = function() {
283 spyOn(element, "on");
286 describe("mouseenter event", function() {
287 it("should add a listener on mouseenter", function() {
288 element.hover(overFn, outFn, fakeScope, options);
290 expect(element.on).toHaveBeenCalledWith("mouseenter", overFn, fakeScope, options);
293 it("should set scope to element.dom if it is not passed in arguments", function() {
294 element.hover(overFn, outFn, null, options);
296 expect(element.on).toHaveBeenCalledWith("mouseenter", overFn, element.dom, options);
300 describe("mouseleave event", function() {
301 it("should add a listener on mouseleave", function() {
302 element.hover(overFn, outFn, fakeScope, options);
304 expect(element.on).toHaveBeenCalledWith("mouseleave", outFn, fakeScope, options);
307 it("should set scope to element.dom if it is not passed in arguments", function() {
308 element.hover(overFn, outFn, null, options);
310 expect(element.on).toHaveBeenCalledWith("mouseleave", outFn, element.dom, options);
315 describe("contains", function() {
317 * TODO: Removed tests for now, need to reinstate once the refactoring is done.
321 describe("getAttributeNs", function() {
322 it("should call element getAttribute", function() {
323 spyOn(element, "getAttribute");
325 element.getAttributeNS("ns1", "align");
327 expect(element.getAttribute).toHaveBeenCalledWith("align", "ns1");
331 describe("getAttribute", function() {
332 var element2, element3;
333 beforeEach(function() {
334 element2 = Ext.getBody().createChild({tag: "div"});
337 if (element.dom.setAttribute) {
338 element.dom.setAttribute("qtip", "bar");
339 element2.dom.setAttribute("ext:qtip", "foo");
341 element.dom["qtip"] = "bar";
342 element2.dom["ext:qtip"] = "foo";
345 if (element.dom.setAttributeNS) {
346 element3 = Ext.getBody().createChild({tag: "div"});
347 element3.dom.setAttributeNS("ext", "qtip", "foobar");
351 afterEach(function() {
361 describe("without namespace", function() {
362 it("should return the attribute value if it exists", function() {
363 expect(element.getAttribute("qtip")).toEqual("bar");
366 it("should return null if the attribute does not exist", function() {
367 expect(element.getAttribute("nothing")).toBeNull();
371 describe("with namespace", function() {
372 it("should return null on a non-namespaced attribute", function() {
373 expect(element.getAttribute("qtip", "ext")).toBeNull();
376 it("should return null if the attribute belong to another namespace", function() {
377 expect(element2.getAttribute("qtip", "nothing")).toBeNull();
380 it("should return the attribute value if it belongs to the namespace", function() {
382 expect(element3.getAttribute("qtip", "ext")).toEqual("foobar");
386 it("should handle xml namespace", function() {
387 expect(element2.getAttribute("qtip", "ext")).toEqual("foo");
392 describe("update", function() {
393 beforeEach(function() {
394 element.dom.innerHTML = "hello world";
397 it("should update dom element innerHTML", function() {
398 element.update("foobar");
400 expect(element.dom).hasHTML("foobar");
403 it("should return element", function() {
404 expect(element.update("foobar")).toBe(element);
408 describe("prototype aliases", function() {
409 it("should aliases addListener with on", function() {
410 expect(element.on).toBe(element.addListener);
413 it("should aliases removeListener with un", function() {
414 expect(element.un).toBe(element.removeListener);
417 it("should aliases removeAllListeners with clearListeners", function() {
418 expect(element.clearListeners).toBe(element.removeAllListeners);
423 describe("class methods", function() {
424 var element2, domEl2, id;
426 beforeEach(function() {
427 element = Ext.getBody().createChild({tag: "div"});
431 domEl2 = document.createElement("div");
433 document.body.appendChild(domEl2);
435 spyOn(Ext.core.Element, "addToCache").andCallThrough();
438 afterEach(function() {
442 if (domEl2 && domEl2.parentNode === document.body) {
443 document.body.removeChild(domEl2);
447 describe("get", function() {
448 describe("alias", function() {
449 it("should alias Ext.core.Element.get with Ext.get", function() {
450 expect(Ext.get).toBe(Ext.core.Element.get);
454 describe("passing string id as first argument", function() {
455 describe("with a dom element which is not already encapsulated", function() {
456 it("should return a new Ext.core.Element", function() {
457 element2 = Ext.get(id);
459 expect(element2 instanceof Ext.core.Element).toBe(true);
462 it("should encapsulate the dom element in the Ext.core.Element", function() {
463 element2 = Ext.get(id);
465 expect(element2.dom).toBe(domEl2);
468 it("should add element to Ext.cache", function() {
469 element2 = Ext.get(id);
471 expect(Ext.core.Element.addToCache).toHaveBeenCalledWith(element2);
475 describe("with a dom element which is already encapsulated", function() {
476 it("should return the corresponding Ext.element", function() {
477 expect(Ext.get(domEl)).toBe(element);
480 it("should not add element to Ext.cache if it is already in", function() {
483 expect(Ext.core.Element.addToCache).not.toHaveBeenCalled();
488 describe("passing dom element as first argument", function() {
489 describe("with a dom element which is not already encapsulated", function() {
490 it("should return a new Ext.core.Element", function() {
491 element2 = Ext.get(domEl2);
493 expect(element2 instanceof Ext.core.Element).toBe(true);
496 it("should encapsulate the dom element in the Ext.core.Element", function() {
497 element2 = Ext.get(domEl2);
499 expect(element2.dom).toBe(domEl2);
502 it("should add element to Ext.cache", function() {
503 element2 = Ext.get(domEl2);
505 expect(Ext.core.Element.addToCache).toHaveBeenCalledWith(element2);
509 describe("with a dom element which is already encapsulated", function() {
510 it("should return the corresponding Ext.element", function() {
511 expect(Ext.get(domEl.id)).toBe(element);
514 it("should not add element to Ext.cache if it is already in", function() {
517 expect(Ext.core.Element.addToCache).not.toHaveBeenCalled();
522 describe("passing an Ext.core.Element as first argument", function() {
523 it("should return Ext.core.Element", function() {
524 expect(Ext.get(element)).toBe(element);
528 describe("passing a CompositeElement as first argument", function() {
529 var compositeElement;
531 beforeEach(function() {
532 compositeElement = Ext.select("div");
535 it("should return Ext.core.Element", function() {
536 expect(Ext.get(compositeElement)).toBe(compositeElement);
540 describe("passing an array as first argument", function() {
541 it("should call Ext.core.Element.select", function() {
542 var arr = [domEl, domEl2];
543 spyOn(Ext.core.Element, "select");
547 expect(Ext.core.Element.select).toHaveBeenCalledWith(arr);
551 describe("passing document as first argument", function() {
552 it("should return an Ext.core.Element", function() {
553 expect(Ext.get(document) instanceof Ext.core.Element).toBe(true);
556 it("should return a bogus Ext.core.Element", function() {
557 expect(Ext.get(document).id).not.toBeDefined();
560 it("should return an Ext.core.Element that encapsulate document", function() {
561 expect(Ext.get(document).dom).toBe(document);
566 xdescribe("garbageCollector", function() {
570 describe("fly", function() {
573 beforeEach(function() {
574 spyOn(Ext, "getDom").andCallThrough();
578 describe("global flyweight", function() {
579 beforeEach(function() {
580 flyWeight = Ext.fly(domEl2);
583 it("should return an Ext.core.Element.Flyweight", function() {
584 expect(flyWeight instanceof Ext.core.Element.Flyweight).toBe(true);
587 it("should not cache a dom element", function() {
588 expect(Ext.cache[domEl2.id]).toBeUndefined();
591 it("should call Ext.getDom", function() {
592 expect(Ext.getDom).toHaveBeenCalledWith(domEl2);
595 it("should create a one time reference", function() {
596 expect(Ext.core.Element._flyweights._global).toEqual(flyWeight);
600 describe("named reusable flyweight", function() {
601 beforeEach(function() {
602 flyWeight = Ext.fly(domEl2, "myflyweight");
605 it("should return an Ext.core.Element.Flyweight", function() {
606 expect(flyWeight instanceof Ext.core.Element.Flyweight).toBe(true);
609 it("should not cache a dom element", function() {
610 expect(Ext.cache[domEl2.id]).toBeUndefined();
613 it("should call Ext.getDom", function() {
614 expect(Ext.getDom).toHaveBeenCalledWith(domEl2);
617 it("should create a one time reference", function() {
618 expect(Ext.core.Element._flyweights.myflyweight).toEqual(flyWeight);
623 describe("aliases", function() {
624 it("should aliases Ext.core.Element.get with Ext.get", function() {
625 expect(Ext.get).toBe(Ext.core.Element.get);
628 it("should aliases Ext.element.fly with Ext.fly", function() {
629 expect(Ext.fly).toBe(Ext.core.Element.fly);
633 }, "/src/dom/Element.js");