1
- import { Scope } from '@sentry/browser' ;
1
+ import { getCurrentHub , Scope } from '@sentry/browser' ;
2
2
import { fireEvent , render , screen } from '@testing-library/react' ;
3
3
import * as React from 'react' ;
4
4
import { useState } from 'react' ;
@@ -8,6 +8,7 @@ import { ErrorBoundary, isAtLeastReact17, UNKNOWN_COMPONENT, withErrorBoundary }
8
8
9
9
const mockCaptureException = jest . fn ( ) ;
10
10
const mockShowReportDialog = jest . fn ( ) ;
11
+ const mockClientOn = jest . fn ( ) ;
11
12
const EVENT_ID = 'test-id-123' ;
12
13
13
14
jest . mock ( '@sentry/browser' , ( ) => {
@@ -82,6 +83,7 @@ describe('ErrorBoundary', () => {
82
83
afterEach ( ( ) => {
83
84
mockCaptureException . mockClear ( ) ;
84
85
mockShowReportDialog . mockClear ( ) ;
86
+ mockClientOn . mockClear ( ) ;
85
87
} ) ;
86
88
87
89
it ( 'renders null if not given a valid `fallback` prop' , ( ) => {
@@ -405,7 +407,34 @@ describe('ErrorBoundary', () => {
405
407
expect ( mockCaptureException ) . toHaveBeenCalledTimes ( 1 ) ;
406
408
} ) ;
407
409
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
+ } ) ;
409
438
const options = { title : 'custom title' } ;
410
439
render (
411
440
< TestApp fallback = { < p > You have hit an error</ p > } showDialog dialogOptions = { options } >
@@ -418,8 +447,13 @@ describe('ErrorBoundary', () => {
418
447
const btn = screen . getByTestId ( 'errorBtn' ) ;
419
448
fireEvent . click ( btn ) ;
420
449
450
+ // Simulate hook being fired
451
+ callback ( { event_id : EVENT_ID } ) ;
452
+
421
453
expect ( mockShowReportDialog ) . toHaveBeenCalledTimes ( 1 ) ;
422
454
expect ( mockShowReportDialog ) . toHaveBeenCalledWith ( { ...options , eventId : EVENT_ID } ) ;
455
+
456
+ hub . bindClient ( undefined ) ;
423
457
} ) ;
424
458
425
459
it ( 'resets to initial state when reset' , async ( ) => {
0 commit comments