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.

400 lines
13 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.GroupingStore'),
  21. assert = Y.Assert;
  22. //a shared setup function used by several of the suites
  23. var defaultSetup = function(config) {
  24. config = config || {};
  25. Ext.applyIf(config, {
  26. proxy : new Ext.data.MemoryProxy({}),
  27. reader: new Ext.data.ArrayReader({}, [
  28. {name: 'name', type: 'string'},
  29. {name: 'email', type: 'string'},
  30. {name: 'greatness', type: 'int'},
  31. {name: 'group', type: 'string'},
  32. {name: 'old', type: 'boolean'}
  33. ]),
  34. storeId: 'myStore',
  35. remoteSort : false,
  36. remoteGroup: false,
  37. groupField : 'group',
  38. sortInfo: {field: 'name', direction: 'ASC'}
  39. });
  40. var store = new Ext.data.GroupingStore(config);
  41. store.loadData([
  42. ['Ed Spencer', 'ed@extjs.com', 100, 'code', false],
  43. ['Abe Elias', 'abe@extjs.com', 70, 'admin', false],
  44. ['Aaron Conran', 'aaron@extjs.com', 5, 'admin', true],
  45. ['Tommy Maintz', 'tommy@extjs.com', -15, 'code', true]
  46. ]);
  47. return store;
  48. };
  49. suite.add(new Y.Test.Case({
  50. name: 'constructor',
  51. testAppliesGroupField: function() {
  52. var GroupingStore = Ext.data.GroupingStore,
  53. proto = GroupingStore.prototype,
  54. oldFunc = proto.applyGroupField,
  55. wasCalled = false;
  56. proto.applyGroupField = function() {
  57. wasCalled = true;
  58. };
  59. var store = new GroupingStore();
  60. assert.isTrue(wasCalled);
  61. proto.applyGroupField = oldFunc;
  62. }
  63. }));
  64. suite.add(new Y.Test.Case({
  65. name: 'clear grouping',
  66. testUnsetsGroupField: function() {
  67. var store = defaultSetup();
  68. store.groupField = 'abc';
  69. store.clearGrouping();
  70. assert.isFalse(store.groupField);
  71. },
  72. testLocalGroupingAppliesSort: function() {
  73. var GroupingStore = Ext.data.GroupingStore,
  74. proto = GroupingStore.prototype,
  75. oldFunc = proto.sort,
  76. wasCalled = false;
  77. proto.sort = function() {
  78. wasCalled = true;
  79. };
  80. var store = defaultSetup({remoteGroup: false});
  81. store.clearGrouping();
  82. assert.isTrue(wasCalled);
  83. proto.sort = oldFunc;
  84. },
  85. testLocalGroupingFiresEvent: function() {
  86. var store = defaultSetup({remoteGroup: false}),
  87. fired = false;
  88. store.on('datachanged', function() {
  89. fired = true;
  90. }, this);
  91. store.clearGrouping();
  92. assert.isTrue(fired);
  93. },
  94. testRemoteGroupingReloads: function() {
  95. var GroupingStore = Ext.data.GroupingStore,
  96. proto = GroupingStore.prototype,
  97. oldFunc = proto.reload,
  98. wasCalled = false;
  99. proto.reload = function() {
  100. wasCalled = true;
  101. };
  102. var store = defaultSetup({remoteGroup: true});
  103. store.clearGrouping();
  104. assert.isTrue(wasCalled);
  105. proto.reload = oldFunc;
  106. },
  107. testRemoteGroupingDeletesBaseParams: function() {
  108. var store = defaultSetup({remoteGroup: true});
  109. //these params should be deleted
  110. Ext.apply(store.baseParams, {
  111. groupBy : 'abc',
  112. groupDir: 'ASC'
  113. });
  114. store.clearGrouping();
  115. assert.isUndefined(store.baseParams.groupBy);
  116. assert.isUndefined(store.baseParams.groupDir);
  117. },
  118. testRemoteGroupingDeletesLastOptions: function() {
  119. var store = defaultSetup({remoteGroup: true});
  120. //these params should be deleted
  121. store.lastOptions = store.lastOptions || {};
  122. store.lastOptions.params = store.lastOptions.params || {};
  123. Ext.apply(store.lastOptions.params, {
  124. groupBy : 'abc',
  125. groupDir: 'ASC'
  126. });
  127. store.clearGrouping();
  128. assert.isUndefined(store.lastOptions.params.groupBy);
  129. assert.isUndefined(store.lastOptions.params.groupDir);
  130. }
  131. }));
  132. suite.add(new Y.Test.Case({
  133. name: 'group by',
  134. testForceRegroup: function() {
  135. var GroupingStore = Ext.data.GroupingStore,
  136. proto = GroupingStore.prototype,
  137. oldFunc = proto.applyGroupField,
  138. callCount = 0;
  139. proto.applyGroupField = function() {
  140. callCount++;
  141. };
  142. var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
  143. store.groupBy('name', 'DESC');
  144. var currentCallCount = callCount;
  145. //this should activate another group operation
  146. store.groupBy('name', 'DESC', true);
  147. //cleanup
  148. proto.applyGroupField = oldFunc;
  149. assert.areEqual(currentCallCount + 1, callCount);
  150. },
  151. //if we already group by this field and direction, it should not group again
  152. testNoForceRegroup: function() {
  153. var GroupingStore = Ext.data.GroupingStore,
  154. proto = GroupingStore.prototype,
  155. oldFunc = proto.applyGroupField,
  156. callCount = 0;
  157. proto.applyGroupField = function() {
  158. callCount++;
  159. };
  160. var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
  161. store.groupBy('name');
  162. var currentCallCount = callCount;
  163. //this should not activate another group operation
  164. store.groupBy('name');
  165. //cleanup
  166. proto.applyGroupField = oldFunc;
  167. assert.areEqual(currentCallCount, callCount);
  168. },
  169. testSetsGroupDir: function() {
  170. var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
  171. store.groupBy('name');
  172. assert.areEqual('name', store.groupField);
  173. },
  174. testSetsGroupField: function() {
  175. var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
  176. store.groupBy('name', false, 'DESC');
  177. assert.areEqual('DESC', store.groupDir);
  178. },
  179. testAppliesGroupField: function() {
  180. var GroupingStore = Ext.data.GroupingStore,
  181. proto = GroupingStore.prototype,
  182. oldFunc = proto.applyGroupField,
  183. wasCalled = false;
  184. proto.applyGroupField = function() {
  185. wasCalled = true;
  186. };
  187. var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
  188. store.groupBy('name');
  189. //cleanup
  190. proto.applyGroupField = oldFunc;
  191. assert.isTrue(wasCalled);
  192. },
  193. testReloadsIfRemote: function() {
  194. var fired = false;
  195. var store = defaultSetup({
  196. remoteGroup: true,
  197. sortInfo : {field: 'name', direction: 'ASC'}
  198. });
  199. //fake a remote load
  200. store.load = function() {
  201. fired = true;
  202. };
  203. store.groupBy('name');
  204. assert.isTrue(fired);
  205. },
  206. testFiresDatachangedIfLocal: function() {
  207. var fired = false,
  208. store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
  209. store.on('datachanged', function() {
  210. fired = true;
  211. }, this);
  212. store.groupBy('name');
  213. assert.isTrue(fired);
  214. },
  215. testFiresGroupchangeIfLocal: function() {
  216. var fired = false,
  217. store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
  218. store.on('groupchange', function() {
  219. fired = true;
  220. }, this);
  221. store.groupBy('name');
  222. assert.isTrue(fired);
  223. },
  224. testFiresGroupchangeIfRemote: function() {
  225. var fired = false;
  226. var store = defaultSetup({
  227. remoteGroup: true,
  228. sortInfo : {field: 'name', direction: 'ASC'}
  229. });
  230. //fake a remote load
  231. store.load = function() {
  232. store.fireEvent('load', store);
  233. };
  234. store.on('groupchange', function() {
  235. fired = true;
  236. }, this);
  237. store.groupBy('name');
  238. assert.isTrue(fired);
  239. },
  240. testGroupOnSort: function() {
  241. }
  242. }));
  243. suite.add(new Y.Test.Case({
  244. name: 'apply group field',
  245. setUp: function() {
  246. this.store = defaultSetup({
  247. remoteGroup: true
  248. });
  249. this.store.groupField = 'abc';
  250. this.store.groupDir = 'DESC';
  251. this.store.lastOptions = {params: {}};
  252. },
  253. testSetsBaseParams: function() {
  254. this.store.applyGroupField();
  255. assert.areEqual('abc', this.store.baseParams.groupBy);
  256. assert.areEqual('DESC', this.store.baseParams.groupDir);
  257. },
  258. testSetsLastOptionsGroupDir: function() {
  259. this.store.applyGroupField();
  260. assert.areEqual('DESC', this.store.lastOptions.params.groupDir);
  261. },
  262. testDeletesLastOptionsGroupBy: function() {
  263. this.store.applyGroupField();
  264. assert.isUndefined(this.store.lastOptions.params.groupBy);
  265. }
  266. }));
  267. suite.add(new Y.Test.Case({
  268. name: 'apply sort'
  269. }));
  270. suite.add(new Y.Test.Case({
  271. name: 'apply grouping'
  272. }));
  273. //not really sure what this function does or why it does it. These tests just ensure
  274. //that any refactoring does not break it
  275. suite.add(new Y.Test.Case({
  276. name: 'get group state',
  277. testReturnsGroupField: function() {
  278. var store = defaultSetup();
  279. store.groupField = 'abc';
  280. assert.areEqual('abc', store.getGroupState());
  281. },
  282. //if only sorting is on sortinfo
  283. testReturnsSortInfoField: function() {
  284. var store = defaultSetup({groupOnSort: true});
  285. store.groupField = 'abc';
  286. store.sortInfo = {field: 'def'};
  287. assert.areEqual('def', store.getGroupState());
  288. },
  289. //if no sorting is applied anywhere
  290. testReturnsUndefined: function() {
  291. var store = defaultSetup({groupOnSort: true});
  292. store.groupField = 'abc';
  293. store.sortInfo = {};
  294. assert.isUndefined(store.getGroupState());
  295. }
  296. }));
  297. })();