File tree Expand file tree Collapse file tree 5 files changed +6305
-5762
lines changed Expand file tree Collapse file tree 5 files changed +6305
-5762
lines changed Original file line number Diff line number Diff line change
1
+ import { renderHook , act } from '@testing-library/react-hooks'
2
+ import { useAsync } from '../src'
3
+
4
+ test ( 'useAsync: resolve' , async ( ) => {
5
+ const delay = time => new Promise ( res => setTimeout ( ( ) => res ( ) , time ) )
6
+ const { result } = renderHook ( ( ) => useAsync ( ( ) => delay ( 100 ) ) )
7
+ expect ( result . current . loading ) . toBe ( false )
8
+ await act ( ( ) => result . current . f ( ) )
9
+ expect ( result . current . loading ) . toBe ( false )
10
+ } )
11
+
12
+ test ( 'useAsync: reject' , async ( ) => {
13
+ const delay = time => new Promise ( ( res , rej ) => setTimeout ( ( ) => rej ( 'error' ) , time ) )
14
+ const { result } = renderHook ( ( ) => useAsync ( ( ) => delay ( 100 ) ) )
15
+ expect ( result . current . loading ) . toBe ( false )
16
+ try {
17
+ await act ( ( ) => result . current . f ( ) )
18
+ } catch ( error ) {
19
+ expect ( result . current . loading ) . toBe ( false )
20
+ expect ( result . current . error ) . toBe ( 'error' )
21
+ }
22
+ } )
Original file line number Diff line number Diff line change 49
49
"@storybook/addons" : " ^5.1.9" ,
50
50
"@storybook/react" : " ^5.1.9" ,
51
51
"@storybook/storybook-deployer" : " ^2.8.1" ,
52
- "@testing-library/react-hooks" : " ^1.1.0 " ,
52
+ "@testing-library/react-hooks" : " ^3.4.1 " ,
53
53
"@types/jest" : " ^23.3.10" ,
54
54
"babel-core" : " ^7.0.0-bridge.0" ,
55
55
"babel-eslint" : " ^10.0.1" ,
Original file line number Diff line number Diff line change
1
+ /* eslint-disable no-underscore-dangle */
2
+ import { useState } from 'react'
3
+
4
+ const useAsync = ( f = ( ) => { } ) => {
5
+ const [ loading , setLoading ] = useState ( false )
6
+ const [ error , setError ] = useState ( undefined )
7
+ const _f = async ( ...args ) => {
8
+ setLoading ( true )
9
+ try {
10
+ const res = await f ( ...args )
11
+ setLoading ( false )
12
+ setError ( undefined )
13
+ return res
14
+ } catch ( e ) {
15
+ setLoading ( false )
16
+ setError ( e )
17
+ throw e
18
+ }
19
+ }
20
+ return {
21
+ f : _f ,
22
+ error,
23
+ loading,
24
+ }
25
+ }
26
+
27
+
28
+ export default useAsync
Original file line number Diff line number Diff line change @@ -35,3 +35,5 @@ export { default as useUndo } from './hooks/useUndo'
35
35
export { default as useStateCallback } from './hooks/useStateCallback'
36
36
37
37
export { useShallowEqualEffect , useDeepEqualEffect } from './hooks/useEqualEffect'
38
+
39
+ export { default as useAsync } from './hooks/useAsync'
You can’t perform that action at this time.
0 commit comments