You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

542 lines
15 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*!
  2. * Ext JS Library 3.3.1
  3. * Copyright(c) 2006-2010 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. suite.add(new Y.Test.Case({
  11. name: 'constructor',
  12. setUp: function() {
  13. this.mc = new Ext.util.MixedCollection();
  14. },
  15. tearDown: function() {
  16. this.mc.clear();
  17. },
  18. //test that a default getKey implementation is set
  19. testHasDefaultGetKey: function() {
  20. var item1 = {id: 1, data: 'first item' },
  21. item2 = {id: 2, data: 'second item'};
  22. this.mc.add(item1);
  23. this.mc.add(item2);
  24. assert.areSame(item1, this.mc.get(1));
  25. assert.areSame(item2, this.mc.get(2));
  26. },
  27. //test that we can provide a getKey implementation
  28. testCanSetGetKey: function() {
  29. var collection = new Ext.util.MixedCollection(false, function(item) {
  30. return item.myKey;
  31. });
  32. var item1 = {myKey: 'a', data: 'first item' },
  33. item2 = {myKey: 'b', data: 'second item'};
  34. collection.add(item1);
  35. collection.add(item2);
  36. assert.areSame(item2, collection.get('b'));
  37. assert.areSame(item1, collection.get('a'));
  38. }
  39. }));
  40. suite.add(new Y.Test.Case({
  41. name: 'iterators',
  42. setUp: function() {
  43. this.mc = new Ext.util.MixedCollection();
  44. this.mc.addAll([
  45. {id: 1, name: 'first'},
  46. {id: 2, name: 'second'},
  47. {id: 3, name: 'third'}
  48. ]);
  49. },
  50. testEach: function() {
  51. var callCount = 0, callScope, total;
  52. this.mc.each(function(item, index, length) {
  53. //make sure that the function is called in the correct scope
  54. callScope = this;
  55. callCount ++;
  56. total = length;
  57. }, this);
  58. assert.areEqual(this, callScope);
  59. assert.areEqual(3, callCount);
  60. assert.areEqual(3, total);
  61. },
  62. testEachKey: function() {
  63. var callCount = 0, callScope;
  64. this.mc.eachKey(function(key, index, length) {
  65. //make sure that the function is called in the correct scope
  66. callScope = this;
  67. callCount ++;
  68. }, this);
  69. assert.areEqual(this, callScope);
  70. assert.areEqual(3, callCount);
  71. }
  72. }));
  73. suite.add(new Y.Test.Case({
  74. name: 'add and remove',
  75. setUp: function() {
  76. this.mc = new Ext.util.MixedCollection();
  77. },
  78. testAddAll: function() {
  79. var mc = this.mc;
  80. assert.areEqual(0, mc.length);
  81. mc.addAll([{id: 1}, {id: 2}, {id: 3}]);
  82. assert.areEqual(3, mc.length);
  83. },
  84. testAddAndClear: function() {
  85. var mc = this.mc;
  86. mc.add({id: 1});
  87. mc.add({id: 2});
  88. mc.add({id: 3});
  89. assert.areEqual(3, mc.length);
  90. mc.clear();
  91. assert.areEqual(0, mc.length);
  92. },
  93. testAddEventFired: function() {
  94. var mc = this.mc,
  95. fired = false;
  96. mc.on('add', function() {fired = true;});
  97. mc.add({id: 1});
  98. assert.isTrue(fired);
  99. },
  100. testClearEventFired: function() {
  101. var mc = this.mc,
  102. fired = false;
  103. mc.on('clear', function() {fired = true;}, this);
  104. mc.clear();
  105. assert.isTrue(fired);
  106. },
  107. testGetCount: function() {
  108. this.mc.add({id: 1});
  109. this.mc.add({id: 2});
  110. this.mc.add({id: 3});
  111. assert.areEqual(3, this.mc.getCount());
  112. },
  113. testRemove: function() {
  114. },
  115. testRemoveFiresEvent: function() {
  116. }
  117. }));
  118. suite.add(new Y.Test.Case({
  119. name: 'insert',
  120. setUp: function() {
  121. this.mc = new Ext.util.MixedCollection();
  122. this.mc.addAll([
  123. {id: 1, name: 'first'},
  124. {id: 2, name: 'second'},
  125. {id: 3, name: 'third'}
  126. ]);
  127. },
  128. doInsert: function() {
  129. this.mc.insert(1, {id: 4, name: 'fourth'});
  130. },
  131. testInsertsToCorrectLocation: function() {
  132. this.doInsert();
  133. assert.areEqual(4, this.mc.itemAt(1).id);
  134. },
  135. testOtherItemsPreserved: function() {
  136. var prevCount = this.mc.getCount();
  137. this.doInsert();
  138. assert.areEqual(prevCount + 1, this.mc.getCount());
  139. },
  140. testFiresAddEvent: function() {
  141. var fired = false;
  142. this.mc.on('add', function() { fired = true; });
  143. this.doInsert();
  144. assert.isTrue(fired);
  145. }
  146. }));
  147. suite.add(new Y.Test.Case({
  148. name: 'replace',
  149. setUp: function() {
  150. this.mc = new Ext.util.MixedCollection();
  151. this.mc.addAll([
  152. {id: 1, name: 'first'},
  153. {id: 2, name: 'second'},
  154. {id: 3, name: 'third'}
  155. ]);
  156. },
  157. doReplace: function() {
  158. this.mc.replace(2, {id: 4, name: 'fourth'});
  159. },
  160. testReplacesCorrectItem: function() {
  161. this.doReplace();
  162. assert.areEqual("fourth", this.mc.itemAt(1).name);
  163. },
  164. testPreviousItemRemoved: function() {
  165. var prevCount = this.mc.getCount();
  166. this.doReplace();
  167. assert.areEqual(prevCount, this.mc.getCount());
  168. },
  169. testReplaceEventFired: function() {
  170. var fired = false;
  171. this.mc.on('replace', function() { fired = true; });
  172. this.doReplace();
  173. assert.isTrue(fired);
  174. }
  175. }));
  176. suite.add(new Y.Test.Case({
  177. name: 'clone',
  178. setUp: function() {
  179. this.mc = new Ext.util.MixedCollection();
  180. this.mc.addAll([
  181. {id: 1, name: 'first'},
  182. {id: 2, name: 'second'},
  183. {id: 3, name: 'third'}
  184. ]);
  185. },
  186. //test that a shallow clone is completed correctly
  187. testClone: function() {
  188. var newMC = this.mc.clone();
  189. assert.areEqual(3, newMC.getCount());
  190. Ext.each([1, 2, 3], function(id) {
  191. assert.areEqual(this.mc.get(id).id, newMC.get(id).id);
  192. }, this);
  193. }
  194. }));
  195. suite.add(new Y.Test.Case({
  196. name: 'getting items',
  197. setUp: function() {
  198. this.mc = new Ext.util.MixedCollection();
  199. this.item1 = {id: 1, name: 'first'};
  200. this.mc.addAll([
  201. this.item1,
  202. {id: 2, name: 'second'},
  203. {id: 3, name: 'third'}
  204. ]);
  205. },
  206. testFirst: function() {
  207. assert.areEqual(1, this.mc.first().id);
  208. },
  209. testLast: function() {
  210. assert.areEqual(3, this.mc.last().id);
  211. },
  212. testGet: function() {
  213. assert.areEqual(2, this.mc.get(2).id);
  214. },
  215. testGetKey: function() {
  216. assert.areEqual(1, this.mc.getKey(this.item1));
  217. },
  218. //should return items in the given range
  219. testGetRange: function() {
  220. var items = this.mc.getRange(1, 2);
  221. assert.areEqual(2, items.length);
  222. assert.areEqual(2, items[0].id);
  223. assert.areEqual(3, items[1].id);
  224. },
  225. //should get all items
  226. testGetRangeWithNoArguments: function() {
  227. var items = this.mc.getRange();
  228. assert.areEqual(3, items.length);
  229. },
  230. //should get all items after the provided start index
  231. testGetRangeWithNoEnd: function() {
  232. var items = this.mc.getRange(1);
  233. assert.areEqual(2, items.length);
  234. },
  235. testIndexOf: function() {
  236. assert.areEqual(0, this.mc.indexOf(this.item1));
  237. },
  238. testIndexOfKey: function() {
  239. assert.areEqual(2, this.mc.indexOfKey(3));
  240. },
  241. testKey: function() {
  242. assert.areEqual(3, this.mc.key(3).id);
  243. },
  244. testItemByIndex: function() {
  245. this.mc.add({id: 'a', name: 'another item'});
  246. this.mc.add({id: 'b', name: 'yet another item'});
  247. assert.areEqual('b', this.mc.item(4).id);
  248. },
  249. //key should take priority over index
  250. testItemByKey: function() {
  251. this.mc.add({id: 'a', name: 'another item'});
  252. assert.areEqual('a', this.mc.item('a').id);
  253. },
  254. testItemAt: function() {
  255. assert.areEqual(3, this.mc.itemAt(2).id);
  256. }
  257. }));
  258. suite.add(new Y.Test.Case({
  259. name: 'find functions',
  260. setUp: function() {
  261. this.mc = new Ext.util.MixedCollection();
  262. this.mc.addAll([
  263. {id: 1, name: 'first'},
  264. {id: 2, name: 'second'},
  265. {id: 3, name: 'third'}
  266. ]);
  267. },
  268. testFind: function() {
  269. var matched = this.mc.find(function(item) {
  270. return item.name == 'third';
  271. });
  272. assert.areEqual('third', matched.name);
  273. },
  274. testFindIndex: function() {
  275. var matched = this.mc.findIndex('name', 'third');
  276. assert.areEqual(2, matched);
  277. },
  278. testFindIndexBy: function() {
  279. var matched = this.mc.findIndexBy(function(item) {
  280. return item.name == 'second';
  281. });
  282. assert.areEqual(1, matched);
  283. }
  284. }));
  285. suite.add(new Y.Test.Case({
  286. name: 'contains',
  287. setUp: function() {
  288. this.mc = new Ext.util.MixedCollection();
  289. this.item = {id: 1, name: 'first'};
  290. this.mc.addAll([
  291. this.item,
  292. {id: 2, name: 'second'},
  293. {id: 3, name: 'third'}
  294. ]);
  295. },
  296. tearDown: function() {
  297. delete this.item;
  298. },
  299. testContains: function() {
  300. assert.isTrue(this.mc.contains(this.item));
  301. },
  302. testDoesNotContain: function() {
  303. assert.isFalse(this.mc.contains({some: 'object'}));
  304. },
  305. testContainsKey: function() {
  306. assert.isTrue(this.mc.containsKey(1));
  307. },
  308. testDoesNotContainKey: function() {
  309. assert.isFalse(this.mc.containsKey('abc'));
  310. }
  311. }));
  312. suite.add(new Y.Test.Case({
  313. name: 'single sorting',
  314. setUp: function() {
  315. this.mc = new Ext.util.MixedCollection(false, function(item) {
  316. return item['code'];
  317. });
  318. this.mc.addAll([
  319. {id: 1, name: 'first', code: 'C', modifier: 10},
  320. {id: 2, name: 'second', code: 'A', modifier: 100},
  321. {id: 3, name: 'third', code: 'B', modifier: 5}
  322. ]);
  323. },
  324. testKeySort: function() {
  325. var mc = this.mc;
  326. mc.keySort();
  327. assert.areEqual('A', mc.itemAt(0).code);
  328. assert.areEqual('B', mc.itemAt(1).code);
  329. assert.areEqual('C', mc.itemAt(2).code);
  330. },
  331. testDirectionalKeySort: function() {
  332. var mc = this.mc;
  333. mc.keySort('DESC');
  334. assert.areEqual('C', mc.itemAt(0).code);
  335. assert.areEqual('B', mc.itemAt(1).code);
  336. assert.areEqual('A', mc.itemAt(2).code);
  337. },
  338. testSort: function() {
  339. var mc = new Ext.util.MixedCollection();
  340. mc.addAll(3, 1, 4, 2);
  341. mc.sort();
  342. assert.areEqual(1, mc.itemAt(0));
  343. assert.areEqual(2, mc.itemAt(1));
  344. assert.areEqual(3, mc.itemAt(2));
  345. assert.areEqual(4, mc.itemAt(3));
  346. },
  347. testDirectionalSort: function() {
  348. },
  349. testSortWithComparator: function() {
  350. var mc = this.mc;
  351. mc.sort('ASC', function(a, b) {
  352. return (a.id * a.modifier) - (b.id * b.modifier);
  353. });
  354. assert.areEqual('C', mc.itemAt(0).code);
  355. assert.areEqual('B', mc.itemAt(1).code);
  356. assert.areEqual('A', mc.itemAt(2).code);
  357. },
  358. testDirectionalSortWithComparator: function() {
  359. var mc = this.mc;
  360. mc.sort('DESC', function(a, b) {
  361. return (a.id * a.modifier) - (b.id * b.modifier);
  362. });
  363. assert.areEqual('A', mc.itemAt(0).code);
  364. assert.areEqual('B', mc.itemAt(1).code);
  365. assert.areEqual('C', mc.itemAt(2).code);
  366. },
  367. testSortEventFired: function() {
  368. var fired = false;
  369. this.mc.on('sort', function() { fired = true; });
  370. this.mc.sort('name');
  371. assert.isTrue(fired);
  372. }
  373. }));
  374. suite.add(new Y.Test.Case({
  375. name: 'reordering',
  376. setUp: function() {
  377. this.mc = new Ext.util.MixedCollection(false, function(item) {
  378. return item['code'];
  379. });
  380. this.mc.addAll([
  381. {id: 1, name: 'first', code: 'C', modifier: 10},
  382. {id: 2, name: 'second', code: 'A', modifier: 100},
  383. {id: 3, name: 'third', code: 'B', modifier: 5}
  384. ]);
  385. },
  386. testReordering: function() {
  387. var mc = this.mc;
  388. mc.reorder({
  389. 1: 2,
  390. 2: 0
  391. });
  392. assert.areEqual('B', mc.itemAt(0).code);
  393. assert.areEqual('C', mc.itemAt(1).code);
  394. assert.areEqual('A', mc.itemAt(2).code);
  395. },
  396. testSortEventFired: function() {
  397. var wasFired = false,
  398. mc = this.mc;
  399. mc.on('sort', function() {
  400. wasFired = true;
  401. }, this);
  402. mc.reorder({
  403. 1: 2,
  404. 2: 0
  405. });
  406. assert.isTrue(wasFired);
  407. }
  408. }));
  409. })();