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.Store'),
15 //a shared setup function used by several of the suites
16 var defaultSetup = function() {
17 this.store = new Ext.data.Store({
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'}
31 ['Ed Spencer', 'ed@extjs.com', 100, 'code', false],
32 ['Abe Elias', 'abe@extjs.com', 70, 'admin', false],
33 ['Aaron Conran', 'aaron@extjs.com', 5, 'admin', true],
34 ['Tommy Maintz', 'tommy@extjs.com', -15, 'code', true]
38 suite.add(new Y.Test.Case({
39 name: 'local single sorting',
43 testSetDefaultSort: function() {
44 this.store.setDefaultSort('name', 'DESC');
46 var store = this.store,
47 sortInfo = store.sortInfo;
49 assert.areEqual('name', sortInfo.field);
50 assert.areEqual('DESC', sortInfo.direction);
51 assert.areEqual('DESC', store.sortToggle['name']);
54 testSetDefaultSortDefaultToASC: function() {
55 this.store.setDefaultSort('email');
57 var store = this.store,
58 sortInfo = store.sortInfo;
60 assert.areEqual('email', sortInfo.field);
61 assert.areEqual('ASC', sortInfo['direction']);
62 assert.areEqual('ASC', store.sortToggle['email']);
65 testSortByField: function() {
66 var store = this.store;
68 store.sort('name', 'ASC');
69 assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
70 assert.areEqual('Abe Elias', store.getAt(1).get('name'));
71 assert.areEqual('Ed Spencer', store.getAt(2).get('name'));
72 assert.areEqual('Tommy Maintz', store.getAt(3).get('name'));
75 testToggling: function() {
76 var store = this.store;
78 store.sort('name', 'ASC');
80 //second call to sort toggles the direction
83 assert.areEqual('Aaron Conran', store.getAt(3).get('name'));
84 assert.areEqual('Abe Elias', store.getAt(2).get('name'));
85 assert.areEqual('Ed Spencer', store.getAt(1).get('name'));
86 assert.areEqual('Tommy Maintz', store.getAt(0).get('name'));
89 testSetsHasMultiSort: function() {
90 this.store.sort('name', 'ASC');
92 assert.isFalse(this.store.hasMultiSort);
95 testEventFired: function() {
99 store.on('datachanged', function() {
104 assert.isTrue(executed);
107 testSavesSortInfo: function() {
108 var store = this.store;
110 store.sort('name', 'DESC');
112 assert.areEqual('name', store.sortInfo.field);
113 assert.areEqual('DESC', store.sortInfo.direction);
116 //if we tell store to sort on a non-existent field it should return false and do nothing
117 testInvalidFieldIgnored: function() {
118 var store = this.store;
120 //first we'll sort by name to give some reference sorting
121 store.sort('name', 'ASC');
123 assert.isFalse(store.sort('someUnknownField'));
125 //make sure the original sorting was preserved
126 assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
127 assert.areEqual('Abe Elias', store.getAt(1).get('name'));
128 assert.areEqual('Ed Spencer', store.getAt(2).get('name'));
129 assert.areEqual('Tommy Maintz', store.getAt(3).get('name'));
133 suite.add(new Y.Test.Case({
134 name: 'local multiple sorting',
137 defaultSetup.call(this);
151 testSetsHasMultiSort: function() {
152 this.store.sort(this.sorters);
154 assert.isTrue(this.store.hasMultiSort);
157 testMultipleSorters: function() {
158 var store = this.store,
159 sorters = this.sorters;
163 assert.areEqual('Aaron Conran', store.getAt(0).get('name'));
164 assert.areEqual('Abe Elias', store.getAt(1).get('name'));
165 assert.areEqual('Tommy Maintz', store.getAt(2).get('name'));
166 assert.areEqual('Ed Spencer', store.getAt(3).get('name'));
169 // testMultipleSorterToggling: function() {
170 // var store = this.store,
171 // sorters = this.sorters;
173 // //first we'll sort to give some reference sorting
174 // store.sort(sorters, "ASC");
176 // //second call to sort toggles direction
177 // store.sort(sorters);
179 // assert.areEqual('Aaron Conran', store.getAt(3).get('name'));
180 // assert.areEqual('Abe Elias', store.getAt(2).get('name'));
181 // assert.areEqual('Tommy Maintz', store.getAt(1).get('name'));
182 // assert.areEqual('Ed Spencer', store.getAt(0).get('name'));
186 suite.add(new Y.Test.Case({
187 name: 'single filtering',
191 testFilterByField: function() {
192 var store = this.store;
194 store.filter('group', 'code');
195 assert.areEqual(2, store.getCount());
198 testFilterByFieldAnyMatch: function() {
199 var store = this.store;
201 store.filter('email', 'extjs', true);
202 assert.areEqual(4, store.getCount());
205 testFilterByFieldCaseSensitive: function() {
206 var store = this.store;
208 store.filter('group', 'Code', false, true);
209 assert.areEqual(0, store.getCount());
212 testFilterByFieldExactMatch: function() {
213 var store = this.store;
215 store.filter('email', 'aaron', false, false, true);
216 assert.areEqual(0, store.getCount());
219 testClearFilter: function() {
220 var store = this.store,
223 store.on('datachanged', function() {
227 store.filter('group', 'code');
228 assert.areEqual(2, store.getCount());
231 assert.areEqual(4, store.getCount());
233 assert.isTrue(executed);
236 testSuppressClearFilterEvent: function() {
237 var store = this.store,
240 store.filter('group', 'email');
241 store.clearFilter(true);
243 store.on('datachanged', function() {
246 assert.isFalse(executed);
249 testIsFiltered: function() {
250 var store = this.store;
252 assert.isFalse(store.isFiltered());
253 store.filter('group', 'code');
254 assert.isTrue(store.isFiltered());
257 testFilterByFunction: function() {
258 var store = this.store,
262 store.on('datachanged', function() {
266 var filterFn = function(item) {
269 return item.get('greatness') > 50;
272 store.filterBy(filterFn, this);
273 assert.areEqual(this, execScope);
274 assert.areEqual(2, store.getCount());
275 assert.isTrue(executed);
279 suite.add(new Y.Test.Case({
280 name : 'filtering more than once',
283 testFirstFilterIsCleared: function() {
284 var store = this.store;
286 store.filter('group', 'code');
287 assert.areEqual(2, store.getCount());
289 store.filter('old', false);
291 //if filter had not been reset first, count would be 1
292 assert.areEqual(2, store.getCount());
296 suite.add(new Y.Test.Case({
297 name : 'multiple filters',
299 defaultSetup.call(this);
313 testMultipleBasicFilters: function() {
314 this.store.filter(this.filters);
316 //applying the filter set above shoule yield one result
317 assert.areEqual(1, this.store.getCount());
318 assert.areEqual('Tommy Maintz', this.store.data.first().get('name'));
321 testMultiFiltersFiresDataChanged: function() {
322 var executed = false;
324 this.store.on('datachanged', function() {
328 this.store.filter(this.filters);
330 assert.isTrue(executed);
333 //tests that the anyMatch and caseSensitive defaults are correctly applied
334 testMultiFilterDefaults: function() {
338 //tests a single custom filter
339 testCustomFilter: function() {
342 //tests that the passed filter function is called
343 //tests that the filter is called in the right scope
345 fn: function(record) {
347 return record.get('group') == 'admin' && record.get('old') === true;
352 assert.areEqual(this, execScope);
353 assert.areEqual(1, this.store.getCount());
354 assert.areEqual('Aaron Conran', this.store.data.first().get('name'));
357 //tests multiple filters where we pass in custom matcher functions
358 testMultiCustomFilters: function() {
361 fn: function(record) {
362 return record.get('group') == 'admin';
366 fn: function(record) {
367 return record.get('old') === false;
372 assert.areEqual(1, this.store.getCount());
373 assert.areEqual('Abe Elias', this.store.data.first().get('name'));
376 testBasicAndCustomFilters: function() {
377 //should return a single result - Ed Spencer
380 fn: function(record) {
381 return record.get('old') === false;
390 assert.areEqual(1, this.store.getCount());
391 assert.areEqual('Ed Spencer', this.store.data.first().get('name'));
394 testClearMultipleFilters: function() {
395 this.store.filter(this.filters);
396 assert.areEqual(1, this.store.getCount());
398 //make sure that clearing multiple filters is still correct
399 this.store.clearFilter();
400 assert.areEqual(4, this.store.getCount());
404 suite.add(new Y.Test.Case({
405 name: 'inserting and removing',
407 testInsert: function() {
411 testInsertFiresAddEvent: function() {
415 testRemove: function() {
419 testRemoveAll: function() {
423 testRemoveAt: function() {
428 suite.add(new Y.Test.Case({
433 //we hijack Ext.StoreMgr.unregister temporarily and then set it back in this test case
434 testUnregistersFromStore: function() {
435 var executed = false,
437 unregister = Ext.StoreMgr.unregister;
439 Ext.StoreMgr.unregister = function(store) {
444 this.store.destroy();
446 assert.isTrue(executed);
447 assert.areEqual(this.store, arg);
449 Ext.StoreMgr.unregister = unregister;
452 testClearsData: function() {
453 var executed = false;
454 this.store.clearData = function() {
458 this.store.destroy();
459 assert.isTrue(executed);
462 testDestroysProxy: function() {
463 var executed = false,
465 proxy = this.store.proxy,
466 destroy = Ext.destroy;
468 Ext.destroy = function(store) {
473 this.store.destroy();
475 assert.isTrue(executed);
476 assert.areEqual(proxy, arg);
478 Ext.destroy = destroy;
481 testUnsetsReferences: function() {
482 this.store.destroy();
484 assert.isNull(this.store.reader);
485 assert.isNull(this.store.writer);
486 assert.isNull(this.store.data);
489 testIsDestroyed: function() {
490 assert.isFalse(this.store.isDestroyed);
492 this.store.destroy();
494 assert.isTrue(this.store.isDestroyed);
498 suite.add(new Y.Test.Case({
499 name: 'counting and iterators',
503 testGetCount: function() {
504 assert.areEqual(4, this.store.getCount());
507 testGetTotalCount: function() {
508 //PENDING - need a fake remote data set test with paging
511 testEach: function() {
515 this.store.each(function() {
520 assert.areEqual(4, count);
521 assert.areEqual(this, callScope);
524 testSum: function() {
525 var sum = this.store.sum('greatness');
527 assert.areEqual(160, sum);
530 testSumWithStartAndEnd: function() {
531 var sum = this.store.sum('greatness', 1, 3);
533 assert.areEqual(60, sum);
536 //a normal collect test - check that we're pulling the right values out of the store
537 testCollect: function() {
538 var values = this.store.collect('name');
540 assert.areEqual(4, values.length);
541 assert.areNotEqual(-1, values.indexOf("Ed Spencer"));
542 assert.areNotEqual(-1, values.indexOf("Abe Elias"));
543 assert.areNotEqual(-1, values.indexOf("Aaron Conran"));
544 assert.areNotEqual(-1, values.indexOf("Tommy Maintz"));
547 //checks that all collected values are unique
548 testCollectIsUnique: function() {
549 var values = this.store.collect('group');
551 assert.areEqual(2, values.length);
552 assert.areNotEqual(-1, values.indexOf("code"));
553 assert.areNotEqual(-1, values.indexOf("admin"));
557 suite.add(new Y.Test.Case({
560 testCommitChanges: function() {
564 testRejectChanges: function() {
568 testAfterCommit: function() {
572 testAfterReject: function() {
577 suite.add(new Y.Test.Case({
580 testGetModifiedRecords: function() {
585 suite.add(new Y.Test.Case({