@@ -97,7 +97,7 @@ describe('SentryBrowser', () => {
9797 getCurrentScope ( ) . setUser ( EX_USER ) ;
9898 setCurrentClient ( client ) ;
9999
100- showReportDialog ( ) ;
100+ showReportDialog ( { eventId : 'foobar' } ) ;
101101
102102 expect ( getReportDialogEndpoint ) . toHaveBeenCalledTimes ( 1 ) ;
103103 expect ( getReportDialogEndpoint ) . toHaveBeenCalledWith (
@@ -136,7 +136,7 @@ describe('SentryBrowser', () => {
136136 setCurrentClient ( client ) ;
137137
138138 const DIALOG_OPTION_USER = { email : 'option@example.com' } ;
139- showReportDialog ( { user : DIALOG_OPTION_USER } ) ;
139+ showReportDialog ( { eventId : 'foobar' , user : DIALOG_OPTION_USER } ) ;
140140
141141 expect ( getReportDialogEndpoint ) . toHaveBeenCalledTimes ( 1 ) ;
142142 expect ( getReportDialogEndpoint ) . toHaveBeenCalledWith (
@@ -170,7 +170,7 @@ describe('SentryBrowser', () => {
170170 it ( 'should call `onClose` when receiving `__sentry_reportdialog_closed__` MessageEvent' , async ( ) => {
171171 const onClose = vi . fn ( ) ;
172172
173- showReportDialog ( { onClose } ) ;
173+ showReportDialog ( { eventId : 'foobar' , onClose } ) ;
174174
175175 await waitForPostMessage ( '__sentry_reportdialog_closed__' ) ;
176176 expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -185,7 +185,7 @@ describe('SentryBrowser', () => {
185185 throw new Error ( ) ;
186186 } ) ;
187187
188- showReportDialog ( { onClose } ) ;
188+ showReportDialog ( { eventId : 'foobar' , onClose } ) ;
189189
190190 await waitForPostMessage ( '__sentry_reportdialog_closed__' ) ;
191191 expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -198,14 +198,53 @@ describe('SentryBrowser', () => {
198198 it ( 'should not call `onClose` for other MessageEvents' , async ( ) => {
199199 const onClose = vi . fn ( ) ;
200200
201- showReportDialog ( { onClose } ) ;
201+ showReportDialog ( { eventId : 'foobar' , onClose } ) ;
202202
203203 await waitForPostMessage ( 'some_message' ) ;
204204 expect ( onClose ) . not . toHaveBeenCalled ( ) ;
205205
206206 await waitForPostMessage ( '__sentry_reportdialog_closed__' ) ;
207207 expect ( onClose ) . toHaveBeenCalledTimes ( 1 ) ;
208208 } ) ;
209+
210+ it ( 'should remove the `onClose` listener when the script fails to load' , async ( ) => {
211+ const onClose = vi . fn ( ) ;
212+
213+ showReportDialog ( { eventId : 'foobar' , onClose } ) ;
214+
215+ const script = WINDOW . document . head . lastElementChild as HTMLScriptElement ;
216+ script . dispatchEvent ( new Event ( 'error' ) ) ;
217+
218+ await waitForPostMessage ( '__sentry_reportdialog_closed__' ) ;
219+ expect ( onClose ) . not . toHaveBeenCalled ( ) ;
220+ } ) ;
221+ } ) ;
222+
223+ describe ( 'onError' , ( ) => {
224+ it ( 'should call `onError` when the script fails to load' , ( ) => {
225+ const onError = vi . fn ( ) ;
226+
227+ showReportDialog ( { eventId : 'foobar' , onError } ) ;
228+
229+ const script = WINDOW . document . head . lastElementChild as HTMLScriptElement ;
230+ script . dispatchEvent ( new Event ( 'error' ) ) ;
231+
232+ expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
233+ expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'Failed to load the report dialog script' ) ) ;
234+ } ) ;
235+
236+ it ( 'should call `onError` and not inject the script without an event ID' , ( ) => {
237+ const onError = vi . fn ( ) ;
238+ const appendChildSpy = vi . spyOn ( WINDOW . document . head , 'appendChild' ) ;
239+
240+ showReportDialog ( { onError } ) ;
241+
242+ expect ( onError ) . toHaveBeenCalledTimes ( 1 ) ;
243+ expect ( onError ) . toHaveBeenCalledWith ( new Error ( 'No event ID to show the report dialog for' ) ) ;
244+ expect ( appendChildSpy ) . not . toHaveBeenCalled ( ) ;
245+
246+ appendChildSpy . mockRestore ( ) ;
247+ } ) ;
209248 } ) ;
210249 } ) ;
211250
0 commit comments