11import { HttpErrorResponse } from '@angular/common/http' ;
22import * as SentryBrowser from '@sentry/browser' ;
33import { Scope } from '@sentry/browser' ;
4+ import type { Event } from '@sentry/types' ;
45import * as SentryUtils from '@sentry/utils' ;
56
67import { createErrorHandler , SentryErrorHandler } from '../src/errorhandler' ;
@@ -507,15 +508,6 @@ describe('SentryErrorHandler', () => {
507508 expect ( captureExceptionSpy ) . toHaveBeenCalledWith ( 'something happened' , expect . any ( Function ) ) ;
508509 } ) ;
509510
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-
519511 it ( 'extracts error with a custom extractor' , ( ) => {
520512 const customExtractor = ( error : unknown ) => {
521513 if ( typeof error === 'string' ) {
@@ -530,5 +522,41 @@ describe('SentryErrorHandler', () => {
530522 expect ( captureExceptionSpy ) . toHaveBeenCalledTimes ( 1 ) ;
531523 expect ( captureExceptionSpy ) . toHaveBeenCalledWith ( new Error ( 'custom error' ) , expect . any ( Function ) ) ;
532524 } ) ;
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+ } ) ;
533561 } ) ;
534562} ) ;
0 commit comments