Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Jul 18, 2024
1 parent 3bb8cc8 commit eae93f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/angular/src/errorhandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
protected readonly _options: ErrorHandlerOptions;

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

public constructor(@Inject('errorHandlerOptions') options?: ErrorHandlerOptions) {
this._options = {
Expand Down Expand Up @@ -129,7 +129,7 @@ class SentryErrorHandler implements AngularErrorHandler, OnDestroy {
this._removeAfterSendEventListener = client.on('afterSendEvent', (event: Event) => {
if (!event.type && event.event_id) {
runOutsideAngular(() => {
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id! });
Sentry.showReportDialog({ ...this._options.dialogOptions, eventId: event.event_id });
});
}
});
Expand Down
44 changes: 44 additions & 0 deletions packages/angular/test/errorhandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,5 +529,49 @@ describe('SentryErrorHandler', () => {
expect(showReportDialogSpy).toBeCalledTimes(1);
});
});

it('only registers the client "afterSendEvent" listener to open the dialog once', () => {
const unsubScribeSpy = vi.fn();
const client = {
cbs: [] as ((event: Event) => void)[],
on: vi.fn((_, cb) => {
client.cbs.push(cb);
return unsubScribeSpy;
}),
};

vi.spyOn(SentryBrowser, 'getClient').mockImplementation(() => client as unknown as Client);

const errorhandler = createErrorHandler({ showDialog: true });
expect(client.cbs).toHaveLength(0);

errorhandler.handleError(new Error('error 1'));
expect(client.cbs).toHaveLength(1);

errorhandler.handleError(new Error('error 2'));
errorhandler.handleError(new Error('error 3'));
expect(client.cbs).toHaveLength(1);
});

it('cleans up the "afterSendEvent" listener once the ErrorHandler is destroyed', () => {
const unsubScribeSpy = vi.fn();
const client = {
cbs: [] as ((event: Event) => void)[],
on: vi.fn((_, cb) => {
client.cbs.push(cb);
return unsubScribeSpy;
}),
};

vi.spyOn(SentryBrowser, 'getClient').mockImplementation(() => client as unknown as Client);

const errorhandler = createErrorHandler({ showDialog: true });

errorhandler.handleError(new Error('error 1'));
expect(client.cbs).toHaveLength(1);

errorhandler.ngOnDestroy();
expect(unsubScribeSpy).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit eae93f2

Please sign in to comment.