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