5
5
// does not automatically invoke the function
6
6
// and it can take arguments.
7
7
8
- import { renderHook } from '@testing-library/react-hooks' ;
8
+ import { act , renderHook } from '@testing-library/react-hooks' ;
9
9
import useAsyncFn , { AsyncState } from '../useAsyncFn' ;
10
10
11
11
type AdderFn = ( a : number , b : number ) => Promise < number > ;
@@ -17,27 +17,26 @@ describe('useAsyncFn', () => {
17
17
18
18
describe ( 'the callback can be awaited and return the value' , ( ) => {
19
19
let hook ;
20
- let callCount = 0 ;
21
20
const adder = async ( a : number , b : number ) : Promise < number > => {
22
- callCount ++ ;
23
21
return a + b ;
24
22
} ;
25
23
26
24
beforeEach ( ( ) => {
27
25
// NOTE: renderHook isn't good at inferring array types
28
26
hook = renderHook < { fn : AdderFn } , [ AsyncState < number > , AdderFn ] > ( ( { fn } ) => useAsyncFn ( fn ) , {
29
- initialProps : {
30
- fn : adder ,
31
- } ,
27
+ initialProps : { fn : adder } ,
32
28
} ) ;
33
29
} ) ;
34
30
35
31
it ( 'awaits the result' , async ( ) => {
36
32
expect . assertions ( 3 ) ;
37
33
38
- const [ s , callback ] = hook . result . current ;
34
+ const [ , callback ] = hook . result . current ;
35
+ let result ;
39
36
40
- const result = await callback ( 5 , 7 ) ;
37
+ await act ( async ( ) => {
38
+ result = await callback ( 5 , 7 ) ;
39
+ } ) ;
41
40
42
41
expect ( result ) . toEqual ( 12 ) ;
43
42
@@ -78,13 +77,15 @@ describe('useAsyncFn', () => {
78
77
it ( 'resolves a value derived from args' , async ( ) => {
79
78
expect . assertions ( 4 ) ;
80
79
81
- const [ s , callback ] = hook . result . current ;
80
+ const [ , callback ] = hook . result . current ;
82
81
83
- callback ( 2 , 7 ) ;
82
+ act ( ( ) => {
83
+ callback ( 2 , 7 ) ;
84
+ } ) ;
84
85
hook . rerender ( { fn : adder } ) ;
85
86
await hook . waitForNextUpdate ( ) ;
86
87
87
- const [ state , c ] = hook . result . current ;
88
+ const [ state ] = hook . result . current ;
88
89
89
90
expect ( callCount ) . toEqual ( 1 ) ;
90
91
expect ( state . loading ) . toEqual ( false ) ;
0 commit comments