UNPKG

@johanblumenberg/ts-mockito

Version:
121 lines 7.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MethodStubVerificator = void 0; var MethodCallToStringConverter_1 = require("./utils/MethodCallToStringConverter"); var localSetTimeout = setTimeout; var MethodStubVerificator = (function () { function MethodStubVerificator(methodToVerify) { this.methodToVerify = methodToVerify; methodToVerify.watcher.invoked(); } MethodStubVerificator.prototype.actualCalls = function () { var calls = this.methodToVerify.mocker.getActionsByName(this.methodToVerify.methodName); return 'Actual calls:\n ' + MethodCallToStringConverter_1.MethodCallToStringConverter.convertActualCalls(calls).join('\n '); }; MethodStubVerificator.prototype.called = function () { this.atLeast(1); }; MethodStubVerificator.prototype.never = function () { this.times(0); }; MethodStubVerificator.prototype.once = function () { this.times(1); }; MethodStubVerificator.prototype.twice = function () { this.times(2); }; MethodStubVerificator.prototype.thrice = function () { this.times(3); }; MethodStubVerificator.prototype.times = function (value) { var allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.methodName, this.methodToVerify.matchers); if (value !== allMatchingActions.length) { var methodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(this.methodToVerify); var msg = "Expected \"" + methodToVerifyAsString + "to be called " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."; throw new Error(msg + '\n' + this.actualCalls()); } }; MethodStubVerificator.prototype.atLeast = function (value) { var allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.methodName, this.methodToVerify.matchers); if (value > allMatchingActions.length) { var methodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(this.methodToVerify); var msg = "Expected \"" + methodToVerifyAsString + "to be called at least " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."; throw new Error(msg + '\n' + this.actualCalls()); } }; MethodStubVerificator.prototype.atMost = function (value) { var allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.methodName, this.methodToVerify.matchers); if (value < allMatchingActions.length) { var methodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(this.methodToVerify); var msg = "Expected \"" + methodToVerifyAsString + "to be called at most " + value + " time(s). But has been called " + allMatchingActions.length + " time(s)."; throw new Error(msg + '\n' + this.actualCalls()); } }; MethodStubVerificator.prototype.calledBefore = function (method) { method.watcher.invoked(); var firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.methodName, this.methodToVerify.matchers); var secondMethodAction = method.mocker.getFirstMatchingAction(method.methodName, method.matchers); var mainMethodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(this.methodToVerify); var secondMethodAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(method); var errorBeginning = "Expected \"" + mainMethodToVerifyAsString + " to be called before " + secondMethodAsString; if (firstMethodAction && secondMethodAction) { if (!firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { throw new Error(errorBeginning + "but has been called after."); } } else if (firstMethodAction && !secondMethodAction) { throw new Error(errorBeginning + "but " + secondMethodAsString + "has never been called."); } else if (!firstMethodAction && secondMethodAction) { throw new Error(errorBeginning + "but " + mainMethodToVerifyAsString + "has never been called."); } else { throw new Error(errorBeginning + "but none of them has been called."); } }; MethodStubVerificator.prototype.calledAfter = function (method) { method.watcher.invoked(); var firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.methodName, this.methodToVerify.matchers); var secondMethodAction = method.mocker.getFirstMatchingAction(method.methodName, method.matchers); var mainMethodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(this.methodToVerify); var secondMethodAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(method); var errorBeginning = "Expected \"" + mainMethodToVerifyAsString + "to be called after " + secondMethodAsString; if (firstMethodAction && secondMethodAction) { if (firstMethodAction.hasBeenCalledBefore(secondMethodAction)) { throw new Error(errorBeginning + "but has been called before."); } } else if (firstMethodAction && !secondMethodAction) { throw new Error(errorBeginning + "but " + secondMethodAsString + "has never been called."); } else if (!firstMethodAction && secondMethodAction) { throw new Error(errorBeginning + "but " + mainMethodToVerifyAsString + "has never been called."); } else { throw new Error(errorBeginning + "but none of them has been called."); } }; MethodStubVerificator.prototype.timeout = function (ms) { var _this = this; return new Promise(function (resolve, reject) { var expired = Date.now() + ms; var check = function () { var allMatchingActions = _this.methodToVerify.mocker.getAllMatchingActions(_this.methodToVerify.methodName, _this.methodToVerify.matchers); if (allMatchingActions.length > 0) { resolve(); } else if (Date.now() >= expired) { var methodToVerifyAsString = MethodCallToStringConverter_1.MethodCallToStringConverter.convert(_this.methodToVerify); reject(new Error("Expected \"" + methodToVerifyAsString + "to be called within " + ms + " ms.")); } else { localSetTimeout(check, 1); } }; check(); }); }; return MethodStubVerificator; }()); exports.MethodStubVerificator = MethodStubVerificator; //# sourceMappingURL=MethodStubVerificator.js.map