1- import { Scope } from '@sentry/browser' ;
1+ import { getCurrentHub , Scope } from '@sentry/browser' ;
22import { fireEvent , render , screen } from '@testing-library/react' ;
33import * as React from 'react' ;
44import { useState } from 'react' ;
@@ -8,6 +8,7 @@ import { ErrorBoundary, isAtLeastReact17, UNKNOWN_COMPONENT, withErrorBoundary }
88
99const mockCaptureException = jest . fn ( ) ;
1010const mockShowReportDialog = jest . fn ( ) ;
11+ const mockClientOn = jest . fn ( ) ;
1112const EVENT_ID = 'test-id-123' ;
1213
1314jest . mock ( '@sentry/browser' , ( ) => {
@@ -82,6 +83,7 @@ describe('ErrorBoundary', () => {
8283 afterEach ( ( ) => {
8384 mockCaptureException . mockClear ( ) ;
8485 mockShowReportDialog . mockClear ( ) ;
86+ mockClientOn . mockClear ( ) ;
8587 } ) ;
8688
8789 it ( 'renders null if not given a valid `fallback` prop' , ( ) => {
@@ -405,7 +407,34 @@ describe('ErrorBoundary', () => {
405407 expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
406408 } ) ;
407409
408- it ( 'shows a Sentry Report Dialog with correct options' , ( ) => {
410+ it ( 'shows a Sentry Report Dialog with correct options if client does not have hooks' , ( ) => {
411+ expect ( getCurrentHub ( ) . getClient ( ) ) . toBeUndefined ( ) ;
412+
413+ const options = { title : 'custom title' } ;
414+ render (
415+ < TestApp fallback = { < p > You have hit an error</ p > } showDialog dialogOptions = { options } >
416+ < h1 > children</ h1 >
417+ </ TestApp > ,
418+ ) ;
419+
420+ expect ( mockShowReportDialog ) . toHaveBeenCalledTimes ( 0 ) ;
421+
422+ const btn = screen . getByTestId ( 'errorBtn' ) ;
423+ fireEvent . click ( btn ) ;
424+
425+ expect ( mockShowReportDialog ) . toHaveBeenCalledTimes ( 1 ) ;
426+ expect ( mockShowReportDialog ) . toHaveBeenCalledWith ( { ...options , eventId : EVENT_ID } ) ;
427+ } ) ;
428+
429+ it ( 'shows a Sentry Report Dialog with correct options if client has hooks' , ( ) => {
430+ let callback : any ;
431+ const hub = getCurrentHub ( ) ;
432+ // @ts -ignore mock client
433+ hub . bindClient ( {
434+ on : ( name : string , cb : any ) => {
435+ callback = cb ;
436+ } ,
437+ } ) ;
409438 const options = { title : 'custom title' } ;
410439 render (
411440 < TestApp fallback = { < p > You have hit an error</ p > } showDialog dialogOptions = { options } >
@@ -418,8 +447,13 @@ describe('ErrorBoundary', () => {
418447 const btn = screen . getByTestId ( 'errorBtn' ) ;
419448 fireEvent . click ( btn ) ;
420449
450+ // Simulate hook being fired
451+ callback ( { event_id : EVENT_ID } ) ;
452+
421453 expect ( mockShowReportDialog ) . toHaveBeenCalledTimes ( 1 ) ;
422454 expect ( mockShowReportDialog ) . toHaveBeenCalledWith ( { ...options , eventId : EVENT_ID } ) ;
455+
456+ hub . bindClient ( undefined ) ;
423457 } ) ;
424458
425459 it ( 'resets to initial state when reset' , async ( ) => {
0 commit comments