Upgrade to ExtJS 3.2.1 - Released 04/27/2010
[extjs.git] / test / unit / widgets / form / CompositeField.js
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.form.CompositeField'),
13         assert = Y.Assert;
14     
15     //builds a simple composite field
16     function buildField(config) {
17         config = config || {};
18               
19         Ext.applyIf(config, {
20             items: [
21                 {xtype: 'textfield', name: 'title',     width: 40, fieldLabel: 'Title'},
22                 {xtype: 'textfield', name: 'firstName', flex:  1,  fieldLabel: 'First'},
23                 {xtype: 'textfield', name: 'lastName',  flex:  1,  fieldLabel: 'Last'}
24             ]
25         });
26         
27         return new Ext.form.CompositeField(config);
28     };
29     
30     //builds and renders a FormPanel containing a composite field
31     function createForm() {
32         this.field1 = new Ext.form.TextField({name: 'firstName', flex: 1, fieldLabel: 'First'});
33         this.field2 = new Ext.form.TextField({name: 'lastName',  flex: 1, fieldLabel: 'Last'});
34         
35         this.composite = new Ext.form.CompositeField({
36             items: [
37                 {xtype: 'textfield', name: 'title', width: 40, fieldLabel: 'Title'},
38                 this.field1,
39                 this.field2
40             ]
41         });
42         
43         this.form = new Ext.form.FormPanel({
44             renderTo: Ext.getBody(),
45             items   : [
46                 this.composite
47             ]
48         });
49     };
50     
51     //use this to tearDown createForm
52     function destroyForm() {
53         this.form.destroy();
54     };
55     
56     suite.add(new Y.Test.Case({
57         name: 'building the label',
58         
59         testDefaultLabel: function() {
60             var field = buildField();
61             
62             assert.areSame('Title, First, Last', field.fieldLabel);
63         },
64         
65         testCustomFieldLabel: function() {
66             var field = buildField({fieldLabel: 'Custom label'});
67             
68             assert.areSame('Custom label', field.fieldLabel);
69         }
70     }));
71     
72     suite.add(new Y.Test.Case({
73         name: 'isDirty',
74         
75         setUp   : createForm,
76         tearDown: destroyForm,
77         
78         testFalseIfDisabled: function() {
79             this.composite.disable();
80
81             assert.isFalse(this.composite.isDirty());
82         },
83         
84         testIsClean: function() {
85             assert.isFalse(this.composite.isDirty());
86         },
87         
88         testOneFieldDirty: function() {
89             this.field1.isDirty = function() {
90                 return true;
91             };
92
93             assert.isTrue(this.composite.isDirty());
94         }
95     }));
96     
97     suite.add(new Y.Test.Case({
98         name: 'reset',
99         
100         setUp   : createForm,
101         tearDown: destroyForm,
102         
103         testPropagatesToChildren: function() {
104             var callCount = 0;
105             
106             var resetFunc =function() {
107                 callCount ++;
108             };
109             
110             this.field1.reset = resetFunc;
111             this.field2.reset = resetFunc;
112             
113             //we need to set this to avoid an unrelated error when resetting
114             //clearInvalid would otherwise try to clear a non-existent DOM node
115             this.composite.clearInvalid = Ext.emptyFn;
116             
117             this.composite.reset();
118             
119             assert.areEqual(2, callCount);
120         }
121     }));
122     
123     suite.add(new Y.Test.Case({
124         name: 'enabling and disabling',
125         
126         setUp   : createForm,
127         tearDown: destroyForm,
128         
129         testEnablePropagatesToChildren: function() {
130             var callCount = 0;
131             
132             var enableFunc =function() {
133                 callCount ++;
134             };
135             
136             this.field1.enable = enableFunc;
137             this.field2.enable = enableFunc;
138             
139             this.composite.onEnable();
140             
141             assert.areEqual(2, callCount);
142         },
143         
144         testDisablePropagatesToChildren: function() {
145             var callCount = 0;
146             
147             var disableFunc = function() {
148                 callCount ++;
149             };
150             
151             this.field1.disable = disableFunc;
152             this.field2.disable = disableFunc;
153             
154             this.composite.onDisable();
155             
156             assert.areEqual(2, callCount);
157         }
158     }));
159     
160     suite.add(new Y.Test.Case({
161         name: 'setting read only',
162         
163         setUp   : createForm,
164         tearDown: destroyForm,
165         
166         testSetsSubFields: function() {
167             var callCount = 0;
168             
169             var func = function() {
170                 callCount ++;
171             };
172             
173             this.field1.setReadOnly = func;
174             this.field2.setReadOnly = func;
175             
176             this.composite.setReadOnly();
177             
178             assert.areEqual(2, callCount);
179         },
180         
181         testSetsReadOnly: function() {
182             var comp = this.composite;
183             
184             assert.isFalse(comp.readOnly);
185             
186             comp.setReadOnly();
187             
188             assert.isTrue(comp.readOnly);
189         }
190     }));
191     
192     // suite.add(new Y.Test.Case({
193     //     name: 'resizing',
194     //     
195     //     setUp   : createForm,
196     //     tearDown: destroyForm,
197     //     
198     //     testResizing
199     //     
200     // }));
201 })();