/javascripts/lib/test/unit/data/GroupingStore.js

https://bitbucket.org/ksokmesa/sina-asian · JavaScript · 386 lines · 264 code · 97 blank · 25 comment · 3 complexity · 9c81798502a2a214d793d046784b5c46 MD5 · raw file

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