@@ -476,35 +476,69 @@ describe('Form.Basic', () => {
476
476
expect ( hasError ) . toBeTruthy ( ) ;
477
477
} ) ;
478
478
479
- it ( 'setFields' , ( ) => {
480
- let form ;
481
- const wrapper = mount (
482
- < div >
483
- < Form
484
- ref = { instance => {
485
- form = instance ;
486
- } }
487
- >
488
- < InfoField name = "username" >
489
- < Input />
490
- </ InfoField >
491
- </ Form >
492
- </ div > ,
493
- ) ;
479
+ describe ( 'setFields' , ( ) => {
480
+ it ( 'should work' , ( ) => {
481
+ let form ;
482
+ const wrapper = mount (
483
+ < div >
484
+ < Form
485
+ ref = { instance => {
486
+ form = instance ;
487
+ } }
488
+ >
489
+ < InfoField name = "username" >
490
+ < Input />
491
+ </ InfoField >
492
+ </ Form >
493
+ </ div > ,
494
+ ) ;
494
495
495
- form . setFields ( [
496
- {
497
- name : 'username' ,
498
- touched : false ,
499
- validating : true ,
500
- errors : [ 'Set It!' ] ,
501
- } ,
502
- ] ) ;
503
- wrapper . update ( ) ;
496
+ form . setFields ( [
497
+ {
498
+ name : 'username' ,
499
+ touched : false ,
500
+ validating : true ,
501
+ errors : [ 'Set It!' ] ,
502
+ } ,
503
+ ] ) ;
504
+ wrapper . update ( ) ;
504
505
505
- matchError ( wrapper , 'Set It!' ) ;
506
- expect ( wrapper . find ( '.validating' ) . length ) . toBeTruthy ( ) ;
507
- expect ( form . isFieldsTouched ( ) ) . toBeFalsy ( ) ;
506
+ matchError ( wrapper , 'Set It!' ) ;
507
+ expect ( wrapper . find ( '.validating' ) . length ) . toBeTruthy ( ) ;
508
+ expect ( form . isFieldsTouched ( ) ) . toBeFalsy ( ) ;
509
+ } ) ;
510
+
511
+ it ( 'should trigger by setField' , ( ) => {
512
+ const triggerUpdate = jest . fn ( ) ;
513
+ const formRef = React . createRef ( ) ;
514
+
515
+ const wrapper = mount (
516
+ < div >
517
+ < Form ref = { formRef } >
518
+ < Field shouldUpdate = { ( prev , next ) => prev . value !== next . value } >
519
+ { ( ) => {
520
+ triggerUpdate ( ) ;
521
+ return < input /> ;
522
+ } }
523
+ </ Field >
524
+ </ Form >
525
+ </ div > ,
526
+ ) ;
527
+ wrapper . update ( ) ;
528
+ triggerUpdate . mockReset ( ) ;
529
+
530
+ // Not trigger render
531
+ formRef . current . setFields ( [
532
+ { name : 'others' , value : 'no need to update' } ,
533
+ ] ) ;
534
+ wrapper . update ( ) ;
535
+ expect ( triggerUpdate ) . not . toHaveBeenCalled ( ) ;
536
+
537
+ // Trigger render
538
+ formRef . current . setFields ( [ { name : 'value' , value : 'should update' } ] ) ;
539
+ wrapper . update ( ) ;
540
+ expect ( triggerUpdate ) . toHaveBeenCalled ( ) ;
541
+ } ) ;
508
542
} ) ;
509
543
510
544
it ( 'render props get meta' , ( ) => {
0 commit comments