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.

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