@@ -992,6 +992,129 @@ describe('renderForm function', () => {
992
992
wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
993
993
expect ( onSubmit ) . toHaveBeenCalledWith ( { unmnounted : undefined , foo : 'barrr' } ) ;
994
994
} ) ;
995
+
996
+ it ( 'should clear values after unmount and set to field cleared value' , ( ) => {
997
+ const schema = {
998
+ fields : [
999
+ {
1000
+ component : componentTypes . TEXT_FIELD ,
1001
+ name : 'foo'
1002
+ } ,
1003
+ {
1004
+ component : componentTypes . TEXT_FIELD ,
1005
+ name : 'unmnounted' ,
1006
+ label : 'Label 1' ,
1007
+ clearedValue : 'bla' ,
1008
+ clearOnUnmount : true ,
1009
+ condition : {
1010
+ when : 'foo' ,
1011
+ is : 'show'
1012
+ }
1013
+ }
1014
+ ]
1015
+ } ;
1016
+
1017
+ const onSubmit = jest . fn ( ) ;
1018
+
1019
+ const wrapper = mount (
1020
+ < FormRenderer
1021
+ FormTemplate = { ( props ) => < FormTemplate { ...props } /> }
1022
+ componentMapper = { {
1023
+ [ componentTypes . TEXT_FIELD ] : TextField
1024
+ } }
1025
+ schema = { schema }
1026
+ onSubmit = { ( values ) => onSubmit ( values ) }
1027
+ clearedValue = "BlaBlaBla"
1028
+ />
1029
+ ) ;
1030
+
1031
+ wrapper
1032
+ . find ( 'input' )
1033
+ . first ( )
1034
+ . simulate ( 'change' , { target : { value : 'show' } } ) ;
1035
+ wrapper . update ( ) ;
1036
+ wrapper
1037
+ . find ( 'input' )
1038
+ . last ( )
1039
+ . simulate ( 'change' , { target : { value : 'foovalue' } } ) ;
1040
+ wrapper . update ( ) ;
1041
+
1042
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1043
+
1044
+ expect ( onSubmit ) . toHaveBeenCalledWith ( { foo : 'show' , unmnounted : 'foovalue' } ) ;
1045
+ onSubmit . mockClear ( ) ;
1046
+
1047
+ wrapper
1048
+ . find ( 'input' )
1049
+ . first ( )
1050
+ . simulate ( 'change' , { target : { value : 'barrr' } } ) ;
1051
+ wrapper . update ( ) ;
1052
+
1053
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1054
+
1055
+ expect ( onSubmit ) . toHaveBeenCalledWith ( { foo : 'barrr' , unmnounted : 'bla' } ) ;
1056
+ } ) ;
1057
+
1058
+ it ( 'should clear values after unmount and set to form cleared value' , ( ) => {
1059
+ const schema = {
1060
+ fields : [
1061
+ {
1062
+ component : componentTypes . TEXT_FIELD ,
1063
+ name : 'foo'
1064
+ } ,
1065
+ {
1066
+ component : componentTypes . TEXT_FIELD ,
1067
+ name : 'unmnounted' ,
1068
+ label : 'Label 1' ,
1069
+ clearOnUnmount : true ,
1070
+ condition : {
1071
+ when : 'foo' ,
1072
+ is : 'show'
1073
+ }
1074
+ }
1075
+ ]
1076
+ } ;
1077
+
1078
+ const onSubmit = jest . fn ( ) ;
1079
+
1080
+ const wrapper = mount (
1081
+ < FormRenderer
1082
+ FormTemplate = { ( props ) => < FormTemplate { ...props } /> }
1083
+ componentMapper = { {
1084
+ [ componentTypes . TEXT_FIELD ] : TextField
1085
+ } }
1086
+ schema = { schema }
1087
+ onSubmit = { ( values ) => onSubmit ( values ) }
1088
+ clearedValue = "BlaBlaBla"
1089
+ />
1090
+ ) ;
1091
+
1092
+ wrapper
1093
+ . find ( 'input' )
1094
+ . first ( )
1095
+ . simulate ( 'change' , { target : { value : 'show' } } ) ;
1096
+ wrapper . update ( ) ;
1097
+ wrapper
1098
+ . find ( 'input' )
1099
+ . last ( )
1100
+ . simulate ( 'change' , { target : { value : 'foovalue' } } ) ;
1101
+ wrapper . update ( ) ;
1102
+
1103
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1104
+
1105
+ expect ( onSubmit ) . toHaveBeenCalledWith ( { foo : 'show' , unmnounted : 'foovalue' } ) ;
1106
+ onSubmit . mockClear ( ) ;
1107
+
1108
+ wrapper
1109
+ . find ( 'input' )
1110
+ . first ( )
1111
+ . simulate ( 'change' , { target : { value : 'barrr' } } ) ;
1112
+ wrapper . update ( ) ;
1113
+
1114
+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1115
+
1116
+ expect ( onSubmit ) . toHaveBeenCalledWith ( { foo : 'barrr' , unmnounted : 'BlaBlaBla' } ) ;
1117
+ } ) ;
995
1118
} ) ;
996
1119
997
1120
describe ( '#initializeOnMount' , ( ) => {
0 commit comments