ts-mock-imports
Version:
Intuitive mocking for Typescript class imports
77 lines • 2.98 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.MockManager = void 0;
var sinonModule = require("sinon");
var manager_1 = require("./manager");
var sinon = sinonModule;
var MockManager = (function (_super) {
__extends(MockManager, _super);
function MockManager(module, importName) {
var _this = _super.call(this, module, importName) || this;
_this.module = module;
_this.importName = importName;
_this.createStubClass();
_this.module[_this.importName] = _this.stubClass;
return _this;
}
MockManager.prototype.mock = function (funcName, returns) {
return this.mockFunction(funcName, returns);
};
MockManager.prototype.set = function (varName, replaceWith) {
this.replace(varName, replaceWith);
};
MockManager.prototype.getMockInstance = function () {
return new this.stubClass();
};
MockManager.prototype.mockFunction = function (funcName, returns) {
var spy = sinon.stub();
spy.returns(returns);
this.replaceFunction(funcName, spy);
return spy;
};
MockManager.prototype.replaceFunction = function (funcName, newFunc) {
this.replace(funcName, newFunc);
};
MockManager.prototype.replace = function (name, arg) {
this.stubClass.prototype[name] = arg;
};
MockManager.prototype.getAllFunctionNames = function (obj) {
var funcNames = [];
do {
funcNames = funcNames.concat(Object.getOwnPropertyNames(obj.prototype));
obj = Object.getPrototypeOf(obj);
} while (obj && obj.prototype && obj.prototype !== Object.prototype);
return funcNames;
};
MockManager.prototype.createStubClass = function () {
var _this = this;
this.stubClass = (function () {
function class_1() {
return;
}
return class_1;
}());
this.getAllFunctionNames(this.original)
.forEach(function (funcName) {
_this.mock(funcName);
});
};
return MockManager;
}(manager_1.Manager));
exports.MockManager = MockManager;
//# sourceMappingURL=mock-manager.js.map