@@ -12,7 +12,6 @@ import { FormSelectComponent } from '../../components/form-select/form-select.co
12
12
import { FormInputHiddenComponent } from '../../components/form-hidden/form-hidden.component' ;
13
13
import { By } from '@angular/platform-browser' ;
14
14
import { FormNavModule } from '../../../nav/nav-app' ;
15
- import { FieldConfigService } from '../../services/field-config.service' ;
16
15
17
16
interface IFormConfig {
18
17
form : any ;
@@ -27,7 +26,7 @@ interface IDynamicForm {
27
26
}
28
27
29
28
@Component ( {
30
- template : `<dynamic-form [formConfig]="formConfig" #form="dynamicForm" [model]="data" [dataProvider]="dataProvider" [ lookups]="lookups"></dynamic-form>`
29
+ template : `<dynamic-form [formConfig]="formConfig" #form="dynamicForm" [model]="data" [lookups]="lookups"></dynamic-form>`
31
30
} )
32
31
class TestComponent implements IDynamicForm {
33
32
formConfig
@@ -52,20 +51,21 @@ describe('DynamicFormComponent', () => {
52
51
TestBed . configureTestingModule ( {
53
52
declarations : [ DynamicFieldDirective , TestComponent , DynamicFormComponent , DynamicPanelComponent ] ,
54
53
imports : [ FormsModule , ReactiveFormsModule , TestModule , FormNavModule ] ,
55
- providers : [ DynamicFieldService , PreloadService , FieldConfigService ]
54
+ providers : [ DynamicFieldService , PreloadService ]
56
55
} )
57
56
. compileComponents ( )
58
57
. then ( ( ) => {
59
58
fixture = TestBed . createComponent ( TestComponent ) ;
60
59
component = fixture . componentInstance ;
61
60
component . formConfig = {
62
- fields : [
63
- { type : 'hidden' , name : 'id' } ,
64
- { type : 'text' , label : 'Publication Title:' , name : 'title' , placeholder : '' , required : true } ,
65
- { type : 'select' , label : 'Publication Type' , name : 'activityType' , options : [ 'a' , 'b' ] }
66
- ] ,
67
- form : [ { label : 'Title and Abstract' , panels : [ { label : 'Title and Abstract' , fields : [ 'id' , 'title' , 'activityType' ] } ] } ]
68
- }
61
+ form : [ {
62
+ fields : [
63
+ { type : 'hidden' , name : 'id' } ,
64
+ { type : 'text' , label : 'Publication Title:' , name : 'title' , placeholder : '' , required : true } ,
65
+ { type : 'select' , label : 'Publication Type' , name : 'activityType' , options : [ 'a' , 'b' ] }
66
+ ]
67
+ } ]
68
+ } ;
69
69
70
70
fixture . detectChanges ( ) ;
71
71
} ) ;
@@ -110,22 +110,17 @@ describe('DynamicFormComponent Core', () => {
110
110
TestBed . configureTestingModule ( {
111
111
declarations : [ DynamicFieldDirective , TestComponent , DynamicFormComponent , DynamicPanelComponent ] ,
112
112
imports : [ FormsModule , ReactiveFormsModule , TestModule , FormNavModule ] ,
113
- providers : [ DynamicFieldService , PreloadService , FieldConfigService ]
113
+ providers : [ DynamicFieldService , PreloadService ]
114
114
} ) . compileComponents ( ) ;
115
115
} ) ;
116
116
117
117
beforeEach ( ( ) => {
118
118
fixture = TestBed . createComponent ( DynamicFormComponent ) ;
119
119
component = fixture . componentInstance ;
120
120
component . formConfig = {
121
- fields : [
122
- { type : 'hidden' , name : 'id' } ,
123
- { type : 'text' , name : 'title' , required : true } ,
124
- { type : 'text' , name : 'test' , required : true }
125
- ] ,
126
121
form : [
127
- { label : 'fields and panels' , panels : [ { label : 'fields' , fields : [ ' title'] } ] } ,
128
- { label : 'fields, no panels' , fields : [ ' test'] } ,
122
+ { label : 'fields and panels' , panels : [ { label : 'fields' , fields : [ { type : 'text' , name : ' title', required : true } ] } ] } ,
123
+ { label : 'fields, no panels' , fields : [ { type : 'text' , name : ' test', required : true } ] } ,
129
124
{ label : 'no fields with panels' , panels : [ { label : 'no fields' } ] } ,
130
125
{ label : 'no fields, no panels' }
131
126
]
@@ -135,128 +130,50 @@ describe('DynamicFormComponent Core', () => {
135
130
} ) ;
136
131
137
132
describe ( 'ngOnInit()' , ( ) => {
138
- it ( 'should add fields references to config group' , ( ) => {
139
- expect ( component . formConfig . form [ 0 ] . controls . length ) . toEqual ( 1 ) ;
140
- } ) ;
141
-
142
- it ( 'should add fields references to config group with no panels' , ( ) => {
143
- expect ( component . formConfig . form [ 1 ] . controls . length ) . toEqual ( 1 ) ;
144
- } ) ;
145
-
146
- it ( 'should not add fields references to config group with no panels' , ( ) => {
147
- expect ( component . formConfig . form [ 2 ] . controls . length ) . toEqual ( 0 ) ;
148
- } ) ;
149
-
150
- it ( 'should not add fields references to config group with no panels no fields' , ( ) => {
151
- expect ( component . formConfig . form [ 3 ] . controls . length ) . toEqual ( 0 ) ;
152
- } ) ;
153
-
154
- it ( 'should patch model' , ( ) => {
155
- expect ( component . form . value . title ) . toEqual ( model . title ) ;
156
- } ) ;
157
133
158
- it ( 'should not patch model' , ( ) => {
159
- component . model = null ;
160
- component . ngOnInit ( ) ;
161
- expect ( component . form . value . title ) . toBeUndefined ( ) ;
134
+ it ( 'should create group' , ( ) => {
135
+ expect ( component . form ) . toBeDefined ( ) ;
162
136
} ) ;
163
137
164
138
describe ( 'Lookup Expansion' , ( ) => {
165
- it ( 'should not set lookup when there is no lookup in field' , ( ) => {
166
- component . lookups = { test : [ 'a' , 'b' , 'c' ] } ;
167
- component . formConfig = { fields : [ { type : 'text' , name : 'title' } ] , form : [ ] } ;
168
- component . ngOnInit ( ) ;
169
- expect ( component . formConfig . fields [ 0 ] . options ) . toBeUndefined ( ) ;
170
- } ) ;
171
139
172
140
it ( 'should not find lookup when no lookups were passed' , ( ) => {
173
141
component . lookups = null ;
174
- component . formConfig = { fields : [ { type : 'text' , name : 'title' , lookup : 'test' } ] , form : [ ] } ;
142
+ component . formConfig = { form : [ { fields : [ { type : 'text' , name : 'title' , lookup : 'test' } ] } ] } ;
175
143
component . ngOnInit ( ) ;
176
- expect ( component . formConfig . fields [ 0 ] . options ) . toBeUndefined ( ) ;
144
+ expect ( component . formConfig . form [ 0 ] . fields [ 0 ] . options ) . toBeUndefined ( ) ;
177
145
} ) ;
178
146
179
147
it ( 'should not find lookup when no lookups found' , ( ) => {
180
148
component . lookups = { test : [ 'a' , 'b' , 'c' ] } ; ;
181
- component . formConfig = { fields : [ { type : 'text' , name : 'title' , lookup : 'test1' } ] , form : [ ] } ;
149
+ component . formConfig = { form : [ { fields : [ { type : 'text' , name : 'title' } ] } ] } ;
182
150
component . ngOnInit ( ) ;
183
- expect ( component . formConfig . fields [ 0 ] . options ) . toBeUndefined ( ) ;
151
+ expect ( component . formConfig . form [ 0 ] . fields [ 0 ] . options ) . toBeUndefined ( ) ;
184
152
} ) ;
185
153
186
- it ( 'should copy lookup' , ( ) => {
154
+ it ( 'should not copy lookup with wrong name ' , ( ) => {
187
155
component . lookups = { test : [ 'a' , 'b' , 'c' ] } ; ;
188
- component . formConfig = { fields : [ { type : 'text' , name : 'title' , lookup : 'test ' } ] , form : [ ] } ;
156
+ component . formConfig = { form : [ { fields : [ { type : 'text' , name : 'title' , lookup : 'test1 ' } ] } ] } ;
189
157
component . ngOnInit ( ) ;
190
- expect ( component . formConfig . fields [ 0 ] . options ) . toBeDefined ( ) ;
158
+ expect ( component . formConfig . form [ 0 ] . fields [ 0 ] . options ) . toBeUndefined ( ) ;
191
159
} ) ;
192
160
193
161
it ( 'should copy lookup' , ( ) => {
194
162
component . lookups = { test : [ 'a' , 'b' , 'c' ] } ; ;
195
- component . formConfig = { fields : [ { type : 'text' , name : 'title' , lookup : 'test' } ] , form : [ ] } ;
163
+ component . formConfig = { form : [ { fields : [ { type : 'text' , name : 'title' , lookup : 'test' } ] } ] } ;
196
164
component . ngOnInit ( ) ;
197
- expect ( component . formConfig . fields [ 0 ] . options ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
165
+ expect ( component . formConfig . form [ 0 ] . fields [ 0 ] . options ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
198
166
} ) ;
199
167
200
168
it ( 'should extract lookup' , ( ) => {
201
169
component . lookups = { test : [ { t : 'a' } , { t : 'b' } , { t : 'c' } ] } ; ;
202
- component . formConfig = { fields : [ { type : 'text' , name : 'title' , lookup : 'test' , extract : 't' } ] , form : [ ] } ;
170
+ component . formConfig = { form : [ { fields : [ { type : 'text' , name : 'title' , lookup : 'test' , extract : 't' } ] } ] } ;
203
171
component . ngOnInit ( ) ;
204
- expect ( component . formConfig . fields [ 0 ] . options ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
172
+ expect ( component . formConfig . form [ 0 ] . fields [ 0 ] . options ) . toEqual ( [ 'a' , 'b' , 'c' ] ) ;
205
173
} ) ;
206
174
207
-
208
- } ) ;
209
- } ) ;
210
-
211
- describe ( 'createControl()' , ( ) => {
212
- let cfg = { name : 'test' , type : 'text' , disabled : true , required : true , minLength : 5 , maxLength : 10 , email : true , min : 1 , max : 10 , pattern : new RegExp ( '\d' ) , nullValidator : true , value : 5 } ;
213
-
214
- it ( 'should set pattern validator' , ( ) => {
215
- let control = component . createControl ( { name : 'test' , type : 'text' , pattern : new RegExp ( '\d' ) , value : 5 } ) ;
216
- const vals = control . validator ( control ) ;
217
- expect ( vals . pattern ) . toBeTruthy ( ) ;
218
- } ) ;
219
-
220
- it ( 'should set email validator' , ( ) => {
221
- let control = component . createControl ( { name : 'test' , type : 'text' , email : true , value : 5 } ) ;
222
- const vals = control . validator ( control ) ;
223
- expect ( vals . email ) . toBeTruthy ( ) ;
224
- } ) ;
225
-
226
- it ( 'should set min length validator' , ( ) => {
227
- let control = component . createControl ( { name : 'test' , type : 'text' , minLength : 5 , maxLength : 10 , value : 'test' } ) ;
228
- const vals = control . validator ( control ) ;
229
- expect ( vals . minlength ) . toBeTruthy ( ) ;
230
- } ) ;
231
-
232
- it ( 'should set max length validator' , ( ) => {
233
- let control = component . createControl ( { name : 'test' , type : 'text' , maxLength : 2 , value : 'test' } ) ;
234
- const vals = control . validator ( control ) ;
235
- expect ( vals . maxlength ) . toBeTruthy ( ) ;
236
- } ) ;
237
-
238
- it ( 'should set required validator' , ( ) => {
239
- let control = component . createControl ( { name : 'test' , type : 'text' , required : true , value : '' } ) ;
240
- const vals = control . validator ( control ) ;
241
- expect ( vals . required ) . toBeTruthy ( ) ;
242
- } ) ;
243
-
244
- it ( 'should set min value validator' , ( ) => {
245
- let control = component . createControl ( { name : 'test' , type : 'text' , min : 2 , value : 1 } ) ;
246
- const vals = control . validator ( control ) ;
247
- expect ( vals . min ) . toBeTruthy ( ) ;
248
- } ) ;
249
-
250
- it ( 'should set max value validator' , ( ) => {
251
- let control = component . createControl ( { name : 'test' , type : 'text' , max : 2 , value : 22 } ) ;
252
- const vals = control . validator ( control ) ;
253
- expect ( vals . max ) . toBeTruthy ( ) ;
254
- } ) ;
255
-
256
- it ( 'should set value' , ( ) => {
257
- let control = component . createControl ( cfg ) ;
258
- expect ( control . value ) . toEqual ( cfg . value ) ;
259
175
} ) ;
260
176
261
177
} ) ;
178
+
262
179
} ) ;
0 commit comments