3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
7 Ext.test.session.addTest('Ext.Container', {
8 name: 'Test Ext.Container methods',
11 var ct = new Ext.Container({
14 ct.add(new Ext.Component());
16 Y.Assert.areEqual(1, ct.items.getCount(), 'Check adding a single item works');
18 ct.add(new Ext.Component({itemId: 'a'}), new Ext.Component({itemId: 'b'}), new Ext.Component({itemId: 'c'}));
19 Y.Assert.areEqual(4, ct.items.getCount(), 'Test add with param array');
20 Y.Assert.areEqual('b', ct.items.itemAt(2).itemId, 'Test that they are correctly added in order');
25 Y.Assert.areEqual(5, ct.items.getCount(), 'Test for lazy instantiation');
28 Y.Assert.isTrue(ct.items.last().isXType('box'), 'Test default type');
33 test_cascade: function(){
34 var ct = new Ext.Container({itemId: 'ct', depth: 0}),
42 /* our container will have:
47 for(i = 0; i < 3; ++i){
54 for(j = 0; j < 3; ++j){
55 id = 'item' + i + '_' + j;
61 for(k = 0; k < 3; ++k){
62 id = 'item' + i + '_' + j + '_' + k;
75 ct.cascade(function(c){
79 Y.Assert.areEqual(40, cnt, 'Test each item is iterated');
80 Y.ArrayAssert.itemsAreEqual(order, items, 'Test that items are iterated depth first');
83 ct.cascade(function(){
84 items.push(this.itemId);
86 Y.ArrayAssert.itemsAreEqual(order, items, 'Test that the scope defaults to the current component');
89 ct.cascade(function(c){
90 cnt += c.depth * this.val;
93 Y.Assert.areEqual(306, cnt, 'Test that custom scope works');
96 ct.cascade(function(a, b){
97 cnt += this.depth * (a + b);
100 Y.Assert.areEqual(306, cnt, 'Test that custom args work');
103 ct.cascade(function(){
104 if(this.itemId == 'item0' || this.itemId == 'item2'){
109 Y.Assert.areEqual(14, cnt, 'Test that returning false stops iteration on a branch');
115 test_find: function(){
116 var ct = new Ext.Container({
123 items: [{}, {prop: 'foo', id: 'd'}, {}, {}, {}, {
125 items: [{}, {}, {}, {}]
131 items: [{prop: 'foo', id: 'e'}, {prop: 'food'}, {
154 var arr = ct.find('prop', 'foo');
155 Y.Assert.areEqual(5, arr.length, 'Ensure correct items are found, at all depths');
156 Y.ArrayAssert.itemsAreEqual(['a', 'd', 'e', 'f', 'c'], Ext.pluck(arr, 'id'), 'Test order the items are found, should be depth first');
158 arr = ct.find('none', 'x');
159 Y.ArrayAssert.isEmpty(arr, 'Test with non existent property');
161 arr = ct.find('foo', 'not here');
162 Y.ArrayAssert.isEmpty(arr, 'Test with property that exists, but no matches');
164 arr = ct.items.first().find('prop', 'foo');
165 Y.Assert.areEqual(1, arr.length, 'Ensure correct items are found, at one child level');
166 Y.ArrayAssert.itemsAreEqual(['d'], Ext.pluck(arr, 'id'), 'Test order the items are found');
168 arr = ct.find('special', 'main');
169 Y.ArrayAssert.isEmpty(arr, 'Ensure the container itself isn\'t found');
175 test_findBy: function(){
176 var ct = new Ext.Container({
218 var arr = ct.findBy(function(){
219 return this.foo % 2 == 0;
222 Y.Assert.areEqual(8, arr.length, 'Test correct items are returned, also tests that scope defaults to the component');
223 Y.ArrayAssert.itemsAreEqual([6, 2, 8, 10, 4, 12, 14, 16], Ext.pluck(arr, 'foo'), 'Test items are returned in the correct order');
225 arr = ct.findBy(function(c){
229 Y.ArrayAssert.isEmpty(arr, 'Ensure that the container is not included in the findBy');
231 arr = ct.findBy(function(c){
232 return c.foo % this.val == 0;
235 Y.Assert.areEqual(5, arr.length, 'Test correct items are returned, also test custom scope');
241 test_findByType: function(){
242 var ct = new Ext.Container({
284 var arr = ct.findByType('component');
285 Y.Assert.areEqual(15, arr.length, 'Check correct number of items are returned');
287 arr = ct.findByType('box', true);
288 Y.Assert.areEqual(4, arr.length, 'Test shallow parameter is respected');
290 arr = ct.findByType('grid');
291 Y.Assert.areEqual(0, arr.length, 'Ensure that no items are returned if there\'s no matches');
293 arr = ct.findByType('container');
294 Y.Assert.areEqual(8, arr.length, 'Check all items are found at different depths');
300 test_getComponent: function(){
301 var component1 = new Ext.Component({itemId: 'd'}),
302 component2 = new Ext.Component({itemId: 'e'}),
303 component3 = new Ext.Component();
305 var ct = new Ext.Container({
319 Y.Assert.areEqual('b', ct.getComponent(1).itemId, 'Test accessing by index');
320 Y.Assert.areEqual('a', ct.getComponent(0).id, 'Test accessing by index');
322 Y.Assert.isUndefined(ct.getComponent(100), 'Test grabbing an index outside of bounds');
323 Y.Assert.isUndefined(ct.getComponent(null), 'Test with passing null');
324 Y.Assert.isUndefined(ct.getComponent('foo'), 'Test with passing id that doesn\'t exist');
325 Y.Assert.isUndefined(ct.getComponent(component3), 'Test with passing a component that doesn\'t belong to the container');
327 Y.Assert.areEqual(component1, ct.getComponent(component1), 'Test passing in a component instance');
328 Y.Assert.areEqual(component2, ct.getComponent(component2), 'Test passing in a component instance');
330 Y.Assert.areEqual('d', ct.getComponent('d').itemId, 'Test passing in itemId');
331 Y.Assert.areEqual('c', ct.getComponent('c').itemId, 'Test the above with an itemId');
333 component3.destroy();
338 test_insert: function(){
339 var ct = new Ext.Container({
350 ct.insert(1, {itemId: 'a'});
351 Y.Assert.areEqual('a', ct.items.itemAt(1).itemId, 'Test simple insert');
353 ct.insert(0, {itemId: 'b'});
354 Y.Assert.areEqual('b', ct.items.first().itemId, 'Test insert at 0');
356 ct.insert(ct.items.getCount(), {itemId: 'c'});
357 Y.Assert.areEqual('c', ct.items.last().itemId, 'Test inserting at the last index');
359 ct.insert(100, {itemId: 'd'});
360 Y.Assert.areEqual('d', ct.items.last().itemId, 'Test inserting well after the last index');
362 ct.insert(2, {itemId: 'e'}, {itemId: 'f'}, {itemId: 'g'});
363 Y.Assert.areEqual('e', ct.items.itemAt(2).itemId, 'Test inserting multiple items at once');
364 Y.Assert.areEqual('f', ct.items.itemAt(3).itemId, 'Test inserting multiple items at once');
365 Y.Assert.areEqual('g', ct.items.itemAt(4).itemId, 'Test inserting multiple items at once');
367 ct.insert(3, new Ext.Component({itemId: 'h'}));
368 Y.Assert.areEqual('h', ct.items.itemAt(3).itemId, 'Test simple insert');
370 Y.Assert.areEqual('first', ct.items.itemAt(1).itemId, 'Test original component held position');
376 test_remove: function(){
380 component1 = new Ext.Component(),
381 component2 = new Ext.Component(),
382 component3 = new Ext.Component();
384 for(var i = 0; i < 10; ++i){
385 items.push({itemId: 'item' + i});
387 items.push(component1, component2);
388 ct = new Ext.Container({items: items});
391 Y.Assert.areEqual(11, ct.items.getCount(), 'Test remove by index');
394 Y.Assert.areEqual(11, ct.items.getCount(), 'Test remove by index, where the index doesn\'t exist')
395 Y.Assert.isUndefined(c, 'Ensure undefined is returned if the item doesn\'t exist');
397 c = ct.remove(component1);
398 Y.Assert.areEqual(10, ct.items.getCount(), 'Test remove with a component instance');
399 Y.Assert.areEqual(component1, c, 'Test component is returned correctly');
401 c = ct.remove(component3);
402 Y.Assert.areEqual(10, ct.items.getCount(), 'Test removing an instance that doesn\'t exist in the component');
403 Y.Assert.isUndefined(c, 'Ensure that the empty is returned in this case');
405 c = ct.remove('item5');
406 Y.Assert.areEqual(9, ct.items.getCount(), 'Test removing with an itemId');
407 Y.Assert.areEqual('item5', c.itemId, 'Ensure component is returned properly');
409 //test autoDestroy behaviour
411 Y.Assert.isTrue(c.isDestroyed, 'Test that container autoDestroy/true is respected');
413 c = ct.remove(0, true);
414 Y.Assert.isTrue(c.isDestroyed, 'Test that container autoDestroy/true and destroy/true works');
416 c = ct.remove(0, false);
417 Y.Assert.isUndefined(c.isDestroyed, 'Test that destroy/false overrides autoDestroy/true');
419 ct.autoDestroy = false;
421 Y.Assert.isUndefined(c.isDestroyed, 'Test that autoDestroy/false is respected');
423 c = ct.remove(0, false);
424 Y.Assert.isUndefined(c.isDestroyed, 'Test autoDestroy/false and destroy/false works');
426 c = ct.remove(0, true);
427 Y.Assert.isTrue(c.isDestroyed, 'Test destroy/true overrides autoDestroy/false');
429 component3.destroy();
434 test_removeAll: function(){
435 var ct = new Ext.Container(),
439 arr = ct.removeAll();
440 Y.ArrayAssert.isEmpty(arr, 'Test removeAll with no items returns an empty array');
442 for(i = 0; i < 3; ++i){
448 arr = ct.removeAll();
449 Y.Assert.areEqual(0, ct.items.getCount(), 'Ensure all items are removed');
450 Y.ArrayAssert.itemsAreEqual(['item0', 'item1', 'item2'], Ext.pluck(arr, 'itemId'), 'Ensure they are removed in a consistent order');
452 for(i = 0; i < 2; ++i){
457 arr = ct.removeAll(true);
458 Y.ArrayAssert.itemsAreEqual([true, true], Ext.pluck(arr, 'isDestroyed'), 'Check destroy is honoured');
460 ct.autoDestroy = true;
461 for(i = 0; i < 3; ++i){
466 arr = ct.removeAll();
467 Y.ArrayAssert.itemsAreEqual([true, true, true], Ext.pluck(arr, 'isDestroyed'), 'Check autoDestroy is honoured');