@@ -230,4 +230,46 @@ it('should not crash when server returns 500 error', async () => {
230230
231231 // onError callback should have been called
232232 expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
233+ } ) ;
234+
235+ it ( 'should not throw unhandled promise rejection when server returns 500 error' , async ( ) => {
236+ const onChange = jest . fn ( ) ;
237+ const onError = jest . fn ( ) ;
238+ const unhandledRejectionHandler = jest . fn ( ) ;
239+ const { flagsmith, initConfig, mockFetch } = getFlagsmith ( {
240+ onChange,
241+ onError,
242+ } ) ;
243+ window . addEventListener ( 'unhandledrejection' , unhandledRejectionHandler ) ;
244+
245+ mockFetch . mockImplementationOnce ( ( ) =>
246+ Promise . resolve ( {
247+ status : 500 ,
248+ headers : { get : ( ) => null } ,
249+ text : ( ) => Promise . resolve ( 'API Response: 500' )
250+ } )
251+ ) ;
252+
253+ expect ( ( ) => {
254+ render (
255+ < FlagsmithProvider flagsmith = { flagsmith } options = { initConfig } >
256+ < FlagsmithPage />
257+ </ FlagsmithProvider >
258+ ) ;
259+ } ) . not . toThrow ( ) ;
260+
261+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 ) ;
262+
263+ await waitFor ( ( ) => {
264+ // Loading should complete with error
265+ const loadingState = JSON . parse ( screen . getByTestId ( "loading-state" ) . innerHTML ) ;
266+ expect ( loadingState . isLoading ) . toBe ( false ) ;
267+ expect ( loadingState . isFetching ) . toBe ( false ) ;
268+ expect ( loadingState . error ) . toBeTruthy ( ) ;
269+ } ) ;
270+
271+ // onError callback should have been called
272+ expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
273+ window . removeEventListener ( 'unhandledrejection' , unhandledRejectionHandler ) ;
274+
233275} ) ;
0 commit comments