@@ -529,5 +529,49 @@ describe('SentryErrorHandler', () => {
529529 expect ( showReportDialogSpy ) . toBeCalledTimes ( 1 ) ;
530530 } ) ;
531531 } ) ;
532+
533+ it ( 'only registers the client "afterSendEvent" listener to open the dialog once' , ( ) => {
534+ const unsubScribeSpy = vi . fn ( ) ;
535+ const client = {
536+ cbs : [ ] as ( ( event : Event ) => void ) [ ] ,
537+ on : vi . fn ( ( _ , cb ) => {
538+ client . cbs . push ( cb ) ;
539+ return unsubScribeSpy ;
540+ } ) ,
541+ } ;
542+
543+ vi . spyOn ( SentryBrowser , 'getClient' ) . mockImplementation ( ( ) => client as unknown as Client ) ;
544+
545+ const errorhandler = createErrorHandler ( { showDialog : true } ) ;
546+ expect ( client . cbs ) . toHaveLength ( 0 ) ;
547+
548+ errorhandler . handleError ( new Error ( 'error 1' ) ) ;
549+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
550+
551+ errorhandler . handleError ( new Error ( 'error 2' ) ) ;
552+ errorhandler . handleError ( new Error ( 'error 3' ) ) ;
553+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
554+ } ) ;
555+
556+ it ( 'cleans up the "afterSendEvent" listener once the ErrorHandler is destroyed' , ( ) => {
557+ const unsubScribeSpy = vi . fn ( ) ;
558+ const client = {
559+ cbs : [ ] as ( ( event : Event ) => void ) [ ] ,
560+ on : vi . fn ( ( _ , cb ) => {
561+ client . cbs . push ( cb ) ;
562+ return unsubScribeSpy ;
563+ } ) ,
564+ } ;
565+
566+ vi . spyOn ( SentryBrowser , 'getClient' ) . mockImplementation ( ( ) => client as unknown as Client ) ;
567+
568+ const errorhandler = createErrorHandler ( { showDialog : true } ) ;
569+
570+ errorhandler . handleError ( new Error ( 'error 1' ) ) ;
571+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
572+
573+ errorhandler . ngOnDestroy ( ) ;
574+ expect ( unsubScribeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
575+ } ) ;
532576 } ) ;
533577} ) ;
0 commit comments