[76] | 1 | /*! |
---|
| 2 | * Ext JS Library 3.4.0 |
---|
| 3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
| 4 | * licensing@sencha.com |
---|
| 5 | * http://www.sencha.com/license |
---|
| 6 | */ |
---|
| 7 | (function() { |
---|
| 8 | var suite = Ext.test.session.getSuite('Ext.util.MixedCollection'), |
---|
| 9 | assert = Y.Assert; |
---|
| 10 | |
---|
| 11 | suite.add(new Y.Test.Case({ |
---|
| 12 | name: 'constructor', |
---|
| 13 | |
---|
| 14 | setUp: function() { |
---|
| 15 | this.mc = new Ext.util.MixedCollection(); |
---|
| 16 | }, |
---|
| 17 | |
---|
| 18 | tearDown: function() { |
---|
| 19 | this.mc.clear(); |
---|
| 20 | }, |
---|
| 21 | |
---|
| 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'}; |
---|
| 26 | |
---|
| 27 | this.mc.add(item1); |
---|
| 28 | this.mc.add(item2); |
---|
| 29 | |
---|
| 30 | assert.areSame(item1, this.mc.get(1)); |
---|
| 31 | assert.areSame(item2, this.mc.get(2)); |
---|
| 32 | }, |
---|
| 33 | |
---|
| 34 | //test that we can provide a getKey implementation |
---|
| 35 | testCanSetGetKey: function() { |
---|
| 36 | var collection = new Ext.util.MixedCollection(false, function(item) { |
---|
| 37 | return item.myKey; |
---|
| 38 | }); |
---|
| 39 | |
---|
| 40 | var item1 = {myKey: 'a', data: 'first item' }, |
---|
| 41 | item2 = {myKey: 'b', data: 'second item'}; |
---|
| 42 | |
---|
| 43 | collection.add(item1); |
---|
| 44 | collection.add(item2); |
---|
| 45 | |
---|
| 46 | assert.areSame(item2, collection.get('b')); |
---|
| 47 | assert.areSame(item1, collection.get('a')); |
---|
| 48 | } |
---|
| 49 | })); |
---|
| 50 | |
---|
| 51 | suite.add(new Y.Test.Case({ |
---|
| 52 | name: 'iterators', |
---|
| 53 | |
---|
| 54 | setUp: function() { |
---|
| 55 | this.mc = new Ext.util.MixedCollection(); |
---|
| 56 | |
---|
| 57 | this.mc.addAll([ |
---|
| 58 | {id: 1, name: 'first'}, |
---|
| 59 | {id: 2, name: 'second'}, |
---|
| 60 | {id: 3, name: 'third'} |
---|
| 61 | ]); |
---|
| 62 | }, |
---|
| 63 | |
---|
| 64 | testEach: function() { |
---|
| 65 | var callCount = 0, callScope, total; |
---|
| 66 | |
---|
| 67 | this.mc.each(function(item, index, length) { |
---|
| 68 | //make sure that the function is called in the correct scope |
---|
| 69 | callScope = this; |
---|
| 70 | callCount ++; |
---|
| 71 | total = length; |
---|
| 72 | }, this); |
---|
| 73 | |
---|
| 74 | assert.areEqual(this, callScope); |
---|
| 75 | assert.areEqual(3, callCount); |
---|
| 76 | assert.areEqual(3, total); |
---|
| 77 | }, |
---|
| 78 | |
---|
| 79 | testEachKey: function() { |
---|
| 80 | var callCount = 0, callScope; |
---|
| 81 | |
---|
| 82 | this.mc.eachKey(function(key, index, length) { |
---|
| 83 | //make sure that the function is called in the correct scope |
---|
| 84 | callScope = this; |
---|
| 85 | callCount ++; |
---|
| 86 | }, this); |
---|
| 87 | |
---|
| 88 | assert.areEqual(this, callScope); |
---|
| 89 | assert.areEqual(3, callCount); |
---|
| 90 | } |
---|
| 91 | })); |
---|
| 92 | |
---|
| 93 | suite.add(new Y.Test.Case({ |
---|
| 94 | name: 'add and remove', |
---|
| 95 | |
---|
| 96 | setUp: function() { |
---|
| 97 | this.mc = new Ext.util.MixedCollection(); |
---|
| 98 | }, |
---|
| 99 | |
---|
| 100 | testAddAll: function() { |
---|
| 101 | var mc = this.mc; |
---|
| 102 | |
---|
| 103 | assert.areEqual(0, mc.length); |
---|
| 104 | |
---|
| 105 | mc.addAll([{id: 1}, {id: 2}, {id: 3}]); |
---|
| 106 | |
---|
| 107 | assert.areEqual(3, mc.length); |
---|
| 108 | }, |
---|
| 109 | |
---|
| 110 | testAddAndClear: function() { |
---|
| 111 | var mc = this.mc; |
---|
| 112 | |
---|
| 113 | mc.add({id: 1}); |
---|
| 114 | mc.add({id: 2}); |
---|
| 115 | mc.add({id: 3}); |
---|
| 116 | |
---|
| 117 | assert.areEqual(3, mc.length); |
---|
| 118 | |
---|
| 119 | mc.clear(); |
---|
| 120 | assert.areEqual(0, mc.length); |
---|
| 121 | }, |
---|
| 122 | |
---|
| 123 | testAddEventFired: function() { |
---|
| 124 | var mc = this.mc, |
---|
| 125 | fired = false; |
---|
| 126 | |
---|
| 127 | mc.on('add', function() {fired = true;}); |
---|
| 128 | |
---|
| 129 | mc.add({id: 1}); |
---|
| 130 | assert.isTrue(fired); |
---|
| 131 | }, |
---|
| 132 | |
---|
| 133 | testClearEventFired: function() { |
---|
| 134 | var mc = this.mc, |
---|
| 135 | fired = false; |
---|
| 136 | |
---|
| 137 | mc.on('clear', function() {fired = true;}, this); |
---|
| 138 | mc.clear(); |
---|
| 139 | |
---|
| 140 | assert.isTrue(fired); |
---|
| 141 | }, |
---|
| 142 | |
---|
| 143 | testGetCount: function() { |
---|
| 144 | this.mc.add({id: 1}); |
---|
| 145 | this.mc.add({id: 2}); |
---|
| 146 | this.mc.add({id: 3}); |
---|
| 147 | |
---|
| 148 | assert.areEqual(3, this.mc.getCount()); |
---|
| 149 | }, |
---|
| 150 | |
---|
| 151 | testRemove: function() { |
---|
| 152 | |
---|
| 153 | }, |
---|
| 154 | |
---|
| 155 | testRemoveFiresEvent: function() { |
---|
| 156 | |
---|
| 157 | } |
---|
| 158 | })); |
---|
| 159 | |
---|
| 160 | suite.add(new Y.Test.Case({ |
---|
| 161 | name: 'insert', |
---|
| 162 | |
---|
| 163 | setUp: function() { |
---|
| 164 | this.mc = new Ext.util.MixedCollection(); |
---|
| 165 | |
---|
| 166 | this.mc.addAll([ |
---|
| 167 | {id: 1, name: 'first'}, |
---|
| 168 | {id: 2, name: 'second'}, |
---|
| 169 | {id: 3, name: 'third'} |
---|
| 170 | ]); |
---|
| 171 | }, |
---|
| 172 | |
---|
| 173 | doInsert: function() { |
---|
| 174 | this.mc.insert(1, {id: 4, name: 'fourth'}); |
---|
| 175 | }, |
---|
| 176 | |
---|
| 177 | testInsertsToCorrectLocation: function() { |
---|
| 178 | this.doInsert(); |
---|
| 179 | |
---|
| 180 | assert.areEqual(4, this.mc.itemAt(1).id); |
---|
| 181 | }, |
---|
| 182 | |
---|
| 183 | testOtherItemsPreserved: function() { |
---|
| 184 | var prevCount = this.mc.getCount(); |
---|
| 185 | |
---|
| 186 | this.doInsert(); |
---|
| 187 | assert.areEqual(prevCount + 1, this.mc.getCount()); |
---|
| 188 | }, |
---|
| 189 | |
---|
| 190 | testFiresAddEvent: function() { |
---|
| 191 | var fired = false; |
---|
| 192 | |
---|
| 193 | this.mc.on('add', function() { fired = true; }); |
---|
| 194 | this.doInsert(); |
---|
| 195 | |
---|
| 196 | assert.isTrue(fired); |
---|
| 197 | } |
---|
| 198 | })); |
---|
| 199 | |
---|
| 200 | suite.add(new Y.Test.Case({ |
---|
| 201 | name: 'replace', |
---|
| 202 | |
---|
| 203 | setUp: function() { |
---|
| 204 | this.mc = new Ext.util.MixedCollection(); |
---|
| 205 | |
---|
| 206 | this.mc.addAll([ |
---|
| 207 | {id: 1, name: 'first'}, |
---|
| 208 | {id: 2, name: 'second'}, |
---|
| 209 | {id: 3, name: 'third'} |
---|
| 210 | ]); |
---|
| 211 | }, |
---|
| 212 | |
---|
| 213 | doReplace: function() { |
---|
| 214 | this.mc.replace(2, {id: 4, name: 'fourth'}); |
---|
| 215 | }, |
---|
| 216 | |
---|
| 217 | testReplacesCorrectItem: function() { |
---|
| 218 | this.doReplace(); |
---|
| 219 | assert.areEqual("fourth", this.mc.itemAt(1).name); |
---|
| 220 | }, |
---|
| 221 | |
---|
| 222 | testPreviousItemRemoved: function() { |
---|
| 223 | var prevCount = this.mc.getCount(); |
---|
| 224 | |
---|
| 225 | this.doReplace(); |
---|
| 226 | assert.areEqual(prevCount, this.mc.getCount()); |
---|
| 227 | }, |
---|
| 228 | |
---|
| 229 | testReplaceEventFired: function() { |
---|
| 230 | var fired = false; |
---|
| 231 | |
---|
| 232 | this.mc.on('replace', function() { fired = true; }); |
---|
| 233 | this.doReplace(); |
---|
| 234 | |
---|
| 235 | assert.isTrue(fired); |
---|
| 236 | } |
---|
| 237 | })); |
---|
| 238 | |
---|
| 239 | suite.add(new Y.Test.Case({ |
---|
| 240 | name: 'clone', |
---|
| 241 | |
---|
| 242 | setUp: function() { |
---|
| 243 | this.mc = new Ext.util.MixedCollection(); |
---|
| 244 | |
---|
| 245 | this.mc.addAll([ |
---|
| 246 | {id: 1, name: 'first'}, |
---|
| 247 | {id: 2, name: 'second'}, |
---|
| 248 | {id: 3, name: 'third'} |
---|
| 249 | ]); |
---|
| 250 | }, |
---|
| 251 | |
---|
| 252 | //test that a shallow clone is completed correctly |
---|
| 253 | testClone: function() { |
---|
| 254 | var newMC = this.mc.clone(); |
---|
| 255 | |
---|
| 256 | assert.areEqual(3, newMC.getCount()); |
---|
| 257 | |
---|
| 258 | Ext.each([1, 2, 3], function(id) { |
---|
| 259 | assert.areEqual(this.mc.get(id).id, newMC.get(id).id); |
---|
| 260 | }, this); |
---|
| 261 | } |
---|
| 262 | })); |
---|
| 263 | |
---|
| 264 | suite.add(new Y.Test.Case({ |
---|
| 265 | name: 'getting items', |
---|
| 266 | |
---|
| 267 | setUp: function() { |
---|
| 268 | this.mc = new Ext.util.MixedCollection(); |
---|
| 269 | this.item1 = {id: 1, name: 'first'}; |
---|
| 270 | |
---|
| 271 | this.mc.addAll([ |
---|
| 272 | this.item1, |
---|
| 273 | {id: 2, name: 'second'}, |
---|
| 274 | {id: 3, name: 'third'} |
---|
| 275 | ]); |
---|
| 276 | }, |
---|
| 277 | |
---|
| 278 | testFirst: function() { |
---|
| 279 | assert.areEqual(1, this.mc.first().id); |
---|
| 280 | }, |
---|
| 281 | |
---|
| 282 | testLast: function() { |
---|
| 283 | assert.areEqual(3, this.mc.last().id); |
---|
| 284 | }, |
---|
| 285 | |
---|
| 286 | testGet: function() { |
---|
| 287 | assert.areEqual(2, this.mc.get(2).id); |
---|
| 288 | }, |
---|
| 289 | |
---|
| 290 | testGetKey: function() { |
---|
| 291 | assert.areEqual(1, this.mc.getKey(this.item1)); |
---|
| 292 | }, |
---|
| 293 | |
---|
| 294 | //should return items in the given range |
---|
| 295 | testGetRange: function() { |
---|
| 296 | var items = this.mc.getRange(1, 2); |
---|
| 297 | |
---|
| 298 | assert.areEqual(2, items.length); |
---|
| 299 | assert.areEqual(2, items[0].id); |
---|
| 300 | assert.areEqual(3, items[1].id); |
---|
| 301 | }, |
---|
| 302 | |
---|
| 303 | //should get all items |
---|
| 304 | testGetRangeWithNoArguments: function() { |
---|
| 305 | var items = this.mc.getRange(); |
---|
| 306 | |
---|
| 307 | assert.areEqual(3, items.length); |
---|
| 308 | }, |
---|
| 309 | |
---|
| 310 | //should get all items after the provided start index |
---|
| 311 | testGetRangeWithNoEnd: function() { |
---|
| 312 | var items = this.mc.getRange(1); |
---|
| 313 | |
---|
| 314 | assert.areEqual(2, items.length); |
---|
| 315 | }, |
---|
| 316 | |
---|
| 317 | testIndexOf: function() { |
---|
| 318 | assert.areEqual(0, this.mc.indexOf(this.item1)); |
---|
| 319 | }, |
---|
| 320 | |
---|
| 321 | testIndexOfKey: function() { |
---|
| 322 | assert.areEqual(2, this.mc.indexOfKey(3)); |
---|
| 323 | }, |
---|
| 324 | |
---|
| 325 | testKey: function() { |
---|
| 326 | assert.areEqual(3, this.mc.key(3).id); |
---|
| 327 | }, |
---|
| 328 | |
---|
| 329 | testItemByIndex: function() { |
---|
| 330 | this.mc.add({id: 'a', name: 'another item'}); |
---|
| 331 | this.mc.add({id: 'b', name: 'yet another item'}); |
---|
| 332 | |
---|
| 333 | assert.areEqual('b', this.mc.item(4).id); |
---|
| 334 | }, |
---|
| 335 | |
---|
| 336 | //key should take priority over index |
---|
| 337 | testItemByKey: function() { |
---|
| 338 | this.mc.add({id: 'a', name: 'another item'}); |
---|
| 339 | |
---|
| 340 | assert.areEqual('a', this.mc.item('a').id); |
---|
| 341 | }, |
---|
| 342 | |
---|
| 343 | testItemAt: function() { |
---|
| 344 | assert.areEqual(3, this.mc.itemAt(2).id); |
---|
| 345 | } |
---|
| 346 | })); |
---|
| 347 | |
---|
| 348 | suite.add(new Y.Test.Case({ |
---|
| 349 | name: 'find functions', |
---|
| 350 | |
---|
| 351 | setUp: function() { |
---|
| 352 | this.mc = new Ext.util.MixedCollection(); |
---|
| 353 | |
---|
| 354 | this.mc.addAll([ |
---|
| 355 | {id: 1, name: 'first'}, |
---|
| 356 | {id: 2, name: 'second'}, |
---|
| 357 | {id: 3, name: 'third'} |
---|
| 358 | ]); |
---|
| 359 | }, |
---|
| 360 | |
---|
| 361 | testFind: function() { |
---|
| 362 | var matched = this.mc.find(function(item) { |
---|
| 363 | return item.name == 'third'; |
---|
| 364 | }); |
---|
| 365 | |
---|
| 366 | assert.areEqual('third', matched.name); |
---|
| 367 | }, |
---|
| 368 | |
---|
| 369 | testFindIndex: function() { |
---|
| 370 | var matched = this.mc.findIndex('name', 'third'); |
---|
| 371 | |
---|
| 372 | assert.areEqual(2, matched); |
---|
| 373 | }, |
---|
| 374 | |
---|
| 375 | testFindIndexBy: function() { |
---|
| 376 | var matched = this.mc.findIndexBy(function(item) { |
---|
| 377 | return item.name == 'second'; |
---|
| 378 | }); |
---|
| 379 | |
---|
| 380 | assert.areEqual(1, matched); |
---|
| 381 | } |
---|
| 382 | })); |
---|
| 383 | |
---|
| 384 | suite.add(new Y.Test.Case({ |
---|
| 385 | name: 'contains', |
---|
| 386 | |
---|
| 387 | setUp: function() { |
---|
| 388 | this.mc = new Ext.util.MixedCollection(); |
---|
| 389 | this.item = {id: 1, name: 'first'}; |
---|
| 390 | |
---|
| 391 | this.mc.addAll([ |
---|
| 392 | this.item, |
---|
| 393 | {id: 2, name: 'second'}, |
---|
| 394 | {id: 3, name: 'third'} |
---|
| 395 | ]); |
---|
| 396 | }, |
---|
| 397 | |
---|
| 398 | tearDown: function() { |
---|
| 399 | delete this.item; |
---|
| 400 | }, |
---|
| 401 | |
---|
| 402 | testContains: function() { |
---|
| 403 | assert.isTrue(this.mc.contains(this.item)); |
---|
| 404 | }, |
---|
| 405 | |
---|
| 406 | testDoesNotContain: function() { |
---|
| 407 | assert.isFalse(this.mc.contains({some: 'object'})); |
---|
| 408 | }, |
---|
| 409 | |
---|
| 410 | testContainsKey: function() { |
---|
| 411 | assert.isTrue(this.mc.containsKey(1)); |
---|
| 412 | }, |
---|
| 413 | |
---|
| 414 | testDoesNotContainKey: function() { |
---|
| 415 | assert.isFalse(this.mc.containsKey('abc')); |
---|
| 416 | } |
---|
| 417 | })); |
---|
| 418 | |
---|
| 419 | suite.add(new Y.Test.Case({ |
---|
| 420 | name: 'single sorting', |
---|
| 421 | |
---|
| 422 | setUp: function() { |
---|
| 423 | this.mc = new Ext.util.MixedCollection(false, function(item) { |
---|
| 424 | return item['code']; |
---|
| 425 | }); |
---|
| 426 | |
---|
| 427 | this.mc.addAll([ |
---|
| 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} |
---|
| 431 | ]); |
---|
| 432 | }, |
---|
| 433 | |
---|
| 434 | testKeySort: function() { |
---|
| 435 | var mc = this.mc; |
---|
| 436 | mc.keySort(); |
---|
| 437 | |
---|
| 438 | assert.areEqual('A', mc.itemAt(0).code); |
---|
| 439 | assert.areEqual('B', mc.itemAt(1).code); |
---|
| 440 | assert.areEqual('C', mc.itemAt(2).code); |
---|
| 441 | }, |
---|
| 442 | |
---|
| 443 | testDirectionalKeySort: function() { |
---|
| 444 | var mc = this.mc; |
---|
| 445 | mc.keySort('DESC'); |
---|
| 446 | |
---|
| 447 | assert.areEqual('C', mc.itemAt(0).code); |
---|
| 448 | assert.areEqual('B', mc.itemAt(1).code); |
---|
| 449 | assert.areEqual('A', mc.itemAt(2).code); |
---|
| 450 | }, |
---|
| 451 | |
---|
| 452 | testSort: function() { |
---|
| 453 | var mc = new Ext.util.MixedCollection(); |
---|
| 454 | mc.addAll(3, 1, 4, 2); |
---|
| 455 | mc.sort(); |
---|
| 456 | |
---|
| 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)); |
---|
| 461 | }, |
---|
| 462 | |
---|
| 463 | testDirectionalSort: function() { |
---|
| 464 | |
---|
| 465 | }, |
---|
| 466 | |
---|
| 467 | testSortWithComparator: function() { |
---|
| 468 | var mc = this.mc; |
---|
| 469 | mc.sort('ASC', function(a, b) { |
---|
| 470 | return (a.id * a.modifier) - (b.id * b.modifier); |
---|
| 471 | }); |
---|
| 472 | |
---|
| 473 | assert.areEqual('C', mc.itemAt(0).code); |
---|
| 474 | assert.areEqual('B', mc.itemAt(1).code); |
---|
| 475 | assert.areEqual('A', mc.itemAt(2).code); |
---|
| 476 | }, |
---|
| 477 | |
---|
| 478 | testDirectionalSortWithComparator: function() { |
---|
| 479 | var mc = this.mc; |
---|
| 480 | mc.sort('DESC', function(a, b) { |
---|
| 481 | return (a.id * a.modifier) - (b.id * b.modifier); |
---|
| 482 | }); |
---|
| 483 | |
---|
| 484 | assert.areEqual('A', mc.itemAt(0).code); |
---|
| 485 | assert.areEqual('B', mc.itemAt(1).code); |
---|
| 486 | assert.areEqual('C', mc.itemAt(2).code); |
---|
| 487 | }, |
---|
| 488 | |
---|
| 489 | testSortEventFired: function() { |
---|
| 490 | var fired = false; |
---|
| 491 | |
---|
| 492 | this.mc.on('sort', function() { fired = true; }); |
---|
| 493 | this.mc.sort('name'); |
---|
| 494 | |
---|
| 495 | assert.isTrue(fired); |
---|
| 496 | } |
---|
| 497 | })); |
---|
| 498 | |
---|
| 499 | suite.add(new Y.Test.Case({ |
---|
| 500 | name: 'reordering', |
---|
| 501 | |
---|
| 502 | setUp: function() { |
---|
| 503 | this.mc = new Ext.util.MixedCollection(false, function(item) { |
---|
| 504 | return item['code']; |
---|
| 505 | }); |
---|
| 506 | |
---|
| 507 | this.mc.addAll([ |
---|
| 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} |
---|
| 511 | ]); |
---|
| 512 | }, |
---|
| 513 | |
---|
| 514 | testReordering: function() { |
---|
| 515 | var mc = this.mc; |
---|
| 516 | |
---|
| 517 | mc.reorder({ |
---|
| 518 | 1: 2, |
---|
| 519 | 2: 0 |
---|
| 520 | }); |
---|
| 521 | |
---|
| 522 | assert.areEqual('B', mc.itemAt(0).code); |
---|
| 523 | assert.areEqual('C', mc.itemAt(1).code); |
---|
| 524 | assert.areEqual('A', mc.itemAt(2).code); |
---|
| 525 | }, |
---|
| 526 | |
---|
| 527 | testSortEventFired: function() { |
---|
| 528 | var wasFired = false, |
---|
| 529 | mc = this.mc; |
---|
| 530 | |
---|
| 531 | mc.on('sort', function() { |
---|
| 532 | wasFired = true; |
---|
| 533 | }, this); |
---|
| 534 | |
---|
| 535 | mc.reorder({ |
---|
| 536 | 1: 2, |
---|
| 537 | 2: 0 |
---|
| 538 | }); |
---|
| 539 | |
---|
| 540 | assert.isTrue(wasFired); |
---|
| 541 | } |
---|
| 542 | })); |
---|
| 543 | })(); |
---|