@@ -70,37 +70,45 @@ describe('useField', () => {
70
70
expect ( unregisterSpy ) . toHaveBeenCalledWith ( unregisterArguments ) ;
71
71
} ) ;
72
72
73
- it ( 'should set correct value on input type text' , ( ) => {
73
+ it ( 'should set correct value on input type text' , async ( ) => {
74
74
const wrapper = mount ( < DummyComponent subscriberProps = { { name : 'spy' } } managerApi = { managerApi } /> ) ;
75
75
const input = wrapper . find ( 'input' ) ;
76
- input . simulate ( 'change' , { target : { value : 'foo' } } ) ;
76
+ await act ( async ( ) => {
77
+ input . simulate ( 'change' , { target : { value : 'foo' } } ) ;
78
+ } ) ;
77
79
wrapper . update ( ) ;
78
80
expect ( wrapper . find ( SpyComponent ) . prop ( 'value' ) ) . toEqual ( 'foo' ) ;
79
81
} ) ;
80
82
81
- it ( 'should set correct value on input type checkbox' , ( ) => {
83
+ it ( 'should set correct value on input type checkbox' , async ( ) => {
82
84
const wrapper = mount ( < DummyComponent subscriberProps = { { name : 'spy' , type : 'checkbox' } } managerApi = { managerApi } /> ) ;
83
85
const input = wrapper . find ( 'input' ) ;
84
- input . simulate ( 'change' , { target : { checked : true , type : 'checkbox' } } ) ;
86
+ await act ( async ( ) => {
87
+ input . simulate ( 'change' , { target : { checked : true , type : 'checkbox' } } ) ;
88
+ } ) ;
85
89
wrapper . update ( ) ;
86
90
expect ( wrapper . find ( SpyComponent ) . prop ( 'checked' ) ) . toEqual ( true ) ;
87
91
} ) ;
88
92
89
- it ( 'should set correct array value' , ( ) => {
93
+ it ( 'should set correct array value' , async ( ) => {
90
94
const wrapper = mount ( < DummyComponent subscriberProps = { { fakeComponent : true , name : 'spy' , changeValue : [ ] } } managerApi = { managerApi } /> ) ;
91
95
const input = wrapper . find ( 'button#fake-change' ) ;
92
- input . simulate ( 'click' ) ;
96
+ await act ( async ( ) => {
97
+ input . simulate ( 'click' ) ;
98
+ } ) ;
93
99
wrapper . update ( ) ;
94
100
expect ( wrapper . find ( NonInputSpyComponent ) . prop ( 'value' ) ) . toEqual ( [ ] ) ;
95
101
} ) ;
96
102
97
- it ( 'should set correct on non event object value' , ( ) => {
103
+ it ( 'should set correct on non event object value' , async ( ) => {
98
104
const nonEventObject = { value : 1 , label : 'bar' } ;
99
105
const wrapper = mount (
100
106
< DummyComponent subscriberProps = { { fakeComponent : true , name : 'spy' , changeValue : nonEventObject } } managerApi = { managerApi } />
101
107
) ;
102
108
const input = wrapper . find ( 'button#fake-change' ) ;
103
- input . simulate ( 'click' ) ;
109
+ await act ( async ( ) => {
110
+ input . simulate ( 'click' ) ;
111
+ } ) ;
104
112
wrapper . update ( ) ;
105
113
expect ( wrapper . find ( NonInputSpyComponent ) . prop ( 'value' ) ) . toEqual ( nonEventObject ) ;
106
114
} ) ;
@@ -431,38 +439,44 @@ describe('useField', () => {
431
439
name : 'async-validate' ,
432
440
validate : asyncValidator
433
441
} ;
442
+
434
443
const wrapper = mount ( < DummyComponent managerApi = { managerApi } subscriberProps = { subscriberProps } /> ) ;
435
- const spy = wrapper . find ( SpyComponent ) ;
436
444
const input = wrapper . find ( 'input' ) ;
437
- expect ( spy . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : true , valid : true } ) ) ;
445
+ expect ( wrapper . find ( SpyComponent ) . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : true , valid : true } ) ) ;
438
446
439
447
await act ( async ( ) => {
440
448
jest . runAllTimers ( ) ; // skip initial validation
441
449
} ) ;
450
+ wrapper . update ( ) ;
442
451
443
- expect ( spy . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : false , valid : true } ) ) ;
452
+ expect ( wrapper . find ( SpyComponent ) . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : false , valid : true } ) ) ;
444
453
445
- input . simulate ( 'change' , { target : { value : 'foo' } } ) ;
454
+ await act ( ( ) => {
455
+ input . simulate ( 'change' , { target : { value : 'foo' } } ) ;
456
+ } ) ;
446
457
/**
447
458
* All validations are pending
448
459
*/
449
460
await act ( async ( ) => {
450
461
jest . advanceTimersByTime ( 10 ) ;
451
462
} ) ;
463
+ wrapper . update ( ) ;
452
464
expect ( wrapper . find ( SpyComponent ) . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : true , valid : true } ) ) ;
453
465
/**
454
466
* Second faster async validation has finished
455
467
*/
456
468
await act ( async ( ) => {
457
469
jest . advanceTimersByTime ( 290 ) ;
458
470
} ) ;
471
+ wrapper . update ( ) ;
459
472
expect ( wrapper . find ( SpyComponent ) . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : true , valid : true } ) ) ;
460
473
/**
461
474
* First slow async validation has finished
462
475
*/
463
476
await act ( async ( ) => {
464
477
jest . advanceTimersByTime ( 200 ) ;
465
478
} ) ;
479
+ wrapper . update ( ) ;
466
480
expect ( wrapper . find ( SpyComponent ) . prop ( 'meta' ) ) . toEqual ( expect . objectContaining ( { validating : false , valid : true } ) ) ;
467
481
} ) ;
468
482
} ) ;
0 commit comments