@@ -16,6 +16,7 @@ import {
1616 describeRepeated ,
1717 submitForm ,
1818} from './test_utils' ;
19+ import widgetsSchema from './widgets_schema.json' ;
1920
2021describeRepeated ( 'Form common' , ( createFormComponent ) => {
2122 let sandbox ;
@@ -3912,4 +3913,98 @@ describe('Form omitExtraData and liveOmit', () => {
39123913 expect ( node . querySelector ( '#root_1' ) . getAttribute ( 'value' ) ) . to . eq ( 'test' ) ;
39133914 } ) ;
39143915 } ) ;
3916+
3917+ describe ( 'Calling reset from ref object' , ( ) => {
3918+ it ( 'Reset API test' , ( ) => {
3919+ const schema = {
3920+ title : 'Test form' ,
3921+ type : 'string' ,
3922+ } ;
3923+ const formRef = React . createRef ( ) ;
3924+ const props = {
3925+ ref : formRef ,
3926+ schema,
3927+ } ;
3928+ const { node } = createFormComponent ( props ) ;
3929+ expect ( formRef . current . reset ) . to . exist ;
3930+ expect ( node . querySelector ( 'input' ) ) . to . exist ;
3931+ Simulate . change ( node . querySelector ( 'input' ) , { target : { value : 'Some Value' } } ) ;
3932+ formRef . current . reset ( ) ;
3933+ expect ( node . querySelector ( 'input' ) . getAttribute ( 'value' ) ) . to . eq ( '' ) ;
3934+ } ) ;
3935+
3936+ it ( 'Clear errors' , ( ) => {
3937+ const schema = {
3938+ title : 'Test form' ,
3939+ type : 'number' ,
3940+ } ;
3941+ const formRef = React . createRef ( ) ;
3942+ const props = {
3943+ ref : formRef ,
3944+ schema,
3945+ } ;
3946+ const { node, comp } = createFormComponent ( props ) ;
3947+ expect ( formRef . current . reset ) . to . exist ;
3948+ expect ( node . querySelector ( 'input' ) ) . to . exist ;
3949+ Simulate . change ( node . querySelector ( 'input' ) , { target : { value : 'Some Value' } } ) ;
3950+ expect ( comp . state . errors ) . to . have . length ( 0 ) ;
3951+ Simulate . submit ( node ) ;
3952+ expect ( comp . state . errors ) . to . have . length ( 1 ) ;
3953+ expect ( node . querySelector ( '.errors' ) ) . to . exist ;
3954+ formRef . current . reset ( ) ;
3955+ expect ( node . querySelector ( '.errors' ) ) . not . to . exist ;
3956+ expect ( node . querySelector ( 'input' ) . getAttribute ( 'value' ) ) . to . eq ( '' ) ;
3957+ expect ( comp . state . errors ) . to . have . length ( 0 ) ;
3958+ } ) ;
3959+
3960+ it ( 'Reset button test with default value' , ( ) => {
3961+ const schemaWithDefault = {
3962+ title : 'Test form' ,
3963+ type : 'string' ,
3964+ default : 'Some-Value' ,
3965+ } ;
3966+ const formRef = React . createRef ( ) ;
3967+ const props = {
3968+ ref : formRef ,
3969+ schema : schemaWithDefault ,
3970+ } ;
3971+ const { node } = createFormComponent ( props ) ;
3972+ const input = node . querySelector ( 'input' ) ;
3973+ expect ( formRef . current . reset ) . to . exist ;
3974+ expect ( input ) . to . exist ;
3975+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( 'Some-Value' ) ;
3976+ formRef . current . reset ( ) ;
3977+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( 'Some-Value' ) ;
3978+ Simulate . change ( input , { target : { value : 'Changed value' } } ) ;
3979+ formRef . current . reset ( ) ;
3980+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( 'Some-Value' ) ;
3981+ } ) ;
3982+
3983+ it ( 'Reset button test with complex schema' , ( ) => {
3984+ const schema = widgetsSchema ;
3985+ const formRef = React . createRef ( ) ;
3986+ const props = {
3987+ ref : formRef ,
3988+ schema,
3989+ } ;
3990+ const { node } = createFormComponent ( props ) ;
3991+ const checkbox = node . querySelector ( 'input[type="checkbox"]' ) ;
3992+ const input = node . querySelector ( 'input[type="text"]' ) ;
3993+ expect ( formRef . current . reset ) . to . exist ;
3994+ expect ( checkbox ) . to . exist ;
3995+ expect ( input ) . to . exist ;
3996+ expect ( checkbox . checked ) . to . eq ( true ) ;
3997+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( '' ) ;
3998+ formRef . current . reset ( ) ;
3999+ expect ( checkbox . checked ) . to . eq ( true ) ;
4000+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( '' ) ;
4001+ Simulate . change ( checkbox , { target : { checked : false } } ) ;
4002+ Simulate . change ( input , { target : { value : 'Changed value' } } ) ;
4003+ expect ( checkbox . checked ) . to . eq ( false ) ;
4004+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( 'Changed value' ) ;
4005+ formRef . current . reset ( ) ;
4006+ expect ( input . getAttribute ( 'value' ) ) . to . eq ( '' ) ;
4007+ expect ( checkbox . checked ) . to . eq ( true ) ;
4008+ } ) ;
4009+ } ) ;
39154010} ) ;
0 commit comments