Skip to content

Commit ed22329

Browse files
committed
add tests
1 parent c44ac4b commit ed22329

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
@@ -85,7 +85,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
8585
protected readonly _options: ErrorHandlerOptions;
8686

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

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

packages/angular/test/errorhandler.test.ts

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

Comments
 (0)