Skip to content

Commit eae93f2

Browse files
committed
add tests
1 parent 3bb8cc8 commit eae93f2

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/angular/src/errorhandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
8484
protected readonly _options: ErrorHandlerOptions;
8585

8686
/** The cleanup function is executed when the injector is destroyed. */
87-
private _removeAfterSendEventListener?: VoidFunction;
87+
private _removeAfterSendEventListener?: () => void;
8888

8989
public constructor(@Inject('errorHandlerOptions') options?: ErrorHandlerOptions) {
9090
this._options = {
@@ -129,7 +129,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
129129
this._removeAfterSendEventListener = client.on('afterSendEvent', (event: Event) => {
130130
if (!event.type && event.event_id) {
131131
runOutsideAngular(() => {
132-
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id! });
132+
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id });
133133
});
134134
}
135135
});

packages/angular/test/errorhandler.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)