Skip to content

Instantly share code, notes, and snippets.

@robinduckett
Created January 19, 2015 09:57
Show Gist options
  • Save robinduckett/41644baa2748b90fe7b2 to your computer and use it in GitHub Desktop.
Save robinduckett/41644baa2748b90fe7b2 to your computer and use it in GitHub Desktop.
var $j = (function($j) {
var funcs = [
'on', 'hide', 'is', 'toggle', 'html', 'parent'
];
var events = {};
funcs.forEach(function(func) {
events[func] = sinon.spy(jQuery.fn, func);
});
function jexpect(selector) {
this.selector = selector;
this.events = events;
this.stubs = {};
}
funcs.forEach(function(evt) {
jexpect.prototype[evt] = function(arg) {
var calls = [];
this.events[evt].thisValues.forEach(function(item, index) {
if (item.selector === this.selector) {
if (this.events[evt].args[index][0] === arg) {
calls.push(this.events[evt].getCall(index));
}
}
}.bind(this));
if (calls.length === 0) {
this.stubs[this.selector] = sinon.stub(jQuery.fn, evt);
return this.events[evt];
}
return calls[0];
}
});
return $j = function(selector) {
return new jexpect(selector);
};
})($j);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment