@@ -102,9 +102,7 @@ describe('useAJVForm', () => {
102
102
103
103
const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema ) ) ;
104
104
105
- act ( ( ) => {
106
- result . current . set ( { title : 'New Title' } ) ;
107
- } ) ;
105
+ result . current . set ( { title : 'New Title' } ) ;
108
106
109
107
result . current . reset ( ) ;
110
108
@@ -367,3 +365,79 @@ describe('useAJVForm', () => {
367
365
expect ( result . current . isValid ) . toBe ( true ) ;
368
366
} ) ;
369
367
} ) ;
368
+
369
+ describe ( 'useAJVForm with multiple field updates' , ( ) => {
370
+ it ( 'updates multiple fields at once' , ( ) => {
371
+ const initialData = { title : '' , description : '' } ;
372
+ const schema : JSONSchemaType < { title : string ; description : string } > = {
373
+ type : 'object' ,
374
+ required : [ 'title' , 'description' ] ,
375
+ properties : {
376
+ title : { type : 'string' } ,
377
+ description : { type : 'string' } ,
378
+ } ,
379
+ } ;
380
+
381
+ const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema ) ) ;
382
+
383
+ result . current . set ( { title : 'New Title' , description : 'New Description' } ) ;
384
+
385
+ expect ( result . current . state . title . value ) . toBe ( 'New Title' ) ;
386
+ expect ( result . current . state . description . value ) . toBe ( 'New Description' ) ;
387
+ } ) ;
388
+
389
+ it ( 'preserves existing error messages when updating multiple fields' , ( ) => {
390
+ const initialData = { title : '' , description : '' } ;
391
+ const schema : JSONSchemaType < { title : string ; description : string } > = {
392
+ type : 'object' ,
393
+ required : [ 'title' , 'description' ] ,
394
+ properties : {
395
+ title : { type : 'string' , minLength : 3 } ,
396
+ description : { type : 'string' , minLength : 3 } ,
397
+ } ,
398
+ } ;
399
+
400
+ const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema ) ) ;
401
+
402
+ result . current . set ( { title : 'Ti' , description : 'De' } ) ;
403
+ result . current . onBlur ( 'title' ) ;
404
+ result . current . onBlur ( 'description' ) ;
405
+
406
+ expect ( result . current . state . title . error ) . not . toBe ( '' ) ;
407
+ expect ( result . current . state . description . error ) . not . toBe ( '' ) ;
408
+
409
+ expect ( result . current . state . title . error ) . toBe (
410
+ 'Should be at least 3 characters long.' ,
411
+ ) ;
412
+ expect ( result . current . state . description . error ) . toBe (
413
+ 'Should be at least 3 characters long.' ,
414
+ ) ;
415
+ } ) ;
416
+
417
+ it ( 'correctly updates isDirty when multiple fields are changed' , ( ) => {
418
+ const initialData = {
419
+ title : 'Original Title' ,
420
+ description : 'Original Description' ,
421
+ } ;
422
+ const schema : JSONSchemaType < { title : string ; description : string } > = {
423
+ type : 'object' ,
424
+ required : [ 'title' , 'description' ] ,
425
+ properties : {
426
+ title : { type : 'string' } ,
427
+ description : { type : 'string' } ,
428
+ } ,
429
+ } ;
430
+
431
+ const { result } = renderHook ( ( ) => useAJVForm ( initialData , schema ) ) ;
432
+
433
+ result . current . set ( {
434
+ title : 'Changed Title' ,
435
+ description : 'Changed Description' ,
436
+ } ) ;
437
+
438
+ expect ( result . current . isDirty ) . toBe ( true ) ;
439
+ } ) ;
440
+
441
+ // Additional tests can be written to cover other scenarios such as validation,
442
+ // resetting the form, handling errors, etc., when multiple fields are involved.
443
+ } ) ;
0 commit comments