@@ -546,5 +546,49 @@ describe('SentryErrorHandler', () => {
546546 expect ( showReportDialogSpy ) . toBeCalledTimes ( 1 ) ;
547547 } ) ;
548548 } ) ;
549+
550+ it ( 'only registers the client "afterSendEvent" listener to open the dialog once' , ( ) => {
551+ const unsubScribeSpy = vi . fn ( ) ;
552+ const client = {
553+ cbs : [ ] as ( ( event : Event ) => void ) [ ] ,
554+ on : vi . fn ( ( _ , cb ) => {
555+ client . cbs . push ( cb ) ;
556+ return unsubScribeSpy ;
557+ } ) ,
558+ } ;
559+
560+ vi . spyOn ( SentryBrowser , 'getClient' ) . mockImplementation ( ( ) => client as unknown as Client ) ;
561+
562+ const errorhandler = createErrorHandler ( { showDialog : true } ) ;
563+ expect ( client . cbs ) . toHaveLength ( 0 ) ;
564+
565+ errorhandler . handleError ( new Error ( 'error 1' ) ) ;
566+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
567+
568+ errorhandler . handleError ( new Error ( 'error 2' ) ) ;
569+ errorhandler . handleError ( new Error ( 'error 3' ) ) ;
570+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
571+ } ) ;
572+
573+ it ( 'cleans up the "afterSendEvent" listener once the ErrorHandler is destroyed' , ( ) => {
574+ const unsubScribeSpy = vi . fn ( ) ;
575+ const client = {
576+ cbs : [ ] as ( ( event : Event ) => void ) [ ] ,
577+ on : vi . fn ( ( _ , cb ) => {
578+ client . cbs . push ( cb ) ;
579+ return unsubScribeSpy ;
580+ } ) ,
581+ } ;
582+
583+ vi . spyOn ( SentryBrowser , 'getClient' ) . mockImplementation ( ( ) => client as unknown as Client ) ;
584+
585+ const errorhandler = createErrorHandler ( { showDialog : true } ) ;
586+
587+ errorhandler . handleError ( new Error ( 'error 1' ) ) ;
588+ expect ( client . cbs ) . toHaveLength ( 1 ) ;
589+
590+ errorhandler . ngOnDestroy ( ) ;
591+ expect ( unsubScribeSpy ) . toHaveBeenCalledTimes ( 1 ) ;
592+ } ) ;
549593 } ) ;
550594} ) ;
0 commit comments