@@ -531,6 +531,150 @@ describe('condition test', () => {
531531 await waitFor ( ( ) => expect ( screen . getByLabelText ( 'field2' ) ) . toHaveValue ( 'set with then' ) ) ;
532532 } ) ;
533533
534+ it ( 'should change fields value by set funciton' , async ( ) => {
535+ const schema = {
536+ fields : [
537+ {
538+ component : 'text-field' ,
539+ name : 'field1' ,
540+ label : 'field1' ,
541+ } ,
542+ {
543+ component : 'text-field' ,
544+ name : 'field2' ,
545+ label : 'field2' ,
546+ condition : {
547+ when : 'field1' ,
548+ is : 'foo' ,
549+ then : {
550+ set : ( formState ) => {
551+ return { field2 : formState . values . field1 } ;
552+ } ,
553+ } ,
554+ else : { visible : true , set : { } } ,
555+ } ,
556+ } ,
557+ ] ,
558+ } ;
559+
560+ render ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
561+ expect ( screen . getByLabelText ( 'field2' ) ) . toHaveValue ( '' ) ;
562+
563+ await userEvent . type ( screen . getByLabelText ( 'field1' ) , 'foo' ) ;
564+ await waitFor ( ( ) => expect ( screen . getByLabelText ( 'field2' ) ) . toHaveValue ( 'foo' ) ) ;
565+ } ) ;
566+
567+ it ( 'check the set function received valid arguments' , async ( ) => {
568+ const setSpy = jest . fn ( ) ;
569+
570+ setSpy . mockImplementation ( ( ) => ( { } ) ) ;
571+
572+ const schema = {
573+ fields : [
574+ {
575+ component : 'text-field' ,
576+ name : 'field1' ,
577+ label : 'field1' ,
578+ } ,
579+ {
580+ component : 'text-field' ,
581+ name : 'field2' ,
582+ label : 'field2' ,
583+ condition : {
584+ when : 'field1' ,
585+ is : 'foo' ,
586+ then : {
587+ set : setSpy ,
588+ } ,
589+ else : { visible : true , set : { } } ,
590+ } ,
591+ } ,
592+ ] ,
593+ } ;
594+
595+ render ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
596+
597+ await userEvent . type ( screen . getByLabelText ( 'field1' ) , 'foo' ) ;
598+
599+ await waitFor ( ( ) => expect ( setSpy ) . toHaveBeenCalledTimes ( 1 ) ) ;
600+
601+ const expected = { active : 'field1' , dirty : true } ;
602+
603+ await waitFor ( ( ) => expect ( setSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( expected ) , expect . any ( Function ) ) ) ;
604+ } ) ;
605+
606+ it ( 'check the object' , async ( ) => {
607+ const errorSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
608+
609+ const schema = {
610+ fields : [
611+ {
612+ component : 'text-field' ,
613+ name : 'field1' ,
614+ label : 'field1' ,
615+ } ,
616+ {
617+ component : 'text-field' ,
618+ name : 'field2' ,
619+ label : 'field2' ,
620+ condition : {
621+ when : 'field1' ,
622+ is : 'foo' ,
623+ then : {
624+ set : ( formState ) => {
625+ return null ;
626+ } ,
627+ } ,
628+ else : { visible : true , set : { } } ,
629+ } ,
630+ } ,
631+ ] ,
632+ } ;
633+
634+ render ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
635+
636+ await userEvent . type ( screen . getByLabelText ( 'field1' ) , 'foo' ) ;
637+
638+ await waitFor ( ( ) => {
639+ expect ( errorSpy ) . toHaveBeenCalled ( ) ;
640+ expect ( console . error . mock . calls [ 0 ] [ 0 ] ) . toContain ( 'Received invalid setterValue. Expected object, received: ' ) ;
641+ } ) ;
642+ } ) ;
643+
644+ it ( 'check the getFieldState object' , async ( ) => {
645+ const schema = {
646+ fields : [
647+ {
648+ component : 'text-field' ,
649+ name : 'field1' ,
650+ label : 'field1' ,
651+ } ,
652+ {
653+ component : 'text-field' ,
654+ name : 'field2' ,
655+ label : 'field2' ,
656+ condition : {
657+ when : 'field1' ,
658+ is : 'foo' ,
659+ then : {
660+ set : ( _formState , getFieldState ) => {
661+ return { field2 : getFieldState ( 'field1' ) . value } ;
662+ } ,
663+ } ,
664+ else : { visible : true , set : { } } ,
665+ } ,
666+ } ,
667+ ] ,
668+ } ;
669+ render ( < FormRenderer { ...initialProps } schema = { schema } /> ) ;
670+
671+ await userEvent . type ( screen . getByLabelText ( 'field1' ) , 'foo' ) ;
672+
673+ await waitFor ( ( ) => {
674+ expect ( screen . getByLabelText ( 'field2' ) ) . toHaveValue ( 'foo' ) ;
675+ } ) ;
676+ } ) ;
677+
534678 describe ( 'reducer' , ( ) => {
535679 it ( 'returns default' , ( ) => {
536680 const initialState = {
0 commit comments