can-globals
Version:
This module provides a dependency injection container. Modules may define a key and specify a default value (which can be static, cached lazy, or dynamic lazy), but other code can set and reset the value as needed. There is also an event system, for alert
47 lines (41 loc) • 890 B
JavaScript
;
function spy(value) {
var fn;
var calls = [];
if (typeof value === 'function') {
fn = value;
} else {
fn = function () {
return value;
};
}
function wrapper() {
var args = Array.prototype.slice.call(arguments);
/* jshint -W040 */
var ret = fn.apply(this, args);
calls.push({
calledWith: args,
returned: ret
});
return ret;
}
Object.defineProperties(wrapper, {
reset: {
value: function () {
calls = [];
}
},
callCount: {
get: function () {
return calls.length;
}
},
calls: {
get: function () {
return calls;
}
}
});
return wrapper;
}
module.exports = spy;