3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 var suite = Ext.test.session.getSuite('Ext.util.MixedCollection'),
11 suite.add(new Y.Test.Case({
15 this.mc = new Ext.util.MixedCollection();
18 tearDown: function() {
22 //test that a default getKey implementation is set
23 testHasDefaultGetKey: function() {
24 var item1 = {id: 1, data: 'first item' },
25 item2 = {id: 2, data: 'second item'};
30 assert.areSame(item1, this.mc.get(1));
31 assert.areSame(item2, this.mc.get(2));
34 //test that we can provide a getKey implementation
35 testCanSetGetKey: function() {
36 var collection = new Ext.util.MixedCollection(false, function(item) {
40 var item1 = {myKey: 'a', data: 'first item' },
41 item2 = {myKey: 'b', data: 'second item'};
43 collection.add(item1);
44 collection.add(item2);
46 assert.areSame(item2, collection.get('b'));
47 assert.areSame(item1, collection.get('a'));
51 suite.add(new Y.Test.Case({
55 this.mc = new Ext.util.MixedCollection();
58 {id: 1, name: 'first'},
59 {id: 2, name: 'second'},
60 {id: 3, name: 'third'}
64 testEach: function() {
65 var callCount = 0, callScope, total;
67 this.mc.each(function(item, index, length) {
68 //make sure that the function is called in the correct scope
74 assert.areEqual(this, callScope);
75 assert.areEqual(3, callCount);
76 assert.areEqual(3, total);
79 testEachKey: function() {
80 var callCount = 0, callScope;
82 this.mc.eachKey(function(key, index, length) {
83 //make sure that the function is called in the correct scope
88 assert.areEqual(this, callScope);
89 assert.areEqual(3, callCount);
93 suite.add(new Y.Test.Case({
94 name: 'add and remove',
97 this.mc = new Ext.util.MixedCollection();
100 testAddAll: function() {
103 assert.areEqual(0, mc.length);
105 mc.addAll([{id: 1}, {id: 2}, {id: 3}]);
107 assert.areEqual(3, mc.length);
110 testAddAndClear: function() {
117 assert.areEqual(3, mc.length);
120 assert.areEqual(0, mc.length);
123 testAddEventFired: function() {
127 mc.on('add', function() {fired = true;});
130 assert.isTrue(fired);
133 testClearEventFired: function() {
137 mc.on('clear', function() {fired = true;}, this);
140 assert.isTrue(fired);
143 testGetCount: function() {
144 this.mc.add({id: 1});
145 this.mc.add({id: 2});
146 this.mc.add({id: 3});
148 assert.areEqual(3, this.mc.getCount());
151 testRemove: function() {
155 testRemoveFiresEvent: function() {
160 suite.add(new Y.Test.Case({
164 this.mc = new Ext.util.MixedCollection();
167 {id: 1, name: 'first'},
168 {id: 2, name: 'second'},
169 {id: 3, name: 'third'}
173 doInsert: function() {
174 this.mc.insert(1, {id: 4, name: 'fourth'});
177 testInsertsToCorrectLocation: function() {
180 assert.areEqual(4, this.mc.itemAt(1).id);
183 testOtherItemsPreserved: function() {
184 var prevCount = this.mc.getCount();
187 assert.areEqual(prevCount + 1, this.mc.getCount());
190 testFiresAddEvent: function() {
193 this.mc.on('add', function() { fired = true; });
196 assert.isTrue(fired);
200 suite.add(new Y.Test.Case({
204 this.mc = new Ext.util.MixedCollection();
207 {id: 1, name: 'first'},
208 {id: 2, name: 'second'},
209 {id: 3, name: 'third'}
213 doReplace: function() {
214 this.mc.replace(2, {id: 4, name: 'fourth'});
217 testReplacesCorrectItem: function() {
219 assert.areEqual("fourth", this.mc.itemAt(1).name);
222 testPreviousItemRemoved: function() {
223 var prevCount = this.mc.getCount();
226 assert.areEqual(prevCount, this.mc.getCount());
229 testReplaceEventFired: function() {
232 this.mc.on('replace', function() { fired = true; });
235 assert.isTrue(fired);
239 suite.add(new Y.Test.Case({
243 this.mc = new Ext.util.MixedCollection();
246 {id: 1, name: 'first'},
247 {id: 2, name: 'second'},
248 {id: 3, name: 'third'}
252 //test that a shallow clone is completed correctly
253 testClone: function() {
254 var newMC = this.mc.clone();
256 assert.areEqual(3, newMC.getCount());
258 Ext.each([1, 2, 3], function(id) {
259 assert.areEqual(this.mc.get(id).id, newMC.get(id).id);
264 suite.add(new Y.Test.Case({
265 name: 'getting items',
268 this.mc = new Ext.util.MixedCollection();
269 this.item1 = {id: 1, name: 'first'};
273 {id: 2, name: 'second'},
274 {id: 3, name: 'third'}
278 testFirst: function() {
279 assert.areEqual(1, this.mc.first().id);
282 testLast: function() {
283 assert.areEqual(3, this.mc.last().id);
286 testGet: function() {
287 assert.areEqual(2, this.mc.get(2).id);
290 testGetKey: function() {
291 assert.areEqual(1, this.mc.getKey(this.item1));
294 //should return items in the given range
295 testGetRange: function() {
296 var items = this.mc.getRange(1, 2);
298 assert.areEqual(2, items.length);
299 assert.areEqual(2, items[0].id);
300 assert.areEqual(3, items[1].id);
303 //should get all items
304 testGetRangeWithNoArguments: function() {
305 var items = this.mc.getRange();
307 assert.areEqual(3, items.length);
310 //should get all items after the provided start index
311 testGetRangeWithNoEnd: function() {
312 var items = this.mc.getRange(1);
314 assert.areEqual(2, items.length);
317 testIndexOf: function() {
318 assert.areEqual(0, this.mc.indexOf(this.item1));
321 testIndexOfKey: function() {
322 assert.areEqual(2, this.mc.indexOfKey(3));
325 testKey: function() {
326 assert.areEqual(3, this.mc.key(3).id);
329 testItemByIndex: function() {
330 this.mc.add({id: 'a', name: 'another item'});
331 this.mc.add({id: 'b', name: 'yet another item'});
333 assert.areEqual('b', this.mc.item(4).id);
336 //key should take priority over index
337 testItemByKey: function() {
338 this.mc.add({id: 'a', name: 'another item'});
340 assert.areEqual('a', this.mc.item('a').id);
343 testItemAt: function() {
344 assert.areEqual(3, this.mc.itemAt(2).id);
348 suite.add(new Y.Test.Case({
349 name: 'find functions',
352 this.mc = new Ext.util.MixedCollection();
355 {id: 1, name: 'first'},
356 {id: 2, name: 'second'},
357 {id: 3, name: 'third'}
361 testFind: function() {
362 var matched = this.mc.find(function(item) {
363 return item.name == 'third';
366 assert.areEqual('third', matched.name);
369 testFindIndex: function() {
370 var matched = this.mc.findIndex('name', 'third');
372 assert.areEqual(2, matched);
375 testFindIndexBy: function() {
376 var matched = this.mc.findIndexBy(function(item) {
377 return item.name == 'second';
380 assert.areEqual(1, matched);
384 suite.add(new Y.Test.Case({
388 this.mc = new Ext.util.MixedCollection();
389 this.item = {id: 1, name: 'first'};
393 {id: 2, name: 'second'},
394 {id: 3, name: 'third'}
398 tearDown: function() {
402 testContains: function() {
403 assert.isTrue(this.mc.contains(this.item));
406 testDoesNotContain: function() {
407 assert.isFalse(this.mc.contains({some: 'object'}));
410 testContainsKey: function() {
411 assert.isTrue(this.mc.containsKey(1));
414 testDoesNotContainKey: function() {
415 assert.isFalse(this.mc.containsKey('abc'));
419 suite.add(new Y.Test.Case({
420 name: 'single sorting',
423 this.mc = new Ext.util.MixedCollection(false, function(item) {
428 {id: 1, name: 'first', code: 'C', modifier: 10},
429 {id: 2, name: 'second', code: 'A', modifier: 100},
430 {id: 3, name: 'third', code: 'B', modifier: 5}
434 testKeySort: function() {
438 assert.areEqual('A', mc.itemAt(0).code);
439 assert.areEqual('B', mc.itemAt(1).code);
440 assert.areEqual('C', mc.itemAt(2).code);
443 testDirectionalKeySort: function() {
447 assert.areEqual('C', mc.itemAt(0).code);
448 assert.areEqual('B', mc.itemAt(1).code);
449 assert.areEqual('A', mc.itemAt(2).code);
452 testSort: function() {
453 var mc = new Ext.util.MixedCollection();
454 mc.addAll(3, 1, 4, 2);
457 assert.areEqual(1, mc.itemAt(0));
458 assert.areEqual(2, mc.itemAt(1));
459 assert.areEqual(3, mc.itemAt(2));
460 assert.areEqual(4, mc.itemAt(3));
463 testDirectionalSort: function() {
467 testSortWithComparator: function() {
469 mc.sort('ASC', function(a, b) {
470 return (a.id * a.modifier) - (b.id * b.modifier);
473 assert.areEqual('C', mc.itemAt(0).code);
474 assert.areEqual('B', mc.itemAt(1).code);
475 assert.areEqual('A', mc.itemAt(2).code);
478 testDirectionalSortWithComparator: function() {
480 mc.sort('DESC', function(a, b) {
481 return (a.id * a.modifier) - (b.id * b.modifier);
484 assert.areEqual('A', mc.itemAt(0).code);
485 assert.areEqual('B', mc.itemAt(1).code);
486 assert.areEqual('C', mc.itemAt(2).code);
489 testSortEventFired: function() {
492 this.mc.on('sort', function() { fired = true; });
493 this.mc.sort('name');
495 assert.isTrue(fired);
499 suite.add(new Y.Test.Case({
503 this.mc = new Ext.util.MixedCollection(false, function(item) {
508 {id: 1, name: 'first', code: 'C', modifier: 10},
509 {id: 2, name: 'second', code: 'A', modifier: 100},
510 {id: 3, name: 'third', code: 'B', modifier: 5}
514 testReordering: function() {
522 assert.areEqual('B', mc.itemAt(0).code);
523 assert.areEqual('C', mc.itemAt(1).code);
524 assert.areEqual('A', mc.itemAt(2).code);
527 testSortEventFired: function() {
528 var wasFired = false,
531 mc.on('sort', function() {
540 assert.isTrue(wasFired);