1
1
import { HttpErrorResponse } from '@angular/common/http' ;
2
2
import * as SentryBrowser from '@sentry/browser' ;
3
3
import { Scope } from '@sentry/browser' ;
4
+ import type { Event } from '@sentry/types' ;
4
5
import * as SentryUtils from '@sentry/utils' ;
5
6
6
7
import { createErrorHandler , SentryErrorHandler } from '../src/errorhandler' ;
@@ -507,15 +508,6 @@ describe('SentryErrorHandler', () => {
507
508
expect ( captureExceptionSpy ) . toHaveBeenCalledWith ( 'something happened' , expect . any ( Function ) ) ;
508
509
} ) ;
509
510
510
- it ( 'handleError method shows report dialog' , ( ) => {
511
- const showReportDialogSpy = jest . spyOn ( SentryBrowser , 'showReportDialog' ) ;
512
-
513
- const errorHandler = createErrorHandler ( { showDialog : true } ) ;
514
- errorHandler . handleError ( new Error ( 'test' ) ) ;
515
-
516
- expect ( showReportDialogSpy ) . toBeCalledTimes ( 1 ) ;
517
- } ) ;
518
-
519
511
it ( 'extracts error with a custom extractor' , ( ) => {
520
512
const customExtractor = ( error : unknown ) => {
521
513
if ( typeof error === 'string' ) {
@@ -530,5 +522,41 @@ describe('SentryErrorHandler', () => {
530
522
expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
531
523
expect ( captureExceptionSpy ) . toHaveBeenCalledWith ( new Error ( 'custom error' ) , expect . any ( Function ) ) ;
532
524
} ) ;
525
+
526
+ describe ( 'opens the report dialog if `showDialog` is true' , ( ) => {
527
+ it ( 'by using SDK lifecycle hooks if available' , ( ) => {
528
+ const client = {
529
+ cb : ( _ : Event ) => { } ,
530
+ on : jest . fn ( ( _ , cb ) => {
531
+ client . cb = cb ;
532
+ } ) ,
533
+ } ;
534
+
535
+ // @ts -ignore this is a minmal hub, we're missing a few props but that's ok
536
+ jest . spyOn ( SentryBrowser , 'getCurrentHub' ) . mockImplementationOnce ( ( ) => {
537
+ return { getClient : ( ) => client } ;
538
+ } ) ;
539
+
540
+ const showReportDialogSpy = jest . spyOn ( SentryBrowser , 'showReportDialog' ) ;
541
+
542
+ const errorHandler = createErrorHandler ( { showDialog : true } ) ;
543
+ errorHandler . handleError ( new Error ( 'test' ) ) ;
544
+ expect ( client . on ) . toHaveBeenCalledWith ( 'afterSendEvent' , expect . any ( Function ) ) ;
545
+
546
+ // this simulates the afterSend hook being called
547
+ client . cb ( { } ) ;
548
+
549
+ expect ( showReportDialogSpy ) . toBeCalledTimes ( 1 ) ;
550
+ } ) ;
551
+
552
+ it ( 'by just calling `showReportDialog` if hooks are not available' , ( ) => {
553
+ const showReportDialogSpy = jest . spyOn ( SentryBrowser , 'showReportDialog' ) ;
554
+
555
+ const errorHandler = createErrorHandler ( { showDialog : true } ) ;
556
+ errorHandler . handleError ( new Error ( 'test' ) ) ;
557
+
558
+ expect ( showReportDialogSpy ) . toBeCalledTimes ( 1 ) ;
559
+ } ) ;
560
+ } ) ;
533
561
} ) ;
534
562
} ) ;
0 commit comments