@@ -7,19 +7,20 @@ Vue.use(VueFormGenerator);
7
7
8
8
let el , vm ;
9
9
10
- function createFormGenerator ( schema = { } , model = null , options = { } ) {
10
+ function createFormGenerator ( schema = { } , model = null , options = { } , multiple = false ) {
11
11
el = document . createElement ( "div" ) ;
12
- el . innerHTML = `<vue-form-generator :schema="schema" :model="model" :options="options"></vue-form-generator>` ;
12
+ el . innerHTML = `<vue-form-generator :schema="schema" :model="model" :options="options" :multiple="multiple" v-ref:form ></vue-form-generator>` ;
13
13
vm = new Vue ( {
14
14
el : el ,
15
15
data : {
16
16
schema,
17
17
model,
18
- options
18
+ options,
19
+ multiple
19
20
}
20
21
} ) ;
21
22
22
- console . log ( el ) ;
23
+ // console.log(el);
23
24
24
25
return [ el , vm ] ;
25
26
}
@@ -213,5 +214,260 @@ describe("VueFormGenerator.vue", () => {
213
214
214
215
} ) ;
215
216
217
+ describe ( "check computed fields if multiple is true" , ( ) => {
218
+ let schema = {
219
+ fields : [
220
+ { type : "text" , label : "name" , model : "name" , multi : false } ,
221
+ { type : "text" , label : "phone" , model : "phone" , multi : true } ,
222
+ { type : "text" , label : "email" , model : "email" } // multi is undefined
223
+ ]
224
+ } ;
225
+ let form ;
226
+
227
+ before ( ( ) => {
228
+ createFormGenerator ( schema , { } , { } , true ) ;
229
+ form = vm . $refs . form ;
230
+ } ) ;
231
+
232
+ it ( "should render only phone field" , ( ) => {
233
+ expect ( form . fields . length ) . to . be . equal ( 1 ) ;
234
+ expect ( el . getElementsByTagName ( "tr" ) [ 0 ] . getElementsByTagName ( "td" ) [ 0 ] . textContent ) . to . be . equal ( "phone" ) ;
235
+ } ) ;
236
+ } ) ;
237
+
238
+ describe ( "check fieldDisabled with function" , ( ) => {
239
+ let schema = {
240
+ fields : [
241
+ {
242
+ type : "text" ,
243
+ label : "Name" ,
244
+ model : "name" ,
245
+ disabled ( model ) { return ! model . status ; }
246
+ }
247
+ ]
248
+ } ;
249
+ let model = {
250
+ name : "John Doe" ,
251
+ status : true
252
+ }
253
+
254
+ before ( ( ) => {
255
+ createFormGenerator ( schema , model ) ;
256
+ } ) ;
257
+
258
+ it ( "should be enabled the name field" , ( ) => {
259
+ let input = el . getElementsByTagName ( "input" ) [ 0 ] ;
260
+ expect ( input . disabled ) . to . be . false ;
261
+ } ) ;
262
+
263
+ it ( "should be disabled the name field" , ( done ) => {
264
+ model . status = false ;
265
+ vm . $nextTick ( ( ) => {
266
+ let input = el . getElementsByTagName ( "input" ) [ 0 ] ;
267
+ expect ( input . disabled ) . to . be . true ;
268
+
269
+ done ( ) ;
270
+ } ) ;
271
+ } ) ;
272
+
273
+ } ) ;
274
+
275
+ describe ( "check fieldDisabled with const" , ( ) => {
276
+ let schema = {
277
+ fields : [
278
+ {
279
+ type : "text" ,
280
+ label : "Name" ,
281
+ model : "name" ,
282
+ disabled : false
283
+ }
284
+ ]
285
+ } ;
286
+
287
+ let model = { name : "John Doe" } ;
288
+
289
+ before ( ( ) => {
290
+ createFormGenerator ( schema , model ) ;
291
+ } ) ;
292
+
293
+ it ( "should be enabled the name field" , ( ) => {
294
+ let input = el . getElementsByTagName ( "input" ) [ 0 ] ;
295
+ expect ( input . disabled ) . to . be . false ;
296
+ } ) ;
297
+
298
+ it ( "should be disabled the name field" , ( done ) => {
299
+ schema . fields [ 0 ] . disabled = true ;
300
+ vm . $nextTick ( ( ) => {
301
+ let input = el . getElementsByTagName ( "input" ) [ 0 ] ;
302
+ expect ( input . disabled ) . to . be . true ;
303
+
304
+ done ( ) ;
305
+ } ) ;
306
+ } ) ;
307
+
308
+ } ) ;
309
+
310
+ // TODO: wrong
311
+ describe ( "check fieldVisible with function" , ( ) => {
312
+ let schema = {
313
+ fields : [
314
+ {
315
+ type : "text" ,
316
+ label : "Name" ,
317
+ model : "name" ,
318
+ visible ( model ) { return model . status ; }
319
+ }
320
+ ]
321
+ } ;
322
+ let model = {
323
+ name : "John Doe" ,
324
+ status : true
325
+ }
326
+
327
+ before ( ( ) => {
328
+ createFormGenerator ( schema , model ) ;
329
+ } ) ;
330
+
331
+ it ( "should be visible the name field" , ( ) => {
332
+ expect ( el . querySelector ( "input[type=text]" ) ) . to . be . defined ;
333
+ } ) ;
334
+
335
+ it ( "should be hidden the name field" , ( done ) => {
336
+ model . status = false ;
337
+ vm . $nextTick ( ( ) => {
338
+ expect ( el . querySelector ( "input[type=text]" ) ) . to . be . null ;
339
+ done ( ) ;
340
+ } ) ;
341
+ } ) ;
342
+
343
+ } ) ;
344
+
345
+ describe ( "check fieldVisible with const" , ( ) => {
346
+ let schema = {
347
+ fields : [
348
+ {
349
+ type : "text" ,
350
+ label : "Name" ,
351
+ model : "name" ,
352
+ visible : true
353
+ }
354
+ ]
355
+ } ;
356
+
357
+ let model = { name : "John Doe" } ;
358
+
359
+ before ( ( ) => {
360
+ createFormGenerator ( schema , model ) ;
361
+ } ) ;
362
+
363
+ it ( "should be enabled the name field" , ( ) => {
364
+ expect ( el . querySelector ( "input[type=text]" ) ) . to . be . defined ;
365
+ } ) ;
366
+
367
+ it ( "should be disabled the name field" , ( done ) => {
368
+ schema . fields [ 0 ] . visible = false ;
369
+ vm . $nextTick ( ( ) => {
370
+ expect ( el . querySelector ( "input[type=text]" ) ) . to . be . null ;
371
+ done ( ) ;
372
+ } ) ;
373
+ } ) ;
374
+
375
+ } ) ;
376
+
377
+ describe ( "check validate" , ( ) => {
378
+ let schema = {
379
+ fields : [
380
+ {
381
+ type : "text" ,
382
+ label : "Name" ,
383
+ model : "name" ,
384
+ min : 3 ,
385
+ validator : VueFormGenerator . validators . string
386
+ }
387
+ ]
388
+ } ;
389
+
390
+ let model = { name : "John Doe" } ;
391
+ let form ;
392
+
393
+ before ( ( ) => {
394
+ createFormGenerator ( schema , model ) ;
395
+ form = vm . $refs . form ;
396
+ } ) ;
397
+
398
+ it ( "should empty the errors" , ( ) => {
399
+ expect ( form . errors ) . to . be . length ( 0 ) ;
400
+ expect ( form . validate ( ) ) . to . be . true ;
401
+ expect ( form . errors ) . to . be . length ( 0 ) ;
402
+ } ) ;
403
+
404
+ it ( "should give an validation error" , ( ) => {
405
+ model . name = "Ab" ;
406
+ expect ( form . validate ( ) ) . to . be . false ;
407
+ expect ( form . errors ) . to . be . length ( 1 ) ;
408
+ } ) ;
409
+
410
+ it ( "should no validation error" , ( ) => {
411
+ model . name = "Abc" ;
412
+ expect ( form . validate ( ) ) . to . be . true ;
413
+ expect ( form . errors ) . to . be . length ( 0 ) ;
414
+ } ) ;
415
+
416
+ } ) ;
417
+
418
+ describe ( "check validateAfterLoad option" , ( ) => {
419
+ let schema = {
420
+ fields : [
421
+ {
422
+ type : "text" ,
423
+ label : "Name" ,
424
+ model : "name" ,
425
+ min : 3 ,
426
+ validator : VueFormGenerator . validators . string
427
+ }
428
+ ]
429
+ } ;
430
+
431
+ let model = { name : "Me" } ;
432
+ let form ;
433
+
434
+ before ( ( ) => {
435
+ createFormGenerator ( schema , model , { validateAfterLoad : true } ) ;
436
+ form = vm . $refs . form ;
437
+ } ) ;
438
+
439
+ it ( "should be validation error at ready()" , ( done ) => {
440
+ vm . $nextTick ( ( ) => {
441
+ expect ( form . errors ) . to . be . length ( 1 ) ;
442
+ done ( ) ;
443
+ } ) ;
444
+ } ) ;
445
+
446
+ it ( "should be validation error if model is changed" , ( done ) => {
447
+ form . model = { name : "Al" } ;
448
+ vm . $nextTick ( ( ) => {
449
+ expect ( form . errors ) . to . be . length ( 1 ) ;
450
+ done ( ) ;
451
+ } ) ;
452
+ } ) ;
453
+
454
+ it ( "should be no errors if model is correct" , ( done ) => {
455
+ form . model = { name : "Bob" } ;
456
+ vm . $nextTick ( ( ) => {
457
+ expect ( form . errors ) . to . be . length ( 0 ) ;
458
+ done ( ) ;
459
+ } ) ;
460
+ } ) ;
461
+
462
+ it ( "should be no errors if validateAfterLoad is false" , ( done ) => {
463
+ form . options . validateAfterLoad = false ;
464
+ form . model = { name : "Ed" } ;
465
+ vm . $nextTick ( ( ) => {
466
+ expect ( form . errors ) . to . be . length ( 0 ) ;
467
+ done ( ) ;
468
+ } ) ;
469
+ } ) ;
470
+
471
+ } ) ;
216
472
217
473
} ) ;
0 commit comments