safe-mock
Version:
Type Safe Mocking Library written for Typescript and Javascript
54 lines (53 loc) • 2.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var StubbedActionMatcher_1 = require("./StubbedActionMatcher");
var LookupResult_1 = require("./LookupResult");
var WhyNoReturnValueMatched_1 = require("./WhyNoReturnValueMatched");
var StubbedActionMatcherRepo = /** @class */ (function () {
function StubbedActionMatcherRepo() {
this.stubbedActionMatcherMap = {};
this.callMap = {};
}
StubbedActionMatcherRepo.prototype.recordAndFindMatch = function (propertyKey, argsToMatch) {
this.recordCall(propertyKey, argsToMatch);
var stubbedActionMatchers = this.stubbedActionMatcherMap[propertyKey] || [];
var lastMatchedMatcher = stubbedActionMatchers
.filter(function (matcher) { return matcher.match(argsToMatch); })
.reverse()[0];
if (lastMatchedMatcher) {
return LookupResult_1.default.returnValueFound(lastMatchedMatcher);
}
else {
return LookupResult_1.default.noReturnValueMatched(new WhyNoReturnValueMatched_1.default(argsToMatch, stubbedActionMatchers, propertyKey));
}
};
StubbedActionMatcherRepo.prototype.setStubbedActionForArgs = function (propertyKey, argumentInvocation, stubbedAction) {
this.deleteCallRecord(propertyKey, argumentInvocation);
this.setStubbedActionMatcher(propertyKey, StubbedActionMatcher_1.default.forArgs(argumentInvocation, stubbedAction));
};
StubbedActionMatcherRepo.prototype.setStubbedActionForAnyArgs = function (propertyKey, stubbedAction) {
this.setStubbedActionMatcher(propertyKey, StubbedActionMatcher_1.default.anyArgs(stubbedAction));
};
StubbedActionMatcherRepo.prototype.setStubbedActionMatcher = function (propertyKey, stubbedActionMatcher) {
if (!this.stubbedActionMatcherMap[propertyKey])
this.stubbedActionMatcherMap[propertyKey] = [];
this.stubbedActionMatcherMap[propertyKey].push(stubbedActionMatcher);
};
StubbedActionMatcherRepo.prototype.lookupCalls = function (propertyKey) {
return this.callMap[propertyKey] || [];
};
StubbedActionMatcherRepo.prototype.recordCall = function (propertyKey, argsToMatch) {
this.callMap[propertyKey] = (this.callMap[propertyKey] || []);
this.callMap[propertyKey].push(argsToMatch);
};
StubbedActionMatcherRepo.prototype.resetPropertyKey = function (propertyKey) {
this.stubbedActionMatcherMap[propertyKey] = [];
this.callMap[propertyKey] = [];
};
StubbedActionMatcherRepo.prototype.deleteCallRecord = function (propertyKey, argumentInvocation) {
this.callMap[propertyKey] = (this.callMap[propertyKey] || []);
this.callMap[propertyKey] = this.callMap[propertyKey].filter(function (call) { return !call.equivalentTo(argumentInvocation); });
};
return StubbedActionMatcherRepo;
}());
exports.StubbedActionMatcherRepo = StubbedActionMatcherRepo;