Upgrade to ExtJS 4.0.7 - Released 10/19/2011
[extjs.git] / src / core / test / unit / spec / dom / Element.traversal.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
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.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 describe("Ext.Element.traversal", function() {
16     var proto = Ext.Element,
17         el, testEl,
18         input, testInputEl,
19         child1, child2, child3, child4, child5;
20     
21     beforeEach(function() {
22         testEl = Ext.getBody().createChild({
23             id      : 'ExtElementHelper',
24             cls     : 'wrapper',
25             style   : 'position:absolute;',
26             children: [
27                 {id: 'child1', style: 'position:absolute;'},
28                 {id: 'child2', style: 'position:absolute;'},
29                 {id: 'child3', style: 'position:absolute;'},
30                 {
31                     id: 'child4',
32                     children: [
33                         {
34                             id : 'child5',
35                             cls: 'findIt'
36                         }
37                     ]
38                 }
39             ]
40         });
41         
42         testInputEl = Ext.getBody().createChild({
43             id  : 'ExtElementInputHelper',
44             tag : 'input',
45             type: 'text'
46         });
47         
48         el    = new Ext.Element(Ext.getDom(testEl));
49         input = new Ext.Element(Ext.getDom(testInputEl));
50         
51         child1 = Ext.get('child1');
52         child2 = Ext.get('child2');
53         child3 = Ext.get('child3');
54         child4 = Ext.get('child4');
55         child5 = Ext.get('child5');
56     });
57
58     afterEach(function() {
59         testEl.remove();
60         testInputEl.remove();
61     });
62     
63     describe("findParentNode", function() {
64         it("should return null", function() {
65             expect(el.findParentNode('body')).toBeNull();
66         });
67         
68         it("should return a dom", function() {
69             expect(child1.findParentNode('.wrapper')).toEqual(Ext.getDom(el));
70         });
71         
72         it("should return an el", function() {
73             expect(child1.findParentNode('.wrapper', null, true)).toEqual(el);
74         });
75         
76         describe("when maxDepth", function() {
77             describe("1", function() {
78                 it("should not return the el", function() {
79                     expect(child5.findParentNode('.wrapper', 1)).toBeNull();
80                 });
81             });
82             
83             describe("2", function() {
84                 it("should not return the el", function() {
85                     expect(child5.findParentNode('.wrapper', 2)).toEqual(Ext.getDom(el));
86                 });
87             });
88         });
89     });
90     
91     describe("up", function() {
92         it("should return null", function() {
93             expect(el.up('body')).toBeNull();
94         });
95         
96         it("should return a el", function() {
97             expect(child1.up('.wrapper')).toEqual(el);
98         });
99         
100         describe("when maxDepth", function() {
101             describe("1", function() {
102                 it("should not return the el", function() {
103                     expect(child5.up('.wrapper', 1)).toBeNull();
104                 });
105             });
106             
107             describe("2", function() {
108                 it("should not return the el", function() {
109                     expect(child5.up('.wrapper', 2)).toEqual(el);
110                 });
111             });
112         });
113     });
114     
115     describe("select", function() {
116         it("should return an Ext.CompositeELementLite", function() {
117             var result = el.select('div');
118             expect(result).toBeDefined();
119             expect(result.elements.length).toEqual(5);
120             expect(result instanceof Ext.CompositeElementLite).toBe(true);
121         });
122     });
123     
124     describe("query", function() {
125         it("should return elements", function() {
126             var result = el.query('div');
127             
128             expect(result).toBeDefined();
129             expect(result.length).toEqual(5);
130             expect(result.isComposite).toBeFalsy();
131             expect(Ext.isArray(result)).toBeTruthy();
132         });
133     });
134     
135     describe("down", function() {
136         it("should return an el", function() {
137             var result = el.down('.findIt');
138             
139             expect(result).toBeDefined();
140             expect(Ext.isElement(result)).toBeFalsy();
141         });
142         
143         it("should return a dom", function() {
144             var result = el.down('.findIt', true);
145             
146             expect(result).toBeDefined();
147             expect(Ext.isElement(result)).toBeTruthy();
148         });
149     });
150
151     describe("child", function() {
152         it("should return null", function() {
153             var result = el.child('.findIt');
154             
155             expect(result).toBeNull();
156         });
157         
158         it("should return an el", function() {
159             var result = child4.child('.findIt');
160             
161             expect(result).toBeDefined();
162             expect(Ext.isElement(result)).toBeFalsy();
163         });
164         
165         it("should return a dom", function() {
166             var result = child4.child('.findIt', true);
167             
168             expect(result).toBeDefined();
169             expect(Ext.isElement(result)).toBeTruthy();
170         });
171     });
172     
173     describe("parent", function() {
174         it("should return an el", function() {
175             var result = child1.parent();
176             
177             expect(result).toBeDefined();
178             expect(result).toEqual(el);
179             expect(Ext.isElement(result)).toBeFalsy();
180         });
181         
182         it("should return a dom", function() {
183             var result = child1.parent(null, true);
184             
185             expect(result).toBeDefined();
186             expect(result).toEqual(Ext.getDom(el));
187             expect(Ext.isElement(result)).toBeTruthy();
188         });
189     });
190     
191     describe("next", function() {
192         it("should return an el", function() {
193             var result = child1.next();
194             
195             expect(result).toBeDefined();
196             expect(result).toEqual(child2);
197             expect(Ext.isElement(result)).toBeFalsy();
198         });
199         
200         it("should return a dom", function() {
201             var result = child1.next(null, true);
202             
203             expect(result).toBeDefined();
204             expect(result).toEqual(Ext.getDom(child2));
205             expect(Ext.isElement(result)).toBeTruthy();
206         });
207     });
208     
209     describe("prev", function() {
210         it("should return an el", function() {
211             var result = child2.prev();
212             
213             expect(result).toBeDefined();
214             expect(result).toEqual(child1);
215             expect(Ext.isElement(result)).toBeFalsy();
216         });
217         
218         it("should return a dom", function() {
219             var result = child2.prev(null, true);
220             
221             expect(result).toBeDefined();
222             expect(result).toEqual(Ext.getDom(child1));
223             expect(Ext.isElement(result)).toBeTruthy();
224         });
225     });
226     
227     describe("first", function() {
228         it("should return an el", function() {
229             var result = el.first();
230             
231             expect(result).toBeDefined();
232             expect(result).toEqual(child1);
233             expect(Ext.isElement(result)).toBeFalsy();
234         });
235         
236         it("should return a dom", function() {
237             var result = el.first(null, true);
238             
239             expect(result).toBeDefined();
240             expect(result).toEqual(Ext.getDom(child1));
241             expect(Ext.isElement(result)).toBeTruthy();
242         });
243     });
244     
245     describe("last", function() {
246         it("should return an el", function() {
247             var result = el.last();
248             
249             expect(result).toBeDefined();
250             expect(result).toEqual(child4);
251             expect(Ext.isElement(result)).toBeFalsy();
252         });
253         
254         it("should return a dom", function() {
255             var result = el.last(null, true);
256             
257             expect(result).toBeDefined();
258             expect(result).toEqual(Ext.getDom(child4));
259             expect(Ext.isElement(result)).toBeTruthy();
260         });
261     });
262     
263     describe("findParent", function() {
264         it("should return null", function() {
265             expect(el.findParent('body')).toBeNull();
266         });
267         
268         it("should return a dom", function() {
269             expect(child1.findParent('.wrapper')).toEqual(Ext.getDom(el));
270         });
271         
272         it("should return an el", function() {
273             expect(child1.findParent('.wrapper', null, true)).toEqual(el);
274         });
275         
276         describe("when maxDepth", function() {
277             describe("1", function() {
278                 it("should not return the el", function() {
279                     expect(child5.findParent('.wrapper', 1)).toBeNull();
280                 });
281             });
282             
283             describe("2", function() {
284                 it("should not return the el", function() {
285                     expect(child5.findParent('.wrapper', 2)).toBeNull();
286                 });
287             });
288             
289             describe("3", function() {
290                 it("should return the el", function() {
291                     expect(child5.findParent('.wrapper', 3)).toEqual(Ext.getDom(el));
292                 });
293             });
294             
295             describe("NaN", function() {
296                 it("should use Number.MAX_VALUE", function() {
297                     expect(child5.findParent('.wrapper', Ext.getBody())).toEqual(Ext.getDom(el));
298                 });
299             });
300         });
301     });
302 }, "/src/dom/Element.traversal.js");
303