safe-mock
Version:
Type Safe Mocking Library written for Typescript and Javascript
62 lines (61 loc) • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ArgumentInvocation_1 = require("./ArgumentInvocation");
var CallsDontMatchError_1 = require("./CallsDontMatchError");
var Verifier = /** @class */ (function () {
function Verifier(repo, propertyKey) {
this.repo = repo;
this.propertyKey = propertyKey;
this.never = new NeverVerifier(repo, propertyKey);
}
//noinspection JSUnusedGlobalSymbols
Verifier.prototype.called = function () {
var calls = this.repo.lookupCalls(this.propertyKey);
if (calls.length === 0)
throw new Error(this.propertyKey + " was never called");
};
//noinspection JSUnusedGlobalSymbols
Verifier.prototype.calledWith = function () {
var expectedArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
expectedArgs[_i] = arguments[_i];
}
var expectedArgumentInvocation = new ArgumentInvocation_1.default(expectedArgs);
var calls = this.repo.lookupCalls(this.propertyKey);
var callIfExists = calls
.filter(function (call) { return expectedArgumentInvocation.equivalentTo(call); })[0];
if (callIfExists == undefined) {
throw new CallsDontMatchError_1.default(expectedArgumentInvocation, calls, this.propertyKey);
}
};
return Verifier;
}());
exports.Verifier = Verifier;
var NeverVerifier = /** @class */ (function () {
function NeverVerifier(repo, propertyKey) {
this.repo = repo;
this.propertyKey = propertyKey;
this.never = this;
}
//noinspection JSUnusedGlobalSymbols
NeverVerifier.prototype.called = function () {
var calls = this.repo.lookupCalls(this.propertyKey);
if (calls.length !== 0)
throw new Error(this.propertyKey + " was called " + calls.length + " times");
};
//noinspection JSUnusedGlobalSymbols
NeverVerifier.prototype.calledWith = function () {
var expectedArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
expectedArgs[_i] = arguments[_i];
}
var expectedArgumentInvocation = new ArgumentInvocation_1.default(expectedArgs);
var calls = this.repo.lookupCalls(this.propertyKey);
var callsWithMatchingArgs = calls
.filter(function (expectedCall) { return expectedArgumentInvocation.equivalentTo(expectedCall); });
if (callsWithMatchingArgs.length !== 0) {
throw new Error(this.propertyKey + " was called " + callsWithMatchingArgs.length + " times with " + expectedArgumentInvocation.prettyPrint());
}
};
return NeverVerifier;
}());