1
+ import { CustomError } from "@umami/utils" ;
2
+
1
3
import { useAsyncActionHandler } from "./useAsyncActionHandler" ;
2
4
import { act , mockToast , renderHook , waitFor } from "../testUtils" ;
3
5
@@ -144,12 +146,12 @@ describe("useAsyncActionHandler", () => {
144
146
describe ( "handleAsyncActionUnsafe" , ( ) => {
145
147
it ( "returns the result of the computation" , async ( ) => {
146
148
const view = fixture ( ) ;
147
-
148
149
const result = await act ( ( ) =>
149
150
view . result . current . handleAsyncActionUnsafe ( ( ) => Promise . resolve ( 42 ) )
150
151
) ;
151
152
152
153
expect ( result ) . toBe ( 42 ) ;
154
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 0 ) ;
153
155
} ) ;
154
156
155
157
it ( "throws when the computation fails" , async ( ) => {
@@ -160,6 +162,41 @@ describe("useAsyncActionHandler", () => {
160
162
view . result . current . handleAsyncActionUnsafe ( ( ) => Promise . reject ( new Error ( "test error" ) ) )
161
163
)
162
164
) . rejects . toThrow ( "test error" ) ;
165
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 2 ) ;
166
+ } ) ;
167
+
168
+ it ( "Unsafe propagates the error and shows the toast once on first handling" , async ( ) => {
169
+ const view = fixture ( ) ;
170
+
171
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 0 ) ;
172
+
173
+ const error : any = new CustomError ( "test nested error handling" ) ;
174
+ await expect (
175
+ act ( ( ) => view . result . current . handleAsyncActionUnsafe ( ( ) => Promise . reject ( error ) ) )
176
+ ) . rejects . toThrow ( "test nested error handling" ) ;
177
+ // check that error.processed is set to true
178
+ expect ( error . processed ) . toBe ( true ) ;
179
+ expect ( mockToast ) . toHaveBeenCalledWith ( {
180
+ description : "test nested error handling" ,
181
+ status : "error" ,
182
+ isClosable : true ,
183
+ } ) ;
184
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 2 ) ;
185
+ } ) ;
186
+
187
+ it ( "Unsafe propagates the error and shows no toast on second handling" , async ( ) => {
188
+ const view = fixture ( ) ;
189
+
190
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 0 ) ;
191
+
192
+ const error : any = new CustomError ( "test nested error handling" ) ;
193
+ error . processed = true ;
194
+ await expect (
195
+ act ( ( ) => view . result . current . handleAsyncActionUnsafe ( ( ) => Promise . reject ( error ) ) )
196
+ ) . rejects . toThrow ( "test nested error handling" ) ;
197
+ // check that error.processed is still true
198
+ expect ( error . processed ) . toBe ( true ) ;
199
+ expect ( mockToast ) . toHaveBeenCalledTimes ( 0 ) ;
163
200
} ) ;
164
201
} ) ;
165
202
} ) ;
0 commit comments