@@ -911,3 +911,65 @@ describe('useAJVForm should properly set errors programmatically using setErrors
911
911
expect ( result . eventUrl . isRequired ) . toEqual ( true ) ;
912
912
} ) ;
913
913
} ) ;
914
+
915
+ describe ( 'useAJVForm with error handling' , ( ) => {
916
+ it ( 'should set initial errors from options' , ( ) => {
917
+ const initialData = { title : '' } ;
918
+ const schema : JSONSchemaType < { title : string } > = {
919
+ type : 'object' ,
920
+ required : [ 'title' ] ,
921
+ properties : {
922
+ title : { type : 'string' } ,
923
+ } ,
924
+ } ;
925
+
926
+ const errors = [
927
+ {
928
+ instancePath : '/title' ,
929
+ keyword : 'required' ,
930
+ params : { missingProperty : 'title' } ,
931
+ schemaPath : '#/required' ,
932
+ } ,
933
+ ] ;
934
+
935
+ const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema , { errors } ) ) ;
936
+
937
+ expect ( result . current . state . title . error ) . toBe ( 'title is required.' ) ;
938
+ expect ( result . current . isValid ) . toBe ( false ) ;
939
+ } ) ;
940
+
941
+ it ( 'should update errors when options.errors changes' , ( ) => {
942
+ const initialData = { title : '' } ;
943
+ const schema : JSONSchemaType < { title : string } > = {
944
+ type : 'object' ,
945
+ required : [ 'title' ] ,
946
+ properties : {
947
+ title : { type : 'string' } ,
948
+ } ,
949
+ } ;
950
+
951
+ const { result, rerender } = renderHook (
952
+ ( props ) => useAJVForm ( initialData , schema , props ) ,
953
+ { initialProps : { } } ,
954
+ ) ;
955
+
956
+ expect ( result . current . state . title . error ) . toBe ( '' ) ;
957
+
958
+ rerender ( {
959
+ errors : [
960
+ {
961
+ instancePath : '/title' ,
962
+ keyword : 'minLength' ,
963
+ params : { limit : 3 } ,
964
+ schemaPath : '#/properties/title/minLength' ,
965
+ } ,
966
+ ] ,
967
+ } ) ;
968
+
969
+ expect ( result . current . state . title . error ) . toBe (
970
+ 'Should be at least 3 characters long.' ,
971
+ ) ;
972
+
973
+ expect ( result . current . isValid ) . toBe ( false ) ;
974
+ } ) ;
975
+ } ) ;
0 commit comments