@@ -321,6 +321,58 @@ describe('Form.List', () => {
321
321
matchKey ( 0 , '3' ) ;
322
322
} ) ;
323
323
324
+ it ( 'add when the second param is number' , ( ) => {
325
+ let operation ;
326
+ const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
327
+ const [ wrapper , getList ] = generateForm ( ( fields , opt ) => {
328
+ operation = opt ;
329
+ return (
330
+ < div >
331
+ { fields . map ( field => (
332
+ < Field { ...field } >
333
+ < Input />
334
+ </ Field >
335
+ ) ) }
336
+ </ div >
337
+ ) ;
338
+ } ) ;
339
+
340
+ act ( ( ) => {
341
+ operation . add ( ) ;
342
+ } ) ;
343
+ act ( ( ) => {
344
+ operation . add ( '1' , 2 ) ;
345
+ } ) ;
346
+
347
+ act ( ( ) => {
348
+ operation . add ( '2' , - 1 ) ;
349
+ } ) ;
350
+
351
+ expect ( errorSpy ) . toHaveBeenCalledWith (
352
+ 'Warning: The second parameter of the add function should be a valid positive number.' ,
353
+ ) ;
354
+ errorSpy . mockRestore ( ) ;
355
+
356
+ wrapper . update ( ) ;
357
+ expect ( getList ( ) . find ( Field ) . length ) . toEqual ( 3 ) ;
358
+ expect ( form . getFieldsValue ( ) ) . toEqual ( {
359
+ list : [ undefined , '1' , '2' ] ,
360
+ } ) ;
361
+
362
+ act ( ( ) => {
363
+ operation . add ( '0' , 0 ) ;
364
+ } ) ;
365
+ act ( ( ) => {
366
+ operation . add ( '4' , 3 ) ;
367
+ } ) ;
368
+
369
+ wrapper . update ( ) ;
370
+ expect ( getList ( ) . find ( Field ) . length ) . toEqual ( 5 ) ;
371
+ expect ( form . getFieldsValue ( ) ) . toEqual ( {
372
+ list : [ '0' , undefined , '1' , '4' , '2' ] ,
373
+ } ) ;
374
+ } ) ;
375
+
324
376
describe ( 'validate' , ( ) => {
325
377
it ( 'basic' , async ( ) => {
326
378
const [ , getList ] = generateForm (
@@ -417,6 +469,52 @@ describe('Form.List', () => {
417
469
expect ( form . getFieldError ( [ 'list' , 0 ] ) ) . toEqual ( [ "'list.1' must be at least 5 characters" ] ) ;
418
470
expect ( wrapper . find ( 'input' ) . props ( ) . value ) . toEqual ( 'test' ) ;
419
471
} ) ;
472
+
473
+ it ( 'when add() second param is number' , async ( ) => {
474
+ const [ wrapper , getList ] = generateForm (
475
+ ( fields , { add } ) => (
476
+ < div >
477
+ { fields . map ( field => (
478
+ < Field { ...field } rules = { [ { required : true } , { min : 5 } ] } >
479
+ < Input />
480
+ </ Field >
481
+ ) ) }
482
+
483
+ < button
484
+ className = "button"
485
+ type = "button"
486
+ onClick = { ( ) => {
487
+ add ( 'test4' , 1 ) ;
488
+ } }
489
+ />
490
+
491
+ < button
492
+ className = "button1"
493
+ type = "button"
494
+ onClick = { ( ) => {
495
+ add ( 'test5' , 0 ) ;
496
+ } }
497
+ />
498
+ </ div >
499
+ ) ,
500
+ {
501
+ initialValues : { list : [ 'test1' , 'test2' , 'test3' ] } ,
502
+ } ,
503
+ ) ;
504
+
505
+ expect ( wrapper . find ( Input ) ) . toHaveLength ( 3 ) ;
506
+ await changeValue ( getField ( getList ( ) , 0 ) , '' ) ;
507
+ expect ( form . getFieldError ( [ 'list' , 0 ] ) ) . toEqual ( [ "'list.0' is required" ] ) ;
508
+
509
+ wrapper . find ( '.button' ) . simulate ( 'click' ) ;
510
+ wrapper . find ( '.button1' ) . simulate ( 'click' ) ;
511
+
512
+ expect ( wrapper . find ( Input ) ) . toHaveLength ( 5 ) ;
513
+ expect ( form . getFieldError ( [ 'list' , 1 ] ) ) . toEqual ( [ "'list.0' is required" ] ) ;
514
+
515
+ await changeValue ( getField ( getList ( ) , 1 ) , 'test' ) ;
516
+ expect ( form . getFieldError ( [ 'list' , 1 ] ) ) . toEqual ( [ "'list.1' must be at least 5 characters" ] ) ;
517
+ } ) ;
420
518
} ) ;
421
519
422
520
it ( 'warning if children is not function' , ( ) => {
0 commit comments