File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ interface StarwarsHero {
6
6
}
7
7
8
8
export const generateMockResponseData = ( amount : number = 5 ) : StarwarsHero [ ] =>
9
+ // @ts -ignore
9
10
[ ...Array ( amount ) . keys ( ) ] . map ( n => ( {
10
11
id : n + 1 ,
11
12
name : `Starwars Hero ${ n + 1 } ` ,
@@ -127,4 +128,31 @@ describe('useAync', () => {
127
128
expect ( onSuccess ) . not . toHaveBeenCalled ( ) ;
128
129
expect ( onError ) . toHaveBeenCalled ( ) ;
129
130
} ) ;
131
+
132
+ it ( 'should set error detail for error thrown synchronously (like when preparing/formatting a payload)' , async ( ) => {
133
+ const onSuccess = jest . fn ( ) ;
134
+ const onError = jest . fn ( ) ;
135
+
136
+ const { result, waitForNextUpdate } = renderHook ( ( ) =>
137
+ useAsync (
138
+ ( ) => {
139
+ throw new Error ( 'something went wrong' ) ;
140
+ } ,
141
+ [ ] ,
142
+ {
143
+ onSuccess : ( ) => onSuccess ( ) ,
144
+ onError : ( ) => onError ( ) ,
145
+ }
146
+ )
147
+ ) ;
148
+
149
+ await waitForNextUpdate ( ) ;
150
+
151
+ expect ( result . current . error ) . toBeDefined ( ) ;
152
+ expect ( result . current . error ! . message ) . toBe ( 'something went wrong' ) ;
153
+ expect ( result . current . loading ) . toBe ( false ) ;
154
+ expect ( result . current . result ) . toBeUndefined ( ) ;
155
+ expect ( onSuccess ) . not . toHaveBeenCalled ( ) ;
156
+ expect ( onError ) . toHaveBeenCalled ( ) ;
157
+ } ) ;
130
158
} ) ;
You can’t perform that action at this time.
0 commit comments