Skip to content

Instantly share code, notes, and snippets.

@frozeman
Last active January 1, 2016 21:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frozeman/8206698 to your computer and use it in GitHub Desktop.
Save frozeman/8206698 to your computer and use it in GitHub Desktop.
jQuery testing stubs. Where the stub can be overwritten e.g. by setting jQueryStub.prototype.find = ...;
var $, jQueryStub;
(function () {
"use strict";
var emptyFunction = function () {
};
var selfReturn = function(){
return new jQueryStub();
};
jQueryStub = function () {
};
jQueryStub.prototype = {
onEventMap: {},
ready: emptyFunction,
show: emptyFunction,
scroll: emptyFunction,
scrollTop: emptyFunction,
val: emptyFunction,
each: emptyFunction,
attr: emptyFunction,
done: emptyFunction,
hide: emptyFunction,
focus: emptyFunction,
off: emptyFunction,
length: 0,
text: function(){
return {
attr: emptyFunction
};
},
on: function (eventKey, eventFunction) {
jQueryStub.prototype.onEventMap[eventKey] = eventFunction;
},
fireOnEvent: function (key, eventObject) {
jQueryStub.prototype.onEventMap[key](eventObject);
},
trigger: function(eventKey){
jQueryStub.prototype.onEventMap[eventKey]();
},
fn: {},
addedClasses: [],
addClass: function (className) {
jQueryStub.prototype.addedClasses.push(className);
},
data: function() {
return;
},
scrollLeft: function() {
},
html: selfReturn,
find: selfReturn,
parents: selfReturn,
parent: selfReturn,
width: selfReturn,
innerWidth: selfReturn,
css: selfReturn
};
$ = function () {
return new jQueryStub();
};
$.ajax = function(){
return {
done: emptyFunction
};
};
$.getJSON = emptyFunction;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment