* Top level private APIs (e.g. jasmine.private.whatever) are no longer exposed * jasmineRequire is no longer exposed * core is self-booting * Globals are automatically created in browsers. (They can subsequently be removed by user code if desired.) * Globals are *not* automatically created in Node. An installGlobals function is exported instead. The jasmine package calls installGlobals unless configured not to do so. * In Node, the same instance is returned each time jasmine-core is imported. A reset function is exported. It effectively resets all state by discarding the env and creating a new one. This allows mulitple sequential runs within the same process to be independent of each other, but does not allow multiple concurrent runs. (That probably never worked anyway.) Fixes #2094
86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
getJasmineRequireObj().SpyFactory = function(j$, private$) {
|
|
'use strict';
|
|
|
|
function SpyFactory(
|
|
getCustomStrategies,
|
|
getDefaultStrategyFn,
|
|
getMatchersUtil
|
|
) {
|
|
this.createSpy = function(name, originalFn) {
|
|
if (private$.isFunction(name) && originalFn === undefined) {
|
|
originalFn = name;
|
|
name = originalFn.name;
|
|
}
|
|
|
|
return private$.Spy(name, getMatchersUtil(), {
|
|
originalFn,
|
|
customStrategies: getCustomStrategies(),
|
|
defaultStrategyFn: getDefaultStrategyFn()
|
|
});
|
|
};
|
|
|
|
this.createSpyObj = function(baseName, methodNames, propertyNames) {
|
|
const baseNameIsCollection =
|
|
private$.isObject(baseName) || Array.isArray(baseName);
|
|
|
|
if (baseNameIsCollection) {
|
|
propertyNames = methodNames;
|
|
methodNames = baseName;
|
|
baseName = 'unknown';
|
|
}
|
|
|
|
const obj = {};
|
|
|
|
const methods = normalizeKeyValues(methodNames);
|
|
for (let i = 0; i < methods.length; i++) {
|
|
const spy = (obj[methods[i][0]] = this.createSpy(
|
|
baseName + '.' + methods[i][0]
|
|
));
|
|
if (methods[i].length > 1) {
|
|
spy.and.returnValue(methods[i][1]);
|
|
}
|
|
}
|
|
|
|
const properties = normalizeKeyValues(propertyNames);
|
|
for (let i = 0; i < properties.length; i++) {
|
|
const descriptor = {
|
|
enumerable: true,
|
|
get: this.createSpy(baseName + '.' + properties[i][0] + '.get'),
|
|
set: this.createSpy(baseName + '.' + properties[i][0] + '.set')
|
|
};
|
|
if (properties[i].length > 1) {
|
|
descriptor.get.and.returnValue(properties[i][1]);
|
|
descriptor.set.and.returnValue(properties[i][1]);
|
|
}
|
|
Object.defineProperty(obj, properties[i][0], descriptor);
|
|
}
|
|
|
|
if (methods.length === 0 && properties.length === 0) {
|
|
throw new Error(
|
|
'createSpyObj requires a non-empty array or object of method names to create spies for'
|
|
);
|
|
}
|
|
|
|
return obj;
|
|
};
|
|
}
|
|
|
|
function normalizeKeyValues(object) {
|
|
const result = [];
|
|
if (Array.isArray(object)) {
|
|
for (let i = 0; i < object.length; i++) {
|
|
result.push([object[i]]);
|
|
}
|
|
} else if (private$.isObject(object)) {
|
|
for (const key in object) {
|
|
if (object.hasOwnProperty(key)) {
|
|
result.push([key, object[key]]);
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return SpyFactory;
|
|
};
|