Add resolveValue and rejectValue spy strategies
This commit is contained in:
@@ -76,7 +76,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#promiseLibrary
|
||||
* @type function
|
||||
* @default undefined
|
||||
*/
|
||||
promiseLibrary: undefined
|
||||
};
|
||||
|
||||
var currentSuite = function() {
|
||||
@@ -142,6 +151,15 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
if (configuration.hasOwnProperty('hideDisabled')) {
|
||||
config.hideDisabled = configuration.hideDisabled;
|
||||
}
|
||||
|
||||
if (configuration.hasOwnProperty('promiseLibrary')) {
|
||||
if (typeof configuration.promiseLibrary.resolve === 'function' &&
|
||||
typeof configuration.promiseLibrary.reject === 'function') {
|
||||
config.promiseLibrary = configuration.promiseLibrary;
|
||||
} else {
|
||||
throw new Error('Custom promise library missing `resolve`/`reject` functions');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -193,6 +211,10 @@ getJasmineRequireObj().Env = function(j$) {
|
||||
}
|
||||
};
|
||||
|
||||
this.getPromise = function() {
|
||||
return config.promiseLibrary;
|
||||
};
|
||||
|
||||
j$.Expectation.addCoreMatchers(j$.matchers);
|
||||
j$.Expectation.addAsyncCoreMatchers(j$.asyncMatchers);
|
||||
|
||||
|
||||
@@ -84,6 +84,44 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
||||
return this.getSpy();
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell the spy to return a promise resolving to the specified value when invoked.
|
||||
* @name SpyStrategy#resolveValue
|
||||
* @function
|
||||
* @param {*} value The value to return.
|
||||
*/
|
||||
SpyStrategy.prototype.resolveValue = function(value) {
|
||||
var Promise = j$.getPromise();
|
||||
|
||||
if (!Promise) {
|
||||
throw new Error('resolveValue is unavailable because the environment does not support promises.');
|
||||
}
|
||||
|
||||
this.plan = function() {
|
||||
return Promise.resolve(value);
|
||||
};
|
||||
return this.getSpy();
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell the spy to return a promise rejecting with the specified value when invoked.
|
||||
* @name SpyStrategy#rejectValue
|
||||
* @function
|
||||
* @param {*} value The value to return.
|
||||
*/
|
||||
SpyStrategy.prototype.rejectValue = function(value) {
|
||||
var Promise = j$.getPromise();
|
||||
|
||||
if (!Promise) {
|
||||
throw new Error('rejectValue is unavailable because the environment does not support promises.');
|
||||
}
|
||||
|
||||
this.plan = function() {
|
||||
return Promise.reject(value);
|
||||
};
|
||||
return this.getSpy();
|
||||
};
|
||||
|
||||
/**
|
||||
* Tell the spy to throw an error when invoked.
|
||||
* @name SpyStrategy#throwError
|
||||
|
||||
@@ -45,6 +45,10 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
||||
return env;
|
||||
};
|
||||
|
||||
j$.getPromise = function() {
|
||||
return j$.getEnv().getPromise() || j$.getGlobal().Promise;
|
||||
};
|
||||
|
||||
j$.isArray_ = function(value) {
|
||||
return j$.isA_('Array', value);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user