PageRenderTime 33ms CodeModel.GetById 16ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/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
Possible License(s): GPL-3.0
  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    
 15    //a shared setup function used by several of the suites
 16    var defaultSetup = function(config) {
 17        config = config || {};
 18              
 19        Ext.applyIf(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'}
 27            ]),
 28            storeId: 'myStore',
 29            remoteSort : false,
 30            remoteGroup: false,
 31            groupField : 'group',
 32            
 33            sortInfo: {field: 'name', direction: 'ASC'}
 34        });
 35        
 36        var store = new Ext.data.GroupingStore(config);
 37        
 38        store.loadData([
 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]
 43        ]);
 44        
 45        return store;
 46    };
 47    
 48    suite.add(new Y.Test.Case({
 49        name: 'constructor',
 50        
 51        testAppliesGroupField: function() {
 52            var GroupingStore = Ext.data.GroupingStore,
 53                proto         = GroupingStore.prototype,
 54                oldFunc       = proto.applyGroupField,
 55                wasCalled     = false;
 56            
 57            proto.applyGroupField = function() {
 58                wasCalled = true;
 59            };
 60            
 61            var store = new GroupingStore();
 62            assert.isTrue(wasCalled);
 63            
 64            proto.applyGroupField = oldFunc;
 65        }
 66    }));
 67    
 68    suite.add(new Y.Test.Case({
 69        name: 'clear grouping',
 70        
 71        testUnsetsGroupField: function() {
 72            var store = defaultSetup();
 73            store.groupField = 'abc';
 74            
 75            store.clearGrouping();
 76            assert.isFalse(store.groupField);
 77        },
 78        
 79        testLocalGroupingAppliesSort: function() {
 80            var GroupingStore = Ext.data.GroupingStore,
 81                proto         = GroupingStore.prototype,
 82                oldFunc       = proto.sort,
 83                wasCalled     = false;
 84            
 85            proto.sort = function() {
 86                wasCalled = true;
 87            };
 88            
 89            var store = defaultSetup({remoteGroup: false});
 90            store.clearGrouping();
 91            
 92            assert.isTrue(wasCalled);
 93            
 94            proto.sort = oldFunc;
 95        },
 96        
 97        testLocalGroupingFiresEvent: function() {
 98            var store = defaultSetup({remoteGroup: false}),
 99                fired = false;
100            
101            store.on('datachanged', function() {
102                fired = true;
103            }, this);
104            
105            store.clearGrouping();
106            assert.isTrue(fired);
107        },
108        
109        testRemoteGroupingReloads: function() {
110            var GroupingStore = Ext.data.GroupingStore,
111                proto         = GroupingStore.prototype,
112                oldFunc       = proto.reload,
113                wasCalled     = false;
114            
115            proto.reload = function() {
116                wasCalled = true;
117            };
118            
119            var store = defaultSetup({remoteGroup: true});
120            store.clearGrouping();
121            
122            assert.isTrue(wasCalled);
123            
124            proto.reload = oldFunc;
125        },
126        
127        testRemoteGroupingDeletesBaseParams: function() {
128            var store = defaultSetup({remoteGroup: true});
129            
130            //these params should be deleted
131            Ext.apply(store.baseParams, {
132                groupBy : 'abc',
133                groupDir: 'ASC'
134            });
135            
136            store.clearGrouping();
137            
138            assert.isUndefined(store.baseParams.groupBy);
139            assert.isUndefined(store.baseParams.groupDir);
140        },
141        
142        testRemoteGroupingDeletesLastOptions: function() {
143            var store = defaultSetup({remoteGroup: true});
144            
145            //these params should be deleted
146            store.lastOptions        = store.lastOptions        || {};
147            store.lastOptions.params = store.lastOptions.params || {};
148            
149            Ext.apply(store.lastOptions.params, {
150                groupBy : 'abc',
151                groupDir: 'ASC'
152            });
153            
154            store.clearGrouping();
155            
156            assert.isUndefined(store.lastOptions.params.groupBy);
157            assert.isUndefined(store.lastOptions.params.groupDir);
158        }
159    }));
160    
161    suite.add(new Y.Test.Case({
162        name: 'group by',
163        
164        testForceRegroup: function() {
165            var GroupingStore = Ext.data.GroupingStore,
166                proto         = GroupingStore.prototype,
167                oldFunc       = proto.applyGroupField,
168                callCount     = 0;
169            
170            proto.applyGroupField = function() {
171                callCount++;
172            };
173            
174            var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
175            store.groupBy('name', 'DESC');
176            
177            var currentCallCount = callCount;
178            
179            //this should activate another group operation
180            store.groupBy('name', 'DESC', true);
181            
182            //cleanup
183            proto.applyGroupField = oldFunc;
184            
185            assert.areEqual(currentCallCount + 1, callCount);
186        },
187        
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,
193                callCount     = 0;
194            
195            proto.applyGroupField = function() {
196                callCount++;
197            };
198            
199            var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
200            store.groupBy('name');
201            
202            var currentCallCount = callCount;
203            
204            //this should not activate another group operation
205            store.groupBy('name');
206            
207            //cleanup
208            proto.applyGroupField = oldFunc;
209            
210            assert.areEqual(currentCallCount, callCount);
211        },
212        
213        testSetsGroupDir: function() {
214            var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
215            store.groupBy('name');
216            
217            assert.areEqual('name', store.groupField);
218        },
219        
220        testSetsGroupField: function() {
221            var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
222            store.groupBy('name', false, 'DESC');
223            
224            assert.areEqual('DESC', store.groupDir);
225        },
226        
227        testAppliesGroupField: function() {
228            var GroupingStore = Ext.data.GroupingStore,
229                proto         = GroupingStore.prototype,
230                oldFunc       = proto.applyGroupField,
231                wasCalled     = false;
232            
233            proto.applyGroupField = function() {
234                wasCalled = true;
235            };
236            
237            var store = defaultSetup({sortInfo: {field: 'name', direction: 'ASC'}});
238            store.groupBy('name');
239            
240            //cleanup
241            proto.applyGroupField = oldFunc;
242            
243            assert.isTrue(wasCalled);
244        },
245        
246        testReloadsIfRemote: function() {
247            var fired = false;
248            var store = defaultSetup({
249                remoteGroup: true, 
250                sortInfo   : {field: 'name', direction: 'ASC'}
251            });
252            
253            //fake a remote load
254            store.load = function() {
255                fired = true;
256            };
257            
258            store.groupBy('name');
259            
260            assert.isTrue(fired);
261        },
262        
263        testFiresDatachangedIfLocal: function() {
264            var fired = false,
265                store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
266            
267            store.on('datachanged', function() {
268                fired = true;
269            }, this);
270            
271            store.groupBy('name');
272            
273            assert.isTrue(fired);
274        },
275        
276        testFiresGroupchangeIfLocal: function() {
277            var fired = false,
278                store = defaultSetup({remoteGroup: false, sortInfo: {field: 'name', direction: 'ASC'}});
279            
280            store.on('groupchange', function() {
281                fired = true;
282            }, this);
283            
284            store.groupBy('name');
285            
286            assert.isTrue(fired);
287        },
288        
289        testFiresGroupchangeIfRemote: function() {
290            var fired = false;
291            var store = defaultSetup({
292                remoteGroup: true, 
293                sortInfo   : {field: 'name', direction: 'ASC'}
294            });
295            
296            //fake a remote load
297            store.load = function() {
298                store.fireEvent('load', store);
299            };
300            
301            store.on('groupchange', function() {
302                fired = true;
303            }, this);
304            
305            store.groupBy('name');
306            
307            assert.isTrue(fired);
308        },
309        
310        testGroupOnSort: function() {
311            
312        }
313    }));
314    
315    suite.add(new Y.Test.Case({
316        name: 'apply group field',
317        
318        setUp: function() {
319            this.store = defaultSetup({
320                remoteGroup: true
321            });
322            
323            this.store.groupField  = 'abc';
324            this.store.groupDir    = 'DESC';            
325            this.store.lastOptions = {params: {}};
326        },
327        
328        testSetsBaseParams: function() {
329            this.store.applyGroupField();
330            
331            assert.areEqual('abc',  this.store.baseParams.groupBy);
332            assert.areEqual('DESC', this.store.baseParams.groupDir);
333        },
334        
335        testSetsLastOptionsGroupDir: function() {
336            this.store.applyGroupField();
337            
338            assert.areEqual('DESC', this.store.lastOptions.params.groupDir);
339        },
340        
341        testDeletesLastOptionsGroupBy: function() {
342            this.store.applyGroupField();
343            
344            assert.isUndefined(this.store.lastOptions.params.groupBy);
345        }
346    }));
347    
348    suite.add(new Y.Test.Case({
349        name: 'apply sort'
350    }));
351    
352    suite.add(new Y.Test.Case({
353        name: 'apply grouping'
354    }));
355    
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',
360
361        testReturnsGroupField: function() {
362            var store = defaultSetup();
363            store.groupField = 'abc';
364            
365            assert.areEqual('abc', store.getGroupState());
366        },
367        
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'};
373            
374            assert.areEqual('def', store.getGroupState());
375        },
376        
377        //if no sorting is applied anywhere
378        testReturnsUndefined: function() {
379            var store = defaultSetup({groupOnSort: true});
380            store.groupField = 'abc';
381            store.sortInfo   = {};
382            
383            assert.isUndefined(store.getGroupState());
384        }
385    }));
386})();