Cleanup spy promise strategies to be more consistent with Jasmine at large
- Rename `resolveWith` to `resolveTo` to match `toBeResolvedTo` - No longer wrap non-Errors in `rejectWith` - Fixes #1715
This commit is contained in:
@@ -6989,12 +6989,12 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Tell the spy to return a promise resolving to the specified value when invoked.
|
* Tell the spy to return a promise resolving to the specified value when invoked.
|
||||||
* @name SpyStrategy#resolveWith
|
* @name SpyStrategy#resolveTo
|
||||||
* @function
|
* @function
|
||||||
* @param {*} value The value to return.
|
* @param {*} value The value to return.
|
||||||
*/
|
*/
|
||||||
this.resolveWith = function(value) {
|
this.resolveTo = function(value) {
|
||||||
var Promise = requirePromise('resolveWith');
|
var Promise = requirePromise('resolveTo');
|
||||||
self.plan = function() {
|
self.plan = function() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
};
|
};
|
||||||
@@ -7009,10 +7009,9 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
*/
|
*/
|
||||||
this.rejectWith = function(value) {
|
this.rejectWith = function(value) {
|
||||||
var Promise = requirePromise('rejectWith');
|
var Promise = requirePromise('rejectWith');
|
||||||
var error = value instanceof Error ? value : new Error(value);
|
|
||||||
|
|
||||||
self.plan = function() {
|
self.plan = function() {
|
||||||
return Promise.reject(error);
|
return Promise.reject(value);
|
||||||
};
|
};
|
||||||
return self.getSpy();
|
return self.getSpy();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ describe('Spies', function() {
|
|||||||
it('works with global Promise library when available', function(done) {
|
it('works with global Promise library when available', function(done) {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
var spy = env.createSpy('foo').and.resolveWith(42);
|
var spy = env.createSpy('foo').and.resolveTo(42);
|
||||||
spy()
|
spy()
|
||||||
.then(function(result) {
|
.then(function(result) {
|
||||||
expect(result).toEqual(42);
|
expect(result).toEqual(42);
|
||||||
@@ -225,7 +225,7 @@ describe('Spies', function() {
|
|||||||
customPromise.resolve.and.returnValue('resolved');
|
customPromise.resolve.and.returnValue('resolved');
|
||||||
env.configure({ Promise: customPromise });
|
env.configure({ Promise: customPromise });
|
||||||
|
|
||||||
var spy = env.createSpy('foo').and.resolveWith(42);
|
var spy = env.createSpy('foo').and.resolveTo(42);
|
||||||
expect(spy()).toEqual('resolved');
|
expect(spy()).toEqual('resolved');
|
||||||
expect(customPromise.resolve).toHaveBeenCalledWith(42);
|
expect(customPromise.resolve).toHaveBeenCalledWith(42);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -117,7 +117,7 @@ describe('SpyStrategy', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#resolveWith', function() {
|
describe('#resolveTo', function() {
|
||||||
it('allows a resolved promise to be returned', function(done) {
|
it('allows a resolved promise to be returned', function(done) {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
@@ -130,7 +130,7 @@ describe('SpyStrategy', function() {
|
|||||||
getPromise: getPromise
|
getPromise: getPromise
|
||||||
});
|
});
|
||||||
|
|
||||||
spyStrategy.resolveWith(37);
|
spyStrategy.resolveTo(37);
|
||||||
spyStrategy
|
spyStrategy
|
||||||
.exec()
|
.exec()
|
||||||
.then(function(returnValue) {
|
.then(function(returnValue) {
|
||||||
@@ -145,9 +145,9 @@ describe('SpyStrategy', function() {
|
|||||||
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
spyStrategy = new jasmineUnderTest.SpyStrategy({ fn: originalFn });
|
||||||
|
|
||||||
expect(function() {
|
expect(function() {
|
||||||
spyStrategy.resolveWith(37);
|
spyStrategy.resolveTo(37);
|
||||||
}).toThrowError(
|
}).toThrowError(
|
||||||
'resolveWith requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'
|
'resolveTo requires global Promise, or `Promise` configured with `jasmine.getEnv().configure()`'
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -176,7 +176,7 @@ describe('SpyStrategy', function() {
|
|||||||
.catch(done.fail);
|
.catch(done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('allows a non-Error to be rejected, wrapping it in an exception when executed', function(done) {
|
it('allows a non-Error to be rejected', function(done) {
|
||||||
jasmine.getEnv().requirePromises();
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
var originalFn = jasmine.createSpy('original'),
|
var originalFn = jasmine.createSpy('original'),
|
||||||
@@ -193,7 +193,7 @@ describe('SpyStrategy', function() {
|
|||||||
.exec()
|
.exec()
|
||||||
.then(done.fail)
|
.then(done.fail)
|
||||||
.catch(function(error) {
|
.catch(function(error) {
|
||||||
expect(error).toEqual(new Error('oops'));
|
expect(error).toEqual('oops');
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(done.fail);
|
.catch(done.fail);
|
||||||
|
|||||||
@@ -46,12 +46,12 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Tell the spy to return a promise resolving to the specified value when invoked.
|
* Tell the spy to return a promise resolving to the specified value when invoked.
|
||||||
* @name SpyStrategy#resolveWith
|
* @name SpyStrategy#resolveTo
|
||||||
* @function
|
* @function
|
||||||
* @param {*} value The value to return.
|
* @param {*} value The value to return.
|
||||||
*/
|
*/
|
||||||
this.resolveWith = function(value) {
|
this.resolveTo = function(value) {
|
||||||
var Promise = requirePromise('resolveWith');
|
var Promise = requirePromise('resolveTo');
|
||||||
self.plan = function() {
|
self.plan = function() {
|
||||||
return Promise.resolve(value);
|
return Promise.resolve(value);
|
||||||
};
|
};
|
||||||
@@ -66,10 +66,9 @@ getJasmineRequireObj().SpyStrategy = function(j$) {
|
|||||||
*/
|
*/
|
||||||
this.rejectWith = function(value) {
|
this.rejectWith = function(value) {
|
||||||
var Promise = requirePromise('rejectWith');
|
var Promise = requirePromise('rejectWith');
|
||||||
var error = value instanceof Error ? value : new Error(value);
|
|
||||||
|
|
||||||
self.plan = function() {
|
self.plan = function() {
|
||||||
return Promise.reject(error);
|
return Promise.reject(value);
|
||||||
};
|
};
|
||||||
return self.getSpy();
|
return self.getSpy();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user