Skip to content

Commit 4425e49

Browse files
feat(browser): Use scope data in report dialog (#3792)
* feat(browser): Use scope data in report dialog If user is set on the scope, we set that user in report dialog options. If a user is already set in the report dialog options, it takes precedence over the user object in the scope. Fixes #2607 * Update packages/browser/test/unit/index.test.ts Co-authored-by: Kamil Ogórek <[email protected]>
1 parent 3095f4b commit 4425e49

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

packages/browser/src/sdk.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,19 @@ export function init(options: BrowserOptions = {}): void {
102102
* @param options Everything is optional, we try to fetch all info need from the global scope.
103103
*/
104104
export function showReportDialog(options: ReportDialogOptions = {}): void {
105+
const hub = getCurrentHub();
106+
const scope = hub.getScope();
107+
if (scope) {
108+
options.user = {
109+
...scope.getUser(),
110+
...options.user,
111+
};
112+
}
113+
105114
if (!options.eventId) {
106-
options.eventId = getCurrentHub().lastEventId();
115+
options.eventId = hub.lastEventId();
107116
}
108-
const client = getCurrentHub().getClient<BrowserClient>();
117+
const client = hub.getClient<BrowserClient>();
109118
if (client) {
110119
client.showReportDialog(options);
111120
}

packages/browser/test/unit/index.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
init,
1616
Integrations,
1717
Scope,
18+
showReportDialog,
1819
wrap,
1920
} from '../../src';
2021
import { SimpleTransport } from './mocks/simpletransport';
@@ -73,6 +74,39 @@ describe('SentryBrowser', () => {
7374
});
7475
});
7576

77+
describe('showReportDialog', () => {
78+
describe('user', () => {
79+
const EX_USER = { email: '[email protected]' };
80+
const client = new BrowserClient({ dsn });
81+
spy(client, 'showReportDialog');
82+
83+
it('uses the user on the scope', () => {
84+
configureScope(scope => {
85+
scope.setUser(EX_USER);
86+
});
87+
getCurrentHub().bindClient(client);
88+
89+
showReportDialog();
90+
91+
expect((client.showReportDialog as SinonSpy).called).to.be.true;
92+
expect((client.showReportDialog as SinonSpy).lastCall.args[0].user.email).to.eq(EX_USER.email);
93+
});
94+
95+
it('prioritizes options user over scope user', () => {
96+
configureScope(scope => {
97+
scope.setUser(EX_USER);
98+
});
99+
getCurrentHub().bindClient(client);
100+
101+
const DIALOG_OPTION_USER = { email: '[email protected]' };
102+
showReportDialog({ user: DIALOG_OPTION_USER });
103+
104+
expect((client.showReportDialog as SinonSpy).called).to.be.true;
105+
expect((client.showReportDialog as SinonSpy).lastCall.args[0].user.email).to.eq(DIALOG_OPTION_USER.email);
106+
});
107+
});
108+
});
109+
76110
describe('breadcrumbs', () => {
77111
it('should record breadcrumbs', async () => {
78112
addBreadcrumb({ message: 'test1' });

0 commit comments

Comments
 (0)