Add resolveValue and rejectValue spy strategies

This commit is contained in:
Elliot Nelson
2019-05-05 08:08:34 -04:00
parent afb24d1050
commit 72aa0bbe4d
6 changed files with 153 additions and 1 deletions

View File

@@ -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