File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -193,7 +193,7 @@ const useAJVForm = <T extends Record<string, any>>(
193
193
194
194
const isDirty = useMemo ( ( ) => {
195
195
return Object . keys ( state ) . some (
196
- ( key ) => state [ key ] . value !== initialStateRef . current [ key ] . value ,
196
+ ( key ) => state [ key ] . value !== initialStateRef . current [ key ] ? .value ,
197
197
) ;
198
198
} , [ state ] ) ;
199
199
Original file line number Diff line number Diff line change @@ -52,6 +52,39 @@ describe('useAJVForm', () => {
52
52
expect ( result . current . state . title . value ) . toBe ( 'New Title' ) ;
53
53
} ) ;
54
54
55
+ it ( 'handles onChange with fields not in initial data' , ( ) => {
56
+ const initialData = { name : '' } ;
57
+ const schema : JSONSchemaType < { name : string } > = {
58
+ type : 'object' ,
59
+ required : [ 'name' ] ,
60
+ properties : {
61
+ name : {
62
+ type : 'string' ,
63
+ minLength : 1 ,
64
+ maxLength : 65 ,
65
+ errorMessage : {
66
+ maxLength : 'Name cannot be greater than 65 characters.' ,
67
+ } ,
68
+ } ,
69
+ } ,
70
+ } ;
71
+
72
+ const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema ) ) ;
73
+
74
+ // Accidentally including an additional field in update
75
+ const payload = { name : 'Dog' , id : 1 } ;
76
+ result . current . set ( payload ) ;
77
+
78
+ expect ( result . current . state . name . value ) . toBe ( 'Dog' ) ;
79
+ expect ( result . current . isDirty ) . toBeTruthy ( ) ;
80
+
81
+ // Clearing existing field has earlier caused hook to throw
82
+ result . current . set ( { name : '' } ) ;
83
+
84
+ expect ( result . current . state . name . value ) . toBe ( '' ) ;
85
+ expect ( result . current . isDirty ) . toBeTruthy ( ) ;
86
+ } ) ;
87
+
55
88
it ( 'handles onBlur event with validation correctly' , ( ) => {
56
89
const initialData = { title : '' } ;
57
90
const schema : JSONSchemaType < { title : string } > = {
You can’t perform that action at this time.
0 commit comments