Skip to content

dataCallback, shouldSendCallback are passed prior callback as 2nd arg #636

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,10 @@ Raven.prototype = {
* @return {Raven}
*/
setDataCallback: function(callback) {
this._globalOptions.dataCallback = callback;
var original = this._globalOptions.dataCallback;
this._globalOptions.dataCallback = isFunction(callback)
? function (data) { return callback(data, original); }
: callback;

return this;
},
Expand All @@ -464,7 +467,10 @@ Raven.prototype = {
* @return {Raven}
*/
setShouldSendCallback: function(callback) {
this._globalOptions.shouldSendCallback = callback;
var original = this._globalOptions.shouldSendCallback;
this._globalOptions.shouldSendCallback = isFunction(callback)
? function (data) { return callback(data, original); }
: callback;

return this;
},
Expand Down
48 changes: 44 additions & 4 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,9 +1787,14 @@ describe('Raven (public API)', function() {

describe('.setDataCallback', function() {
it('should set the globalOptions.dataCallback attribute', function() {
var foo = function(){};
var foo = sinon.stub();
Raven.setDataCallback(foo);
assert.equal(Raven._globalOptions.dataCallback, foo);

// note that setDataCallback creates a callback/closure around
// foo, so can't test for equality - just verify that calling the wrapper
// also calls foo
Raven._globalOptions.dataCallback();
assert.isTrue(foo.calledOnce);
});

it('should clear globalOptions.dataCallback with no arguments', function() {
Expand All @@ -1798,13 +1803,33 @@ describe('Raven (public API)', function() {
Raven.setDataCallback();
assert.isUndefined(Raven._globalOptions.dataCallback);
});

it('should generate a wrapper that passes the prior callback as the 2nd argument', function () {
var foo = sinon.stub();
var bar = sinon.spy(function(data, orig) {
assert.equal(orig, foo);
foo();
});
Raven._globalOptions.dataCallback = foo;
Raven.setDataCallback(bar);
Raven._globalOptions.dataCallback({
'a': 1 // "data"
});
assert.isTrue(bar.calledOnce);
assert.isTrue(foo.calledOnce);
});
});

describe('.setShouldSendCallback', function() {
it('should set the globalOptions.shouldSendCallback attribute', function() {
var foo = function(){};
var foo = sinon.stub();
Raven.setShouldSendCallback(foo);
assert.equal(Raven._globalOptions.shouldSendCallback, foo);

// note that setShouldSendCallback creates a callback/closure around
// foo, so can't test for equality - just verify that calling the wrapper
// also calls foo
Raven._globalOptions.shouldSendCallback();
assert.isTrue(foo.calledOnce);
});

it('should clear globalOptions.shouldSendCallback with no arguments', function() {
Expand All @@ -1813,6 +1838,21 @@ describe('Raven (public API)', function() {
Raven.setShouldSendCallback();
assert.isUndefined(Raven._globalOptions.shouldSendCallback);
});

it('should generate a wrapper that passes the prior callback as the 2nd argument', function () {
var foo = sinon.stub();
var bar = sinon.spy(function(data, orig) {
assert.equal(orig, foo);
foo();
});
Raven._globalOptions.shouldSendCallback = foo;
Raven.setShouldSendCallback(bar);
Raven._globalOptions.shouldSendCallback({
'a': 1 // "data"
});
assert.isTrue(bar.calledOnce);
assert.isTrue(foo.calledOnce);
});
});

describe('.captureMessage', function() {
Expand Down