Don't clobber previous custom plans with later plans

[finishes #37288941]
This commit is contained in:
Gregg Van Hove
2018-01-22 12:10:24 -08:00
parent 170a6dce76
commit f20f78f82b
8 changed files with 109 additions and 35 deletions

View File

@@ -6,18 +6,18 @@ getJasmineRequireObj().SpyFactory = function(j$) {
this.createSpy = function(name, originalFn) {
return j$.Spy(name, originalFn, getCustomStrategies());
};
this.createSpyObj = function(baseName, methodNames) {
var baseNameIsCollection = j$.isObject_(baseName) || j$.isArray_(baseName);
if (baseNameIsCollection && j$.util.isUndefined(methodNames)) {
methodNames = baseName;
baseName = 'unknown';
}
var obj = {};
var spiesWereSet = false;
if (j$.isArray_(methodNames)) {
for (var i = 0; i < methodNames.length; i++) {
obj[methodNames[i]] = self.createSpy(baseName + '.' + methodNames[i]);
@@ -32,11 +32,11 @@ getJasmineRequireObj().SpyFactory = function(j$) {
}
}
}
if (!spiesWereSet) {
throw 'createSpyObj requires a non-empty array or object of method names to create spies for';
}
return obj;
};
}

View File

@@ -20,20 +20,24 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
var k, cs = options.customStrategies || {};
for (k in cs) {
if (j$.util.has(cs, k) && !this[k]) {
this[k] = function() {
var plan = cs[k].apply(null, arguments);
if (!j$.isFunction_(plan)) {
throw new Error('Spy strategy must return a function');
}
this.plan = plan;
return this.getSpy();
};
this[k] = createCustomPlan(cs[k]);
}
}
}
function createCustomPlan(factory) {
return function() {
var plan = factory.apply(null, arguments);
if (!j$.isFunction_(plan)) {
throw new Error('Spy strategy must return a function');
}
this.plan = plan;
return this.getSpy();
};
}
/**
* Execute the current spy strategy.
* @name SpyStrategy#exec

View File

@@ -280,5 +280,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.createSpyObj(baseName, methodNames);
};
/**
* Add a custom spy strategy for the current scope of specs.
*
* _Note:_ This is only callable from within a {@link beforeEach}, {@link it}, or {@link beforeAll}.
* @name jasmine.addSpyStrategy
* @function
* @param {String} name - The name of the strategy (i.e. what you call from `and`)
* @param {Function} factory - Factory function that returns the plan to be executed.
*/
jasmine.addSpyStrategy = function(name, factory) {
return env.addSpyStrategy(identifier, factory);
};
return jasmineInterface;
};