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.

588 lines
17 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. /**
  8. * Tests Ext.data.Store functionality
  9. * @author Ed Spencer
  10. */
  11. (function() {
  12. var suite = Ext.test.session.getSuite('Ext.data.Store'),
  13. assert = Y.Assert;
  14. //a shared setup function used by several of the suites
  15. var defaultSetup = function() {
  16. this.store = new Ext.data.Store({
  17. proxy : new Ext.data.MemoryProxy({}),
  18. reader: new Ext.data.ArrayReader({}, [
  19. {name: 'name', type: 'string'},
  20. {name: 'email', type: 'string'},
  21. {name: 'greatness', type: 'int'},
  22. {name: 'group', type: 'string'},
  23. {name: 'old', type: 'boolean'}
  24. ]),
  25. storeId: 'myStore',
  26. remoteSort: false
  27. });
  28. this.store.loadData([
  29. ['Ed Spencer', 'ed@extjs.com', 100, 'code', false],
  30. ['Abe Elias', 'abe@extjs.com', 70, 'admin', false],
  31. ['Aaron Conran', 'aaron@extjs.com', 5, 'admin', true],
  32. ['Tommy Maintz', 'tommy@extjs.com', -15, 'code', true]
  33. ]);
  34. };
  35. suite.add(new Y.Test.Case({
  36. name: 'local single sorting',
  37. setUp: defaultSetup,
  38. testSetDefaultSort: function() {
  39. this.store.setDefaultSort('name', 'DESC');
  40. var store = this.store,
  41. sortInfo = store.sortInfo;
  42. assert.areEqual('name', sortInfo.field);
  43. assert.areEqual('DESC', sortInfo.direction);
  44. assert.areEqual('DESC', store.sortToggle['name']);
  45. },
  46. testSetDefaultSortDefaultToASC: function() {
  47. this.store.setDefaultSort('email');
  48. var store = this.store,
  49. sortInfo = store.sortInfo;
  50. assert.areEqual('email', sortInfo.field);
  51. assert.areEqual('ASC', sortInfo['direction']);
  52. assert.areEqual('ASC', store.sortToggle['email']);
  53. },
  54. testSortByField: function() {
  55. var store = this.store;
  56. store.sort('name', 'ASC');
  57. assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
  58. assert.areEqual('Abe Elias', store.getAt(1).get('name'));
  59. assert.areEqual('Ed Spencer', store.getAt(2).get('name'));
  60. assert.areEqual('Tommy Maintz', store.getAt(3).get('name'));
  61. },
  62. testToggling: function() {
  63. var store = this.store;
  64. store.sort('name', 'ASC');
  65. //second call to sort toggles the direction
  66. store.sort('name');
  67. assert.areEqual('Aaron Conran', store.getAt(3).get('name'));
  68. assert.areEqual('Abe Elias', store.getAt(2).get('name'));
  69. assert.areEqual('Ed Spencer', store.getAt(1).get('name'));
  70. assert.areEqual('Tommy Maintz', store.getAt(0).get('name'));
  71. },
  72. testSetsHasMultiSort: function() {
  73. this.store.sort('name', 'ASC');
  74. assert.isFalse(this.store.hasMultiSort);
  75. },
  76. testEventFired: function() {
  77. var executed = false,
  78. store = this.store;
  79. store.on('datachanged', function() {
  80. executed = true;
  81. }, this);
  82. store.sort('name');
  83. assert.isTrue(executed);
  84. },
  85. testSavesSortInfo: function() {
  86. var store = this.store;
  87. store.sort('name', 'DESC');
  88. assert.areEqual('name', store.sortInfo.field);
  89. assert.areEqual('DESC', store.sortInfo.direction);
  90. },
  91. //if we tell store to sort on a non-existent field it should return false and do nothing
  92. testInvalidFieldIgnored: function() {
  93. var store = this.store;
  94. //first we'll sort by name to give some reference sorting
  95. store.sort('name', 'ASC');
  96. assert.isFalse(store.sort('someUnknownField'));
  97. //make sure the original sorting was preserved
  98. assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
  99. assert.areEqual('Abe Elias', store.getAt(1).get('name'));
  100. assert.areEqual('Ed Spencer', store.getAt(2).get('name'));
  101. assert.areEqual('Tommy Maintz', store.getAt(3).get('name'));
  102. }
  103. }));
  104. suite.add(new Y.Test.Case({
  105. name: 'local multiple sorting',
  106. setUp: function() {
  107. defaultSetup.call(this);
  108. this.sorters = [
  109. {
  110. field : 'group',
  111. direction: 'ASC'
  112. },
  113. {
  114. field : 'old',
  115. direction: 'DESC'
  116. }
  117. ];
  118. },
  119. testSetsHasMultiSort: function() {
  120. this.store.sort(this.sorters);
  121. assert.isTrue(this.store.hasMultiSort);
  122. },
  123. testMultipleSorters: function() {
  124. var store = this.store,
  125. sorters = this.sorters;
  126. store.sort(sorters);
  127. assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
  128. assert.areEqual('Abe Elias', store.getAt(1).get('name'));
  129. assert.areEqual('Tommy Maintz', store.getAt(2).get('name'));
  130. assert.areEqual('Ed Spencer', store.getAt(3).get('name'));
  131. }
  132. // testMultipleSorterToggling: function() {
  133. // var store = this.store,
  134. // sorters = this.sorters;
  135. //
  136. // //first we'll sort to give some reference sorting
  137. // store.sort(sorters, "ASC");
  138. //
  139. // //second call to sort toggles direction
  140. // store.sort(sorters);
  141. //
  142. // assert.areEqual('Aaron Conran', store.getAt(3).get('name'));
  143. // assert.areEqual('Abe Elias', store.getAt(2).get('name'));
  144. // assert.areEqual('Tommy Maintz', store.getAt(1).get('name'));
  145. // assert.areEqual('Ed Spencer', store.getAt(0).get('name'));
  146. // }
  147. }));
  148. suite.add(new Y.Test.Case({
  149. name: 'single filtering',
  150. setUp: defaultSetup,
  151. testFilterByField: function() {
  152. var store = this.store;
  153. store.filter('group', 'code');
  154. assert.areEqual(2, store.getCount());
  155. },
  156. testFilterByFieldAnyMatch: function() {
  157. var store = this.store;
  158. store.filter('email', 'extjs', true);
  159. assert.areEqual(4, store.getCount());
  160. },
  161. testFilterByFieldCaseSensitive: function() {
  162. var store = this.store;
  163. store.filter('group', 'Code', false, true);
  164. assert.areEqual(0, store.getCount());
  165. },
  166. testFilterByFieldExactMatch: function() {
  167. var store = this.store;
  168. store.filter('email', 'aaron', false, false, true);
  169. assert.areEqual(0, store.getCount());
  170. },
  171. testClearFilter: function() {
  172. var store = this.store,
  173. executed = false;
  174. store.on('datachanged', function() {
  175. executed = true;
  176. }, this);
  177. store.filter('group', 'code');
  178. assert.areEqual(2, store.getCount());
  179. store.clearFilter();
  180. assert.areEqual(4, store.getCount());
  181. assert.isTrue(executed);
  182. },
  183. testSuppressClearFilterEvent: function() {
  184. var store = this.store,
  185. executed = false;
  186. store.filter('group', 'email');
  187. store.clearFilter(true);
  188. store.on('datachanged', function() {
  189. executed = true;
  190. }, this);
  191. assert.isFalse(executed);
  192. },
  193. testIsFiltered: function() {
  194. var store = this.store;
  195. assert.isFalse(store.isFiltered());
  196. store.filter('group', 'code');
  197. assert.isTrue(store.isFiltered());
  198. },
  199. testFilterByFunction: function() {
  200. var store = this.store,
  201. execScope,
  202. executed;
  203. store.on('datachanged', function() {
  204. executed = true;
  205. }, this);
  206. var filterFn = function(item) {
  207. execScope = this;
  208. return item.get('greatness') > 50;
  209. };
  210. store.filterBy(filterFn, this);
  211. assert.areEqual(this, execScope);
  212. assert.areEqual(2, store.getCount());
  213. assert.isTrue(executed);
  214. }
  215. }));
  216. suite.add(new Y.Test.Case({
  217. name : 'filtering more than once',
  218. setUp: defaultSetup,
  219. testFirstFilterIsCleared: function() {
  220. var store = this.store;
  221. store.filter('group', 'code');
  222. assert.areEqual(2, store.getCount());
  223. store.filter('old', false);
  224. //if filter had not been reset first, count would be 1
  225. assert.areEqual(2, store.getCount());
  226. }
  227. }));
  228. suite.add(new Y.Test.Case({
  229. name : 'multiple filters',
  230. setUp: function() {
  231. defaultSetup.call(this);
  232. this.filters = [
  233. {
  234. property: 'group',
  235. value : 'code'
  236. },
  237. {
  238. property: 'old',
  239. value : true
  240. }
  241. ];
  242. },
  243. testMultipleBasicFilters: function() {
  244. this.store.filter(this.filters);
  245. //applying the filter set above shoule yield one result
  246. assert.areEqual(1, this.store.getCount());
  247. assert.areEqual('Tommy Maintz', this.store.data.first().get('name'));
  248. },
  249. testMultiFiltersFiresDataChanged: function() {
  250. var executed = false;
  251. this.store.on('datachanged', function() {
  252. executed = true;
  253. }, this);
  254. this.store.filter(this.filters);
  255. assert.isTrue(executed);
  256. },
  257. //tests that the anyMatch and caseSensitive defaults are correctly applied
  258. testMultiFilterDefaults: function() {
  259. //PENDING
  260. },
  261. //tests a single custom filter
  262. testCustomFilter: function() {
  263. var execScope;
  264. //tests that the passed filter function is called
  265. //tests that the filter is called in the right scope
  266. this.store.filter({
  267. fn: function(record) {
  268. execScope = this;
  269. return record.get('group') == 'admin' && record.get('old') === true;
  270. },
  271. scope: this
  272. });
  273. assert.areEqual(this, execScope);
  274. assert.areEqual(1, this.store.getCount());
  275. assert.areEqual('Aaron Conran', this.store.data.first().get('name'));
  276. },
  277. //tests multiple filters where we pass in custom matcher functions
  278. testMultiCustomFilters: function() {
  279. this.store.filter([
  280. {
  281. fn: function(record) {
  282. return record.get('group') == 'admin';
  283. }
  284. },
  285. {
  286. fn: function(record) {
  287. return record.get('old') === false;
  288. }
  289. }
  290. ]);
  291. assert.areEqual(1, this.store.getCount());
  292. assert.areEqual('Abe Elias', this.store.data.first().get('name'));
  293. },
  294. testBasicAndCustomFilters: function() {
  295. //should return a single result - Ed Spencer
  296. this.store.filter([
  297. {
  298. fn: function(record) {
  299. return record.get('old') === false;
  300. }
  301. },
  302. {
  303. property: 'group',
  304. value : 'code'
  305. }
  306. ]);
  307. assert.areEqual(1, this.store.getCount());
  308. assert.areEqual('Ed Spencer', this.store.data.first().get('name'));
  309. },
  310. testClearMultipleFilters: function() {
  311. this.store.filter(this.filters);
  312. assert.areEqual(1, this.store.getCount());
  313. //make sure that clearing multiple filters is still correct
  314. this.store.clearFilter();
  315. assert.areEqual(4, this.store.getCount());
  316. }
  317. }));
  318. suite.add(new Y.Test.Case({
  319. name: 'inserting and removing',
  320. testInsert: function() {
  321. //PENDING
  322. },
  323. testInsertFiresAddEvent: function() {
  324. //PENDING
  325. },
  326. testRemove: function() {
  327. //PENDING
  328. },
  329. testRemoveAll: function() {
  330. //PENDING
  331. },
  332. testRemoveAt: function() {
  333. //PENDING
  334. }
  335. }));
  336. suite.add(new Y.Test.Case({
  337. name: 'destroying',
  338. setUp: defaultSetup,
  339. //we hijack Ext.StoreMgr.unregister temporarily and then set it back in this test case
  340. testUnregistersFromStore: function() {
  341. var executed = false,
  342. arg = undefined,
  343. unregister = Ext.StoreMgr.unregister;
  344. Ext.StoreMgr.unregister = function(store) {
  345. executed = true;
  346. arg = store;
  347. };
  348. this.store.destroy();
  349. assert.isTrue(executed);
  350. assert.areEqual(this.store, arg);
  351. Ext.StoreMgr.unregister = unregister;
  352. },
  353. testClearsData: function() {
  354. var executed = false;
  355. this.store.clearData = function() {
  356. executed = true;
  357. };
  358. this.store.destroy();
  359. assert.isTrue(executed);
  360. },
  361. testDestroysProxy: function() {
  362. var executed = false,
  363. arg = undefined,
  364. proxy = this.store.proxy,
  365. destroy = Ext.destroy;
  366. Ext.destroy = function(store) {
  367. executed = true;
  368. arg = store;
  369. };
  370. this.store.destroy();
  371. assert.isTrue(executed);
  372. assert.areEqual(proxy, arg);
  373. Ext.destroy = destroy;
  374. },
  375. testUnsetsReferences: function() {
  376. this.store.destroy();
  377. assert.isNull(this.store.reader);
  378. assert.isNull(this.store.writer);
  379. assert.isNull(this.store.data);
  380. },
  381. testIsDestroyed: function() {
  382. assert.isFalse(this.store.isDestroyed);
  383. this.store.destroy();
  384. assert.isTrue(this.store.isDestroyed);
  385. }
  386. }));
  387. suite.add(new Y.Test.Case({
  388. name: 'counting and iterators',
  389. setUp: defaultSetup,
  390. testGetCount: function() {
  391. assert.areEqual(4, this.store.getCount());
  392. },
  393. testGetTotalCount: function() {
  394. //PENDING - need a fake remote data set test with paging
  395. },
  396. testEach: function() {
  397. var count = 0,
  398. callScope;
  399. this.store.each(function() {
  400. callScope = this;
  401. count ++;
  402. }, this);
  403. assert.areEqual(4, count);
  404. assert.areEqual(this, callScope);
  405. },
  406. testSum: function() {
  407. var sum = this.store.sum('greatness');
  408. assert.areEqual(160, sum);
  409. },
  410. testSumWithStartAndEnd: function() {
  411. var sum = this.store.sum('greatness', 1, 3);
  412. assert.areEqual(60, sum);
  413. },
  414. //a normal collect test - check that we're pulling the right values out of the store
  415. testCollect: function() {
  416. var values = this.store.collect('name');
  417. assert.areEqual(4, values.length);
  418. assert.areNotEqual(-1, values.indexOf("Ed Spencer"));
  419. assert.areNotEqual(-1, values.indexOf("Abe Elias"));
  420. assert.areNotEqual(-1, values.indexOf("Aaron Conran"));
  421. assert.areNotEqual(-1, values.indexOf("Tommy Maintz"));
  422. },
  423. //checks that all collected values are unique
  424. testCollectIsUnique: function() {
  425. var values = this.store.collect('group');
  426. assert.areEqual(2, values.length);
  427. assert.areNotEqual(-1, values.indexOf("code"));
  428. assert.areNotEqual(-1, values.indexOf("admin"));
  429. }
  430. }));
  431. suite.add(new Y.Test.Case({
  432. name: 'committing',
  433. testCommitChanges: function() {
  434. //PENDING
  435. },
  436. testRejectChanges: function() {
  437. //PENDING
  438. },
  439. testAfterCommit: function() {
  440. //PENDING
  441. },
  442. testAfterReject: function() {
  443. //PENDING
  444. }
  445. }));
  446. suite.add(new Y.Test.Case({
  447. name: 'modified',
  448. testGetModifiedRecords: function() {
  449. //PENDING
  450. }
  451. }));
  452. suite.add(new Y.Test.Case({
  453. name: 'loading'
  454. }));
  455. })();