Skip to content

Commit

Permalink
Fix jQuery tests, linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
benvinegar committed Nov 20, 2015
1 parent 3941ce8 commit 8327df0
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 106 deletions.
36 changes: 18 additions & 18 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ module.exports = function(grunt) {
return path;
});

// custom browserify transformer to re-write plugins to
// self-register with Raven via addPlugin
function AddPluginBrowserifyTransformer() {
return function (file, options) {
return through(function (buf, enc, next) {
buf = buf.toString('utf8');
if (/plugins/.test(file)) {
buf += "\nRaven.addPlugin(module.exports);";
}
this.push(buf);
next();
});
};
}

// Taken from http://dzone.com/snippets/calculate-all-combinations
var combine = function (a) {
var fn = function (n, src, got, all) {
Expand Down Expand Up @@ -75,32 +90,17 @@ module.exports = function(grunt) {
options: {
browserifyOptions: {
banner: grunt.file.read('template/_copyright.js'),
standalone: 'Raven', // umd
standalone: 'Raven' // umd
}
},
core: {
src: 'src/raven.js',
dest: 'build/raven.js',
dest: 'build/raven.js'
},
plugins: {
files: pluginConcatFiles,
transform: [
[
// custom transformer to re-write plugins to self-register
// with Raven
new function () {
return function (file, options) {
return through(function (buf, enc, next) {
var buf = buf.toString('utf8');
if (/plugins/.test(file)) {
buf += "\nRaven.addPlugin(module.exports.install);";
}
this.push(buf);
next();
});
}
}
]
[ new AddPluginBrowserifyTransformer() ]
]
},
test: {
Expand Down
2 changes: 2 additions & 0 deletions plugins/angular.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

/**
* Angular.js plugin
*
Expand Down
167 changes: 83 additions & 84 deletions plugins/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,105 @@
*
* Patches event handler callbacks and ajax callbacks.
*/
;(function(window) {
'use strict';

if (window.Raven) Raven.addPlugin(function jQueryPlugin() {

var $ = window.jQuery;

// quit if jQuery isn't on the page
if (!$) return;

var _oldEventAdd = $.event.add;
$.event.add = function ravenEventAdd(elem, types, handler, data, selector) {
var _handler;

if (handler && handler.handler) {
_handler = handler.handler;
handler.handler = Raven.wrap(handler.handler);
} else {
_handler = handler;
handler = Raven.wrap(handler);
}

// If the handler we are attaching doesn’t have the same guid as
// the original, it will never be removed when someone tries to
// unbind the original function later. Technically as a result of
// this our guids are no longer globally unique, but whatever, that
// never hurt anybody RIGHT?!
if (_handler.guid) {
handler.guid = _handler.guid;
} else {
handler.guid = _handler.guid = $.guid++;
}

return _oldEventAdd.call(this, elem, types, handler, data, selector);
};
var Raven = require('../src/raven');

var _oldReady = $.fn.ready;
$.fn.ready = function ravenjQueryReadyWrapper(fn) {
return _oldReady.call(this, Raven.wrap(fn));
};
function install(jQuery) {
var $ = jQuery || window.jQuery;

var _oldAjax = $.ajax;
$.ajax = function ravenAjaxWrapper(url, options) {
var keys = ['complete', 'error', 'success'], key;
// quit if jQuery isn't on the page

// Taken from https://github.com/jquery/jquery/blob/eee2eaf1d7a189d99106423a4206c224ebd5b848/src/ajax.js#L311-L318
// If url is an object, simulate pre-1.5 signature
if (typeof url === 'object') {
options = url;
url = undefined;
}
var _oldEventAdd = $.event.add;
$.event.add = function ravenEventAdd(elem, types, handler, data, selector) {
var _handler;

// Force options to be an object
options = options || {};
if (handler && handler.handler) {
_handler = handler.handler;
handler.handler = Raven.wrap(handler.handler);
} else {
_handler = handler;
handler = Raven.wrap(handler);
}

/*jshint -W084*/
while (key = keys.pop()) {
if ($.isFunction(options[key])) {
options[key] = Raven.wrap(options[key]);
// If the handler we are attaching doesn’t have the same guid as
// the original, it will never be removed when someone tries to
// unbind the original function later. Technically as a result of
// this our guids are no longer globally unique, but whatever, that
// never hurt anybody RIGHT?!
if (_handler.guid) {
handler.guid = _handler.guid;
} else {
handler.guid = _handler.guid = $.guid++;
}
}
/*jshint +W084*/

try {
var jqXHR = _oldAjax.call(this, url, options);
// jqXHR.complete is not a regular deferred callback
if ($.isFunction(jqXHR.complete))
jqXHR.complete = Raven.wrap(jqXHR.complete);
return jqXHR;
} catch (e) {
Raven.captureException(e);
throw e;
}
};

var _oldDeferred = $.Deferred;
$.Deferred = function ravenDeferredWrapper(func) {
return !_oldDeferred ? null : _oldDeferred(function beforeStartWrapper(deferred) {
var methods = ['resolve', 'reject', 'notify', 'resolveWith', 'rejectWith', 'notifyWith'], method;
return _oldEventAdd.call(this, elem, types, handler, data, selector);
};

var _oldReady = $.fn.ready;
$.fn.ready = function ravenjQueryReadyWrapper(fn) {
return _oldReady.call(this, Raven.wrap(fn));
};

var _oldAjax = $.ajax;
$.ajax = function ravenAjaxWrapper(url, options) {
var keys = ['complete', 'error', 'success'], key;

// since jQuery 1.9, deferred[resolve | reject | notify] are calling internally
// deferred[resolveWith | rejectWith | notifyWith] but we need to wrap them as well
// to support all previous versions.
// Taken from https://github.com/jquery/jquery/blob/eee2eaf1d7a189d99106423a4206c224ebd5b848/src/ajax.js#L311-L318
// If url is an object, simulate pre-1.5 signature
if (typeof url === 'object') {
options = url;
url = undefined;
}

// Force options to be an object
options = options || {};

/*jshint -W084*/
while (method = methods.pop()) {
if ($.isFunction(deferred[method])) {
deferred[method] = Raven.wrap(deferred[method]);
while (key = keys.pop()) {
if ($.isFunction(options[key])) {
options[key] = Raven.wrap(options[key]);
}
}
/*jshint +W084*/

// Call given func if any
if (func) {
func.call(deferred, deferred);
try {
var jqXHR = _oldAjax.call(this, url, options);
// jqXHR.complete is not a regular deferred callback
if ($.isFunction(jqXHR.complete))
jqXHR.complete = Raven.wrap(jqXHR.complete);
return jqXHR;
} catch (e) {
Raven.captureException(e);
throw e;
}
});
};
};

var _oldDeferred = $.Deferred;
$.Deferred = function ravenDeferredWrapper(func) {
return !_oldDeferred ? null : _oldDeferred(function beforeStartWrapper(deferred) {
var methods = ['resolve', 'reject', 'notify', 'resolveWith', 'rejectWith', 'notifyWith'], method;

// since jQuery 1.9, deferred[resolve | reject | notify] are calling internally
// deferred[resolveWith | rejectWith | notifyWith] but we need to wrap them as well
// to support all previous versions.

/*jshint -W084*/
while (method = methods.pop()) {
if ($.isFunction(deferred[method])) {
deferred[method] = Raven.wrap(deferred[method]);
}
}
/*jshint +W084*/

// End of plugin factory
});
// Call given func if any
if (func) {
func.call(deferred, deferred);
}
});
};
}

}(typeof window !== 'undefined' ? window : this));
module.exports = {
install: install
};
9 changes: 6 additions & 3 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,11 @@ var Raven = {
return Raven;
},

addPlugin: function(plugin) {
addPlugin: function(plugin /*arg1, arg2, ... argN*/) {
plugins.push(plugin);
if (isRavenInstalled) plugin();
if (isRavenInstalled) {
plugin.install.apply(this, Array.prototype.slice.call(arguments, 1));
}
return Raven;
},

Expand Down Expand Up @@ -910,7 +912,7 @@ Raven._uuid4 = function() {
return v.toString(16);
});
}
}
};

Raven._logDebug = function(level) {
if (originalConsoleMethods[level] && Raven.debug) {
Expand Down Expand Up @@ -1010,6 +1012,7 @@ if (typeof TEST !== 'undefined' && TEST) {
ravenNotConfiguredError = undefined;
originalConsole = window.console || {};
originalConsoleMethods = {};
plugins = [];

for (var method in originalConsole) {
originalConsoleMethods[method] = originalConsole[method];
Expand Down
15 changes: 14 additions & 1 deletion test/plugins/jquery.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
'use strict';
/*jshint mocha:true*/
/*global assert:false, console:true*/

// TODO(dcramer): need some kind of clean setup state and dynamic
// loading of the jquery plugin

var Raven = require('../../src/raven');
var RavenjQueryPlugin = require('../../plugins/jquery');
var jQuery = require('jquery');

describe('jQuery', function(){
before(function () {
Raven._test.setGlobalState({ isRavenInstalled: true });
Raven.addPlugin(RavenjQueryPlugin, jQuery);
});

describe('.fn.ready', function(){
it('captures exceptions #integration', function(){
var err = new Error('foo');
Expand Down Expand Up @@ -72,7 +85,7 @@ describe('jQuery', function(){
it('captures errors from ' + key + ' with context', function(key){
return function(){
assertErrorRecorded(function(){
var dfd = $.Deferred();
var dfd = jQuery.Deferred();
dfd.promise()[key](function() {
throw err;
});
Expand Down

0 comments on commit 8327df0

Please sign in to comment.