@@ -57,35 +57,59 @@ describe('useAync', () => {
57
57
it ( 'should resolve a successful request' , async ( ) => {
58
58
fetch . mockResponseOnce ( JSON . stringify ( fakeResults ) ) ;
59
59
60
+ const onSuccess = jest . fn ( ) ;
61
+ const onError = jest . fn ( ) ;
62
+
60
63
const { result, waitForNextUpdate } = renderHook <
61
64
StarwarsHeroArgs ,
62
65
UseAsyncReturn < StarwarsHero [ ] >
63
- > ( p => useAsync < StarwarsHero [ ] , any > ( p . asyncFunction , [ ] ) , {
64
- initialProps : { ...props } ,
65
- } ) ;
66
+ > (
67
+ p =>
68
+ useAsync < StarwarsHero [ ] , any > ( p . asyncFunction , [ ] , {
69
+ onSuccess : ( ) => onSuccess ( ) ,
70
+ onError : ( ) => onError ( ) ,
71
+ } ) ,
72
+ {
73
+ initialProps : { ...props } ,
74
+ }
75
+ ) ;
66
76
67
77
await waitForNextUpdate ( ) ;
68
78
69
79
expect ( result . current . result ) . toEqual ( fakeResults ) ;
70
80
expect ( result . current . loading ) . toBe ( false ) ;
71
81
expect ( result . current . error ) . toBeUndefined ( ) ;
82
+ expect ( onSuccess ) . toHaveBeenCalled ( ) ;
83
+ expect ( onError ) . not . toHaveBeenCalled ( ) ;
72
84
} ) ;
73
85
74
86
it ( 'should set error detail for unsuccessful request' , async ( ) => {
75
87
fetch . mockReject ( new Error ( 'something went wrong' ) ) ;
76
88
89
+ const onSuccess = jest . fn ( ) ;
90
+ const onError = jest . fn ( ) ;
91
+
77
92
const { result, waitForNextUpdate } = renderHook <
78
93
StarwarsHeroArgs ,
79
94
UseAsyncReturn < StarwarsHero [ ] >
80
- > ( p => useAsync < StarwarsHero [ ] , any > ( p . asyncFunction , [ ] ) , {
81
- initialProps : { ...props } ,
82
- } ) ;
95
+ > (
96
+ p =>
97
+ useAsync < StarwarsHero [ ] , any > ( p . asyncFunction , [ ] , {
98
+ onSuccess : ( ) => onSuccess ( ) ,
99
+ onError : ( ) => onError ( ) ,
100
+ } ) ,
101
+ {
102
+ initialProps : { ...props } ,
103
+ }
104
+ ) ;
83
105
84
106
await waitForNextUpdate ( ) ;
85
107
86
108
expect ( result . current . error ) . toBeDefined ( ) ;
87
109
expect ( result . current . error ! . message ) . toBe ( 'something went wrong' ) ;
88
110
expect ( result . current . loading ) . toBe ( false ) ;
89
111
expect ( result . current . result ) . toBeUndefined ( ) ;
112
+ expect ( onSuccess ) . not . toHaveBeenCalled ( ) ;
113
+ expect ( onError ) . toHaveBeenCalled ( ) ;
90
114
} ) ;
91
115
} ) ;
0 commit comments