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

@@ -1,5 +1,5 @@
/* /*
Copyright (c) 2008-2017 Pivotal Labs Copyright (c) 2008-2018 Pivotal Labs
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

View File

@@ -1,5 +1,5 @@
/* /*
Copyright (c) 2008-2017 Pivotal Labs Copyright (c) 2008-2018 Pivotal Labs
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

View File

@@ -4916,6 +4916,19 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.createSpyObj(baseName, methodNames); 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; return jasmineInterface;
}; };
@@ -5284,20 +5297,24 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
var k, cs = options.customStrategies || {}; var k, cs = options.customStrategies || {};
for (k in cs) { for (k in cs) {
if (j$.util.has(cs, k) && !this[k]) { if (j$.util.has(cs, k) && !this[k]) {
this[k] = function() { this[k] = createCustomPlan(cs[k]);
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();
};
} }
} }
} }
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. * Execute the current spy strategy.
* @name SpyStrategy#exec * @name SpyStrategy#exec

View File

@@ -1,5 +1,5 @@
/* /*
Copyright (c) 2008-2017 Pivotal Labs Copyright (c) 2008-2018 Pivotal Labs
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

View File

@@ -95,4 +95,44 @@ describe('Custom Spy Strategies (Integration)', function() {
env.addReporter({ jasmineDone: jasmineDone }); env.addReporter({ jasmineDone: jasmineDone });
env.execute(); env.execute();
}); });
it('allows multiple custom strategies to be used', function(done) {
var plan1 = jasmine.createSpy('plan 1').and.returnValue(42),
strategy1 = jasmine.createSpy('strat 1').and.returnValue(plan1),
plan2 = jasmine.createSpy('plan 2').and.returnValue(24),
strategy2 = jasmine.createSpy('strat 2').and.returnValue(plan2),
specDone = jasmine.createSpy('specDone');
env.beforeEach(function() {
env.addSpyStrategy('frobnicate', strategy1);
env.addSpyStrategy('jiggle', strategy2);
});
env.it('frobnicates', function() {
plan1.calls.reset();
plan2.calls.reset();
var spy = env.createSpy('spy').and.frobnicate();
expect(spy()).toEqual(42);
expect(plan1).toHaveBeenCalled();
expect(plan2).not.toHaveBeenCalled();
});
env.it('jiggles', function() {
plan1.calls.reset();
plan2.calls.reset();
var spy = env.createSpy('spy').and.jiggle();
expect(spy()).toEqual(24);
expect(plan1).not.toHaveBeenCalled();
expect(plan2).toHaveBeenCalled();
});
function jasmineDone(result) {
expect(result.overallStatus).toEqual('passed');
expect(specDone.calls.count()).toBe(2);
done();
}
env.addReporter({ jasmineDone: jasmineDone, specDone: specDone });
env.execute();
});
}); });

View File

@@ -20,20 +20,24 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
var k, cs = options.customStrategies || {}; var k, cs = options.customStrategies || {};
for (k in cs) { for (k in cs) {
if (j$.util.has(cs, k) && !this[k]) { if (j$.util.has(cs, k) && !this[k]) {
this[k] = function() { this[k] = createCustomPlan(cs[k]);
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();
};
} }
} }
} }
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. * Execute the current spy strategy.
* @name SpyStrategy#exec * @name SpyStrategy#exec

View File

@@ -280,5 +280,18 @@ getJasmineRequireObj().interface = function(jasmine, env) {
return env.createSpyObj(baseName, methodNames); 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; return jasmineInterface;
}; };