3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * Tests Ext.data.Store functionality
12 var suite = Ext.test.session.getSuite('Ext.data.GroupingStore'),
15 //a shared setup function used by several of the suites
16 var defaultSetup = function(config) {
17 config = config || {};
20 proxy : new Ext.data.MemoryProxy({}),
21 reader: new Ext.data.ArrayReader({}, [
22 {name: 'name', type: 'string'},
23 {name: 'email', type: 'string'},
24 {name: 'greatness', type: 'int'},
25 {name: 'group', type: 'string'},
26 {name: 'old', type: 'boolean'}
33 sortInfo: {field: 'name', direction: 'ASC'}
36 var store = new Ext.data.GroupingStore(config);
39 ['Ed Spencer', 'ed@extjs.com', 100, 'code', false],
40 ['Abe Elias', 'abe@extjs.com', 70, 'admin', false],
41 ['Aaron Conran', 'aaron@extjs.com', 5, 'admin', true],
42 ['Tommy Maintz', 'tommy@extjs.com', -15, 'code', true]
48 suite.add(new Y.Test.Case({
51 testAppliesGroupField: function() {
52 var GroupingStore = Ext.data.GroupingStore,
53 proto = GroupingStore.prototype,
54 oldFunc = proto.applyGroupField,
57 proto.applyGroupField = function() {
61 var store = new GroupingStore();
62 assert.isTrue(wasCalled);
64 proto.applyGroupField = oldFunc;
68 suite.add(new Y.Test.Case({
69 name: 'clear grouping',
71 testUnsetsGroupField: function() {
72 var store = defaultSetup();
73 store.groupField = 'abc';
75 store.clearGrouping();
76 assert.isFalse(store.groupField);
79 testLocalGroupingAppliesSort: function() {
80 var GroupingStore = Ext.data.GroupingStore,
81 proto = GroupingStore.prototype,
85 proto.sort = function() {
89 var store = defaultSetup({remoteGroup: false});
90 store.clearGrouping();
92 assert.isTrue(wasCalled);
97 testLocalGroupingFiresEvent: function() {
98 var store = defaultSetup({remoteGroup: false}),
101 store.on('datachanged', function() {
105 store.clearGrouping();
106 assert.isTrue(fired);
109 testRemoteGroupingReloads: function() {
110 var GroupingStore = Ext.data.GroupingStore,
111 proto = GroupingStore.prototype,
112 oldFunc = proto.reload,
115 proto.reload = function() {
119 var store = defaultSetup({remoteGroup: true});
120 store.clearGrouping();
122 assert.isTrue(wasCalled);
124 proto.reload = oldFunc;
127 testRemoteGroupingDeletesBaseParams: function() {
128 var store = defaultSetup({remoteGroup: true});
130 //these params should be deleted
131 Ext.apply(store.baseParams, {
136 store.clearGrouping();
138 assert.isUndefined(store.baseParams.groupBy);
139 assert.isUndefined(store.baseParams.groupDir);
142 testRemoteGroupingDeletesLastOptions: function() {
143 var store = defaultSetup({remoteGroup: true});
145 //these params should be deleted
146 store.lastOptions = store.lastOptions || {};
147 store.lastOptions.params = store.lastOptions.params || {};
149 Ext.apply(store.lastOptions.params, {
154 store.clearGrouping();
156 assert.isUndefined(store.lastOptions.params.groupBy);
157 assert.isUndefined(store.lastOptions.params.groupDir);
161 suite.add(new Y.Test.Case({
164 testForceRegroup: function() {
165 var GroupingStore = Ext.data.GroupingStore,
166 proto = GroupingStore.prototype,
167 oldFunc = proto.applyGroupField,
170 proto.applyGroupField = function() {
174 var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
175 store.groupBy('name', 'DESC');
177 var currentCallCount = callCount;
179 //this should activate another group operation
180 store.groupBy('name', 'DESC', true);
183 proto.applyGroupField = oldFunc;
185 assert.areEqual(currentCallCount + 1, callCount);
188 //if we already group by this field and direction, it should not group again
189 testNoForceRegroup: function() {
190 var GroupingStore = Ext.data.GroupingStore,
191 proto = GroupingStore.prototype,
192 oldFunc = proto.applyGroupField,
195 proto.applyGroupField = function() {
199 var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
200 store.groupBy('name');
202 var currentCallCount = callCount;
204 //this should not activate another group operation
205 store.groupBy('name');
208 proto.applyGroupField = oldFunc;
210 assert.areEqual(currentCallCount, callCount);
213 testSetsGroupDir: function() {
214 var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
215 store.groupBy('name');
217 assert.areEqual('name', store.groupField);
220 testSetsGroupField: function() {
221 var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
222 store.groupBy('name', false, 'DESC');
224 assert.areEqual('DESC', store.groupDir);
227 testAppliesGroupField: function() {
228 var GroupingStore = Ext.data.GroupingStore,
229 proto = GroupingStore.prototype,
230 oldFunc = proto.applyGroupField,
233 proto.applyGroupField = function() {
237 var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
238 store.groupBy('name');
241 proto.applyGroupField = oldFunc;
243 assert.isTrue(wasCalled);
246 testReloadsIfRemote: function() {
248 var store = defaultSetup({
250 sortInfo : {field: 'name', direction: 'ASC'}
254 store.load = function() {
258 store.groupBy('name');
260 assert.isTrue(fired);
263 testFiresDatachangedIfLocal: function() {
265 store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
267 store.on('datachanged', function() {
271 store.groupBy('name');
273 assert.isTrue(fired);
276 testFiresGroupchangeIfLocal: function() {
278 store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
280 store.on('groupchange', function() {
284 store.groupBy('name');
286 assert.isTrue(fired);
289 testFiresGroupchangeIfRemote: function() {
291 var store = defaultSetup({
293 sortInfo : {field: 'name', direction: 'ASC'}
297 store.load = function() {
298 store.fireEvent('load', store);
301 store.on('groupchange', function() {
305 store.groupBy('name');
307 assert.isTrue(fired);
310 testGroupOnSort: function() {
315 suite.add(new Y.Test.Case({
316 name: 'apply group field',
319 this.store = defaultSetup({
323 this.store.groupField = 'abc';
324 this.store.groupDir = 'DESC';
325 this.store.lastOptions = {params: {}};
328 testSetsBaseParams: function() {
329 this.store.applyGroupField();
331 assert.areEqual('abc', this.store.baseParams.groupBy);
332 assert.areEqual('DESC', this.store.baseParams.groupDir);
335 testSetsLastOptionsGroupDir: function() {
336 this.store.applyGroupField();
338 assert.areEqual('DESC', this.store.lastOptions.params.groupDir);
341 testDeletesLastOptionsGroupBy: function() {
342 this.store.applyGroupField();
344 assert.isUndefined(this.store.lastOptions.params.groupBy);
348 suite.add(new Y.Test.Case({
352 suite.add(new Y.Test.Case({
353 name: 'apply grouping'
356 //not really sure what this function does or why it does it. These tests just ensure
357 //that any refactoring does not break it
358 suite.add(new Y.Test.Case({
359 name: 'get group state',
361 testReturnsGroupField: function() {
362 var store = defaultSetup();
363 store.groupField = 'abc';
365 assert.areEqual('abc', store.getGroupState());
368 //if only sorting is on sortinfo
369 testReturnsSortInfoField: function() {
370 var store = defaultSetup({groupOnSort: true});
371 store.groupField = 'abc';
372 store.sortInfo = {field: 'def'};
374 assert.areEqual('def', store.getGroupState());
377 //if no sorting is applied anywhere
378 testReturnsUndefined: function() {
379 var store = defaultSetup({groupOnSort: true});
380 store.groupField = 'abc';
383 assert.isUndefined(store.getGroupState());