Merge branch 'enelson/spystrategies' of https://github.com/elliot-nelson/jasmine into elliot-nelson-enelson/spystrategies
- Merges #1688 from @enelson - See #1590
This commit is contained in:
@@ -10,6 +10,7 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
|
||||
var self = this;
|
||||
var global = options.global || j$.getGlobal();
|
||||
var customPromise;
|
||||
|
||||
var totalSpecsDefined = 0;
|
||||
|
||||
@@ -76,7 +77,16 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
* @type Boolean
|
||||
* @default false
|
||||
*/
|
||||
hideDisabled: false
|
||||
hideDisabled: false,
|
||||
/**
|
||||
* Set to provide a custom promise library that Jasmine will use if it needs
|
||||
* to create a promise. If not set, it will default to whatever global Promise
|
||||
* library is available (if any).
|
||||
* @name Configuration#Promise
|
||||
* @type function
|
||||
* @default undefined
|
||||
*/
|
||||
Promise: undefined
|
||||
};
|
||||
|
||||
var currentSuite = function() {
|
||||
@@ -142,6 +152,15 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
if (configuration.hasOwnProperty('hideDisabled')) {
|
||||
config.hideDisabled = configuration.hideDisabled;
|
||||
}
|
||||
|
||||
if (configuration.hasOwnProperty('Promise')) {
|
||||
if (typeof configuration.Promise.resolve === 'function' &&
|
||||
typeof configuration.Promise.reject === 'function') {
|
||||
customPromise = configuration.Promise;
|
||||
} else {
|
||||
throw new Error('Custom promise library missing `resolve`/`reject` functions');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -620,15 +639,20 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
reporter.clearReporters();
|
||||
};
|
||||
|
||||
var spyFactory = new j$.SpyFactory(function() {
|
||||
var runnable = currentRunnable();
|
||||
var spyFactory = new j$.SpyFactory(
|
||||
function getCustomStrategies() {
|
||||
var runnable = currentRunnable();
|
||||
|
||||
if (runnable) {
|
||||
return runnableResources[runnable.id].customSpyStrategies;
|
||||
if (runnable) {
|
||||
return runnableResources[runnable.id].customSpyStrategies;
|
||||
}
|
||||
|
||||
return {};
|
||||
},
|
||||
function getPromise() {
|
||||
return customPromise || global.Promise;
|
||||
}
|
||||
|
||||
return {};
|
||||
});
|
||||
);
|
||||
|
||||
var spyRegistry = new j$.SpyRegistry({
|
||||
currentSpies: function() {
|
||||
|
||||
@@ -13,7 +13,7 @@ getJasmineRequireObj().Spy = function (j$) {
|
||||
* @constructor
|
||||
* @name Spy
|
||||
*/
|
||||
function Spy(name, originalFn, customStrategies) {
|
||||
function Spy(name, originalFn, customStrategies, getPromise) {
|
||||
var numArgs = (typeof originalFn === 'function' ? originalFn.length : 0),
|
||||
wrapper = makeFunc(numArgs, function () {
|
||||
return spy.apply(this, Array.prototype.slice.call(arguments));
|
||||
@@ -24,7 +24,8 @@ getJasmineRequireObj().Spy = function (j$) {
|
||||
getSpy: function () {
|
||||
return wrapper;
|
||||
},
|
||||
customStrategies: customStrategies
|
||||
customStrategies: customStrategies,
|
||||
getPromise: getPromise
|
||||
}),
|
||||
callTracker = new j$.CallTracker(),
|
||||
spy = function () {
|
||||
@@ -98,7 +99,6 @@ getJasmineRequireObj().Spy = function (j$) {
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
|
||||
function SpyStrategyDispatcher(strategyArgs) {
|
||||
var baseStrategy = new j$.SpyStrategy(strategyArgs);
|
||||
var argsStrategies = new StrategyDict(function() {
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
getJasmineRequireObj().SpyFactory = function(j$) {
|
||||
|
||||
function SpyFactory(getCustomStrategies) {
|
||||
function SpyFactory(getCustomStrategies, getPromise) {
|
||||
var self = this;
|
||||
|
||||
this.createSpy = function(name, originalFn) {
|
||||
return j$.Spy(name, originalFn, getCustomStrategies());
|
||||
return j$.Spy(name, originalFn, getCustomStrategies(), getPromise);
|
||||
};
|
||||
|
||||
this.createSpyObj = function(baseName, methodNames) {
|
||||
|
||||
@@ -6,6 +6,8 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
||||
function SpyStrategy(options) {
|
||||
options = options || {};
|
||||
|
||||
var self = this;
|
||||
|
||||
/**
|
||||
* Get the identifying information for the spy.
|
||||
* @name SpyStrategy#identity
|
||||
@@ -23,6 +25,48 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
||||
this[k] = createCustomPlan(cs[k]);
|
||||
}
|
||||
}
|
||||
|
||||
var getPromise = (typeof options.getPromise === 'function') ? options.getPromise : function() {};
|
||||
|
||||
var requirePromise = function(name) {
|
||||
var Promise = getPromise();
|
||||
|
||||
if (!Promise) {
|
||||
throw new Error(name + ' requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`');
|
||||
}
|
||||
|
||||
return Promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell the spy to return a promise resolving to the specified value when invoked.
|
||||
* @name SpyStrategy#resolveWith
|
||||
* @function
|
||||
* @param {*} value The value to return.
|
||||
*/
|
||||
this.resolveWith = function(value) {
|
||||
var Promise = requirePromise('resolveWith');
|
||||
self.plan = function() {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
return self.getSpy();
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell the spy to return a promise rejecting with the specified value when invoked.
|
||||
* @name SpyStrategy#rejectWith
|
||||
* @function
|
||||
* @param {*} value The value to return.
|
||||
*/
|
||||
this.rejectWith = function(value) {
|
||||
var Promise = requirePromise('rejectWith');
|
||||
var error = (value instanceof Error) ? value : new Error(value);
|
||||
|
||||
self.plan = function() {
|
||||
return Promise.reject(error);
|
||||
};
|
||||
return self.getSpy();
|
||||
};
|
||||
}
|
||||
|
||||
function createCustomPlan(factory) {
|
||||
|
||||
Reference in New Issue
Block a user