Skip to content

Commit

Permalink
test: Added tests for dynamic options in showReportDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jul 20, 2018
1 parent 84ed55c commit 7d7202b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 16 deletions.
33 changes: 17 additions & 16 deletions packages/raven-js/src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,11 +877,14 @@ Raven.prototype = {
)
return;

options = Object.assign({
eventId: this.lastEventId(),
dsn: this._dsn,
user: this._globalContext.user,
}, options || {});
options = Object.assign(
{
eventId: this.lastEventId(),
dsn: this._dsn,
user: this._globalContext.user || {}
},
options
);

if (!options.eventId) {
throw new RavenConfigError('Missing eventId');
Expand All @@ -892,24 +895,22 @@ Raven.prototype = {
}

var encode = encodeURIComponent;
var qs = '?';
var encodedOptions = [];

for (var key in options) {
if (key !== 'user') {
qs += encode(key) + '=' + encode(options[key]) + '&';
if (key === 'user') {
var user = options.user;
if (user.name) encodedOptions.push('name=' + encode(user.name));
if (user.email) encodedOptions.push('email=' + encode(user.email));
} else {
encodedOptions.push(encode(key) + '=' + encode(options[key]));
}
}

var user = options.user;
if (user) {
if (user.name) qs += '&name=' + encode(user.name);
if (user.email) qs += '&email=' + encode(user.email);
}

var globalServer = this._getGlobalServer(this._parseDSN(options.dsn));

var script = _document.createElement('script');
script.async = true;
script.src = globalServer + '/api/embed/error-page/' + qs;
script.src = globalServer + '/api/embed/error-page/?' + encodedOptions.join('&');
(_document.head || _document.body).appendChild(script);
},

Expand Down
39 changes: 39 additions & 0 deletions packages/raven-js/test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3706,6 +3706,45 @@ describe('Raven (public API)', function() {
'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2&name=Average%20Normalperson%202&email=an2%40example.com'
);
});

it('should specify detailed query string for dialog customization', function() {
this.sinon.stub(Raven, '_makeRequest');

Raven.showReportDialog({
eventId: 'abc123',
dsn: SENTRY_DSN,
title: 'title',
subtitle: 'subtitle',
subtitle2: 'subtitle2',
labelName: 'labelName',
labelEmail: 'labelEmail',
labelComments: 'labelComments',
labelClose: 'labelClose',
labelSubmit: 'labelSubmit',
errorGeneric: 'errorGeneric',
errorFormEntry: 'errorFormEntry',
successMessage: 'successMessage',
});

var script = this.appendChildStub.getCall(0).args[0];
assert.equal(
script.src,
'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2&title=title&subtitle=subtitle&subtitle2=subtitle2&labelName=labelName&labelEmail=labelEmail&labelComments=labelComments&labelClose=labelClose&labelSubmit=labelSubmit&errorGeneric=errorGeneric&errorFormEntry=errorFormEntry&successMessage=successMessage'
);

this.appendChildStub.reset();

Raven.config(SENTRY_DSN)
.captureException(new Error('foo')) // generates lastEventId
.showReportDialog();

this.appendChildStub.getCall(0).args[0];
assert.equal(
script.src,
'http://example.com/api/embed/error-page/?eventId=abc123&dsn=http%3A%2F%2Fabc%40example.com%3A80%2F2&title=title&subtitle=subtitle&subtitle2=subtitle2&labelName=labelName&labelEmail=labelEmail&labelComments=labelComments&labelClose=labelClose&labelSubmit=labelSubmit&errorGeneric=errorGeneric&errorFormEntry=errorFormEntry&successMessage=successMessage'
);
});

});
});

Expand Down

0 comments on commit 7d7202b

Please sign in to comment.