@hirez_io/auto-spies-core
Version:
Create automatic spies from classes in tests, also for promises and observables, used as a common code for jasmine-auto-spies and jest-auto-spies
114 lines • 5.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFunctionAutoSpy = void 0;
var _1 = require(".");
var error_handler_1 = require("./errors/error-handler");
var args_map_1 = require("./args-map");
function createFunctionAutoSpy(functionName, addFrameworkMethodsToCalledWithObject, frameworkFunctionSpyFactory) {
var calledWithObject = {
wasConfigured: false,
argsToValuesMap: new args_map_1.ArgsMap(),
};
var mustBeCalledWithObject = {
wasConfigured: false,
argsToValuesMap: new args_map_1.ArgsMap(),
};
var valueContainer = {
value: undefined,
};
// Function to pass to the specific testing library to call
// whenever someone calls the spied on method
function spyFunctionImpl() {
// >>> Add "per call" logic here
var actualArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
actualArgs[_i] = arguments[_i];
}
return returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs, functionName);
}
var _a = frameworkFunctionSpyFactory(functionName, spyFunctionImpl), functionSpy = _a.functionSpy, objectToAddSpyMethodsTo = _a.objectToAddSpyMethodsTo;
(0, _1.addPromiseHelpersToFunctionSpy)(objectToAddSpyMethodsTo, valueContainer);
(0, _1.addObservableHelpersToFunctionSpy)(objectToAddSpyMethodsTo, valueContainer);
functionSpy.calledWith = function () {
var calledWithArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
calledWithArgs[_i] = arguments[_i];
}
return addMethodsToCalledWith(calledWithObject, calledWithArgs, addFrameworkMethodsToCalledWithObject);
};
functionSpy.mustBeCalledWith = function () {
var calledWithArgs = [];
for (var _i = 0; _i < arguments.length; _i++) {
calledWithArgs[_i] = arguments[_i];
}
return addMethodsToCalledWith(mustBeCalledWithObject, calledWithArgs, addFrameworkMethodsToCalledWithObject);
};
return functionSpy;
}
exports.createFunctionAutoSpy = createFunctionAutoSpy;
function addMethodsToCalledWith(calledWith, calledWithArgs, addFrameworkMethodsToCalledWithObject) {
calledWith.wasConfigured = true;
calledWith = addFrameworkMethodsToCalledWithObject(calledWith, calledWithArgs);
calledWith = (0, _1.addPromiseHelpersToCalledWithObject)(calledWith, calledWithArgs);
calledWith = (0, _1.addObservableHelpersToCalledWithObject)(calledWith, calledWithArgs);
return calledWith;
}
function returnTheCorrectFakeValue(calledWithObject, mustBeCalledWithObject, valueContainer, actualArgs, functionName) {
var _a, _b, _c;
if (calledWithObject.wasConfigured) {
var expectedReturnValueContainer = calledWithObject.argsToValuesMap.get(actualArgs);
/* istanbul ignore else */
if (expectedReturnValueContainer) {
if (expectedReturnValueContainer._isRejectedPromise) {
return Promise.reject(expectedReturnValueContainer.value);
}
if ((_a = expectedReturnValueContainer.valuesPerCalls) === null || _a === void 0 ? void 0 : _a.length) {
return getNextCallValue(expectedReturnValueContainer);
}
return expectedReturnValueContainer.value;
}
}
if (mustBeCalledWithObject.wasConfigured) {
var expectedReturnValueContainer = mustBeCalledWithObject.argsToValuesMap.get(actualArgs);
/* istanbul ignore else */
if (expectedReturnValueContainer) {
if (expectedReturnValueContainer._isRejectedPromise) {
return Promise.reject(expectedReturnValueContainer.value);
}
if ((_b = expectedReturnValueContainer.valuesPerCalls) === null || _b === void 0 ? void 0 : _b.length) {
return getNextCallValue(expectedReturnValueContainer);
}
return expectedReturnValueContainer.value;
}
error_handler_1.errorHandler.throwArgumentsError(actualArgs, functionName);
}
if (valueContainer._isRejectedPromise) {
return Promise.reject(valueContainer.value);
}
if ((_c = valueContainer.valuesPerCalls) === null || _c === void 0 ? void 0 : _c.length) {
return getNextCallValue(valueContainer);
}
return valueContainer.value;
}
function getNextCallValue(valueContainer) {
var _a;
/* istanbul ignore next */
if ((_a = valueContainer.valuesPerCalls) === null || _a === void 0 ? void 0 : _a.length) {
var wrappedValueConfigForNextCall_1 = valueContainer.valuesPerCalls.shift();
/* istanbul ignore next */
var returnedValue = wrappedValueConfigForNextCall_1 === null || wrappedValueConfigForNextCall_1 === void 0 ? void 0 : wrappedValueConfigForNextCall_1.wrappedValue;
/* istanbul ignore else */
if (wrappedValueConfigForNextCall_1 && wrappedValueConfigForNextCall_1.delay) {
// if it has a delay at this point, it must be a promise
returnedValue = returnedValue.then(function (value) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve(value);
}, wrappedValueConfigForNextCall_1.delay);
});
});
}
return returnedValue;
}
}
//# sourceMappingURL=create-function-auto-spy.js.map