@@ -33,15 +33,15 @@ describe('useAync', () => {
33
33
34
34
beforeEach ( ( ) => {
35
35
fetch . resetMocks ( ) ;
36
-
37
- fetch . mockResponseOnce ( JSON . stringify ( fakeResults ) ) ;
38
36
} ) ;
39
37
40
38
it ( 'should have a useAsync hook' , ( ) => {
41
39
expect ( useAsync ) . toBeDefined ( ) ;
42
40
} ) ;
43
41
44
42
it ( 'should set loading flag when request is initially made' , ( ) => {
43
+ fetch . mockResponseOnce ( JSON . stringify ( fakeResults ) ) ;
44
+
45
45
const { result } = renderHook <
46
46
StarwarsHeroArgs ,
47
47
UseAsyncReturn < StarwarsHero [ ] >
@@ -55,6 +55,8 @@ describe('useAync', () => {
55
55
} ) ;
56
56
57
57
it ( 'should resolve a successful request' , async ( ) => {
58
+ fetch . mockResponseOnce ( JSON . stringify ( fakeResults ) ) ;
59
+
58
60
const { result, waitForNextUpdate } = renderHook <
59
61
StarwarsHeroArgs ,
60
62
UseAsyncReturn < StarwarsHero [ ] >
@@ -68,4 +70,22 @@ describe('useAync', () => {
68
70
expect ( result . current . loading ) . toBe ( false ) ;
69
71
expect ( result . current . error ) . toBeUndefined ( ) ;
70
72
} ) ;
73
+
74
+ it ( 'should set error detail for unsuccessful request' , async ( ) => {
75
+ fetch . mockReject ( new Error ( 'something went wrong' ) ) ;
76
+
77
+ const { result, waitForNextUpdate } = renderHook <
78
+ StarwarsHeroArgs ,
79
+ UseAsyncReturn < StarwarsHero [ ] >
80
+ > ( p => useAsync < StarwarsHero [ ] , any > ( p . asyncFunction , [ ] ) , {
81
+ initialProps : { ...props } ,
82
+ } ) ;
83
+
84
+ await waitForNextUpdate ( ) ;
85
+
86
+ expect ( result . current . error ) . toBeDefined ( ) ;
87
+ expect ( result . current . error ! . message ) . toBe ( 'something went wrong' ) ;
88
+ expect ( result . current . loading ) . toBe ( false ) ;
89
+ expect ( result . current . result ) . toBeUndefined ( ) ;
90
+ } ) ;
71
91
} ) ;
0 commit comments