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.

556 lines
16 KiB

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