3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * Tests Ext.data.Store functionality
12 var suite = Ext.test.session.getSuite('Ext.form.BasicForm'),
15 function buildForm(config) {
16 config = config || {};
22 return new Ext.form.BasicForm(null, config);
26 //creates a duck object simulating a form field
27 function buildFormItem(options) {
30 options = options || {};
32 Ext.applyIf(options, {
38 return (function(itemId, config) {
39 return Ext.apply(config || {}, {
44 return config.name || config.id;
47 getItemId: function() {
51 validate: function() {
59 setValue: function() {
63 })(itemIdCount, options);
66 suite.add(new Y.Test.Case({
67 name: 'initialization with an element',
70 //create a fake form element to test that initEl is called
71 this.el = Ext.getBody().createChild({tag: 'form'});
74 tearDown: function() {
78 testItemsCreated: function() {
79 var form = buildForm();
81 assert.isTrue(form.items instanceof Ext.util.MixedCollection);
84 testInitsEl: function() {
85 var BasicForm = Ext.form.BasicForm,
86 proto = BasicForm.prototype;
88 var wasCalled = false,
89 oldInitEl = proto.initEl;
91 proto.initEl = function() {
95 var form = new BasicForm(this.el, {});
97 assert.isTrue(wasCalled);
100 proto.initEl = oldInitEl;
103 testGetEL: function() {
104 var form = new Ext.form.BasicForm(this.el, {});
106 assert.areEqual(this.el, form.getEl());
109 testGetsFormClass: function() {
110 var form = new Ext.form.BasicForm(this.el, {});
112 assert.isTrue(this.el.hasClass('x-form'));
116 suite.add(new Y.Test.Case({
120 this.form = buildForm();
122 this.validItem = buildFormItem({valid: true});
123 this.invalidItem = buildFormItem({valid: false});
126 testAllValid: function() {
127 this.form.add(this.validItem);
129 assert.isTrue(this.form.isValid());
132 testSomeInvalid: function() {
133 this.form.add(this.validItem, this.invalidItem);
135 assert.isFalse(this.form.isValid());
139 suite.add(new Y.Test.Case({
143 this.form = buildForm();
145 this.cleanItem = buildFormItem({dirty: false});
146 this.dirtyItem = buildFormItem({dirty: true});
149 testAllClean: function() {
150 this.form.add(this.cleanItem);
152 assert.isFalse(this.form.isDirty());
155 testSomeDirty: function() {
156 this.form.add(this.cleanItem, this.dirtyItem);
158 assert.isTrue(this.form.isDirty());
162 suite.add(new Y.Test.Case({
163 name: 'performing actions'
166 suite.add(new Y.Test.Case({
170 suite.add(new Y.Test.Case({
171 name: 'loading, resetting and updating records',
174 this.form = buildForm();
176 this.item1 = buildFormItem({id: 'name'});
177 this.item2 = buildFormItem({id: 'email'});
178 this.item3 = buildFormItem({id: 'phone'});
180 this.form.add(this.item1, this.item2, this.item3);
183 testReset: function() {
185 var reset = function() {
189 this.item1.reset = reset;
190 this.item2.reset = reset;
191 this.item3.reset = reset;
194 assert.areEqual(3, resetCount);
198 suite.add(new Y.Test.Case({
199 name: 'finding fields',
202 this.form = buildForm();
204 this.item1 = buildFormItem({id: 'name', dataIndex: 'someDataIndex'});
205 this.item2 = buildFormItem({id: 'email'});
206 this.item3 = buildFormItem({id: 'phone', name: 'phone number'});
208 this.form.add(this.item1, this.item2, this.item3);
211 testfindByItemId: function() {
212 assert.areEqual(this.item3, this.form.findField(this.item3.itemId));
215 testFindById: function() {
216 assert.areEqual(this.item2, this.form.findField('email'));
219 testFindByDataIndex: function() {
220 assert.areEqual(this.item1, this.form.findField('someDataIndex'));
223 testFindByName: function() {
224 assert.areEqual(this.item3, this.form.findField('phone number'));
228 suite.add(new Y.Test.Case({
229 name: 'marking invalid',
232 this.form = buildForm();
234 this.item1 = buildFormItem({id: 'name'});
235 this.item2 = buildFormItem({id: 'email'});
236 this.item3 = buildFormItem({id: 'phone'});
238 this.form.add(this.item1, this.item2, this.item3);
241 testMarkInvalidWithObject: function() {
242 var wasCalled = false, message;
244 this.item1.markInvalid = function(msg) {
249 this.form.markInvalid({
250 name: 'is a bad name'
253 assert.isTrue(wasCalled);
254 assert.areEqual('is a bad name', message);
257 testMarkInvalidWithArray: function() {
258 var wasCalled = false, message;
260 this.item2.markInvalid = function(msg) {
265 this.form.markInvalid([{id: 'email', msg: 'is the wrong format'}]);
267 assert.isTrue(wasCalled);
268 assert.areEqual('is the wrong format', message);
271 testClearInvalid: function() {
273 var clearInvalid = function() {
277 this.item1.clearInvalid = clearInvalid;
278 this.item2.clearInvalid = clearInvalid;
279 this.item3.clearInvalid = clearInvalid;
281 this.form.clearInvalid();
282 assert.areEqual(3, clearCount);
286 suite.add(new Y.Test.Case({
287 name: 'setting values',
290 this.form = buildForm({
291 trackResetOnLoad: true
294 this.item1 = buildFormItem({name: 'name', getValue: function() {return 'ed'; }});
295 this.item2 = buildFormItem({name: 'email'});
296 this.item3 = buildFormItem({name: 'phone', getValue: function() {return '333';}});
298 this.form.add(this.item1, this.item2, this.item3);
301 testSetByObject: function() {
302 var called = false, value;
304 this.item1.setValue = function(val) {
309 this.form.setValues({name: 'my name'});
311 assert.isTrue(wasCalled);
312 assert.areEqual('my name', value);
315 testSetByArray: function() {
316 var called = false, value;
318 this.item1.setValue = function(val) {
323 this.form.setValues([{id: 'name', value: 'my name'}]);
325 assert.isTrue(wasCalled);
326 assert.areEqual('my name', value);
329 testSavesOriginalValue: function() {
330 this.form.setValues({'phone': '444'});
332 assert.areEqual('333', this.item3.originalValue);
336 suite.add(new Y.Test.Case({
337 name: 'getting values',
340 this.form = buildForm();
342 this.item1 = buildFormItem({name: 'name', getValue: function() {return 'Edward';}});
343 this.item2 = buildFormItem({name: 'email', getValue: function() {return 'ed@extjs.com';}});
344 this.item3 = buildFormItem({name: 'phone', getValue: function() {return '333';}});
346 this.form.add(this.item1, this.item2, this.item3);
349 testGetFieldValues: function() {
350 var values = this.form.getFieldValues();
352 assert.areEqual('Edward', values.name);
353 assert.areEqual('ed@extjs.com', values.email);
354 assert.areEqual('333', values.phone);
357 testGetFieldValuesWithArray: function() {
358 //adding a second item with the same name should result in an array being returned
359 this.item4 = buildFormItem({
361 getValue: function() {return 200;}
364 this.form.add(this.item4);
366 var values = this.form.getFieldValues();
368 assert.areEqual(2, values.name.length);
369 assert.areEqual('Edward', values.name[0]);
370 assert.areEqual(200, values.name[1]);
373 testGetDirtyFieldValues: function() {
374 this.item1.isDirty = function() {return true;};
376 var values = this.form.getFieldValues(true);
378 assert.areEqual('Edward', values.name);
379 assert.isUndefined(values.email);
380 assert.isUndefined(values.phone);
383 testGetValuesForSubmission: function() {
388 suite.add(new Y.Test.Case({
389 name: 'adding and removing',
392 this.form = buildForm();
394 this.item1 = buildFormItem({id: 'name'});
395 this.item2 = buildFormItem({id: 'email'});
396 this.item3 = buildFormItem({id: 'phone'});
398 this.form.add(this.item1);
401 testAddField: function() {
402 var count = this.form.items.getCount();
404 this.form.add(this.item2);
406 assert.areEqual(count + 1, this.form.items.getCount());
409 testAddMultipleFields: function() {
410 var count = this.form.items.getCount();
412 this.form.add(this.item2, this.item3);
414 assert.areEqual(count + 2, this.form.items.getCount());
417 testAddReturnsForm: function() {
418 assert.areEqual(this.form, this.form.add(this.item2));
421 testRemoveField: function() {
422 var count = this.form.items.getCount();
424 this.form.remove(this.item1);
426 assert.areEqual(count - 1, this.form.items.getCount());
429 testRemoveReturnsForm: function() {
430 assert.areEqual(this.form, this.form.remove(this.item1));