@@ -992,6 +992,129 @@ describe('renderForm function', () => {
992992 wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
993993 expect ( onSubmit ) . toHaveBeenCalledWith ( { unmnounted : undefined , foo : 'barrr' } ) ;
994994 } ) ;
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+ } ) ;
9951118 } ) ;
9961119
9971120 describe ( '#initializeOnMount' , ( ) => {
@@ -1016,7 +1139,7 @@ describe('renderForm function', () => {
10161139 component : componentTypes . TEXT_FIELD ,
10171140 name : INITIALIZED_FIELD ,
10181141 initializeOnMount,
1019- initialValue,
1142+ ... ( initialValue ? { initialValue } : { } ) ,
10201143 condition : {
10211144 when : SHOWER_FIELD ,
10221145 is : SHOW_VALUE
@@ -1183,6 +1306,138 @@ describe('renderForm function', () => {
11831306 expect ( onSubmit ) . toHaveBeenCalledWith ( expect . objectContaining ( { [ INITIALIZED_FIELD ] : SCHEMA_INITIAL_VALUE , [ SHOWER_FIELD ] : SHOW_VALUE } ) ) ;
11841307 onSubmit . mockReset ( ) ;
11851308 } ) ;
1309+
1310+ it ( 'should set false value in initializeOnMount' , ( ) => {
1311+ const schema = {
1312+ fields : [
1313+ {
1314+ component : componentTypes . TEXT_FIELD ,
1315+ name : 'input'
1316+ } ,
1317+ {
1318+ component : componentTypes . TEXT_FIELD ,
1319+ name : 'unmounted' ,
1320+ initialValue : false ,
1321+ initializeOnMount : true ,
1322+ condition : {
1323+ when : 'input' ,
1324+ is : 'show_false'
1325+ }
1326+ } ,
1327+ {
1328+ component : componentTypes . TEXT_FIELD ,
1329+ name : 'unmounted' ,
1330+ initialValue : true ,
1331+ initializeOnMount : true ,
1332+ condition : {
1333+ when : 'input' ,
1334+ is : 'show_true'
1335+ }
1336+ }
1337+ ]
1338+ } ;
1339+
1340+ const onSubmit = jest . fn ( ) ;
1341+
1342+ const wrapper = mount (
1343+ < FormRenderer
1344+ FormTemplate = { ( props ) => < FormTemplate { ...props } /> }
1345+ componentMapper = { {
1346+ [ componentTypes . TEXT_FIELD ] : TextField
1347+ } }
1348+ schema = { schema }
1349+ onSubmit = { onSubmit }
1350+ />
1351+ ) ;
1352+
1353+ wrapper
1354+ . find ( 'input' )
1355+ . first ( )
1356+ . simulate ( 'change' , { target : { value : 'show_true' } } ) ;
1357+ wrapper . update ( ) ;
1358+
1359+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1360+
1361+ expect ( onSubmit ) . toHaveBeenCalledWith ( { input : 'show_true' , unmounted : true } , expect . any ( Object ) , expect . any ( Function ) ) ;
1362+ onSubmit . mockClear ( ) ;
1363+
1364+ wrapper
1365+ . find ( 'input' )
1366+ . first ( )
1367+ . simulate ( 'change' , { target : { value : 'show_false' } } ) ;
1368+ wrapper . update ( ) ;
1369+
1370+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1371+ wrapper . update ( ) ;
1372+
1373+ expect ( onSubmit ) . toHaveBeenCalledWith ( { input : 'show_false' , unmounted : false } , expect . any ( Object ) , expect . any ( Function ) ) ;
1374+ } ) ;
1375+
1376+ it ( 'should set unefined value in initializeOnMount' , ( ) => {
1377+ const schema = {
1378+ fields : [
1379+ {
1380+ component : componentTypes . TEXT_FIELD ,
1381+ name : 'input'
1382+ } ,
1383+ {
1384+ component : componentTypes . TEXT_FIELD ,
1385+ name : 'unmounted' ,
1386+ initialValue : undefined ,
1387+ initializeOnMount : true ,
1388+ condition : {
1389+ when : 'input' ,
1390+ is : 'show_undef'
1391+ }
1392+ } ,
1393+ {
1394+ component : componentTypes . TEXT_FIELD ,
1395+ name : 'unmounted' ,
1396+ initialValue : true ,
1397+ initializeOnMount : true ,
1398+ condition : {
1399+ when : 'input' ,
1400+ is : 'show_true'
1401+ }
1402+ }
1403+ ]
1404+ } ;
1405+
1406+ const onSubmit = jest . fn ( ) ;
1407+
1408+ const wrapper = mount (
1409+ < FormRenderer
1410+ FormTemplate = { ( props ) => < FormTemplate { ...props } /> }
1411+ componentMapper = { {
1412+ [ componentTypes . TEXT_FIELD ] : TextField
1413+ } }
1414+ schema = { schema }
1415+ onSubmit = { onSubmit }
1416+ />
1417+ ) ;
1418+
1419+ wrapper
1420+ . find ( 'input' )
1421+ . first ( )
1422+ . simulate ( 'change' , { target : { value : 'show_true' } } ) ;
1423+ wrapper . update ( ) ;
1424+
1425+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1426+
1427+ expect ( onSubmit ) . toHaveBeenCalledWith ( { input : 'show_true' , unmounted : true } , expect . any ( Object ) , expect . any ( Function ) ) ;
1428+ onSubmit . mockClear ( ) ;
1429+
1430+ wrapper
1431+ . find ( 'input' )
1432+ . first ( )
1433+ . simulate ( 'change' , { target : { value : 'show_undef' } } ) ;
1434+ wrapper . update ( ) ;
1435+
1436+ wrapper . find ( 'form' ) . simulate ( 'submit' ) ;
1437+ wrapper . update ( ) ;
1438+
1439+ expect ( onSubmit ) . toHaveBeenCalledWith ( { input : 'show_undef' , unmounted : undefined } , expect . any ( Object ) , expect . any ( Function ) ) ;
1440+ } ) ;
11861441 } ) ;
11871442
11881443 it ( 'should use actionMapper' , ( ) => {
0 commit comments