probe.gl
Version:
JavaScript Console Instrumentation and Benchmarking for Browser and Node
64 lines (54 loc) • 1.32 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.makeSpy = makeSpy;
function makeSpy(obj, func) {
var methodName;
if (!obj && !func) {
func = function mock() {};
obj = {};
methodName = 'spy';
} else if (typeof obj === 'function' && !func) {
func = obj;
obj = {};
methodName = "".concat(func.name, "-spy");
} else {
methodName = func;
func = obj[methodName];
}
return wrapFunction(obj, func, methodName);
}
function wrapFunction(obj, func, methodName) {
if (func.func !== undefined) {
return func;
}
function spy() {
spy.callCount++;
spy.called = true;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return 'returnValue' in spy ? spy.returnValue : func.apply(this, args);
}
Object.assign(spy, {
reset: function reset() {
spy.callCount = 0;
spy.called = false;
},
restore: function restore() {
obj[methodName] = func;
},
returns: function returns(returnValue) {
spy.returnValue = returnValue;
},
obj: obj,
methodName: methodName,
func: func,
method: func
});
spy.reset();
obj[methodName] = spy;
return spy;
}
//# sourceMappingURL=make-spy.js.map