Merge branch 'tobepending' of https://github.com/DCtheTall/jasmine
* Merges #1808 from @DCtheTall * Fixes #1803
This commit is contained in:
@@ -4054,6 +4054,32 @@ getJasmineRequireObj().GlobalErrors = function(j$) {
|
|||||||
return GlobalErrors;
|
return GlobalErrors;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
getJasmineRequireObj().toBePending = function(j$) {
|
||||||
|
/**
|
||||||
|
* Expect a promise to be pending, ie. the promise is neither resolved nor rejected.
|
||||||
|
* @function
|
||||||
|
* @async
|
||||||
|
* @name async-matchers#toBePending
|
||||||
|
* @since 3.5.1 (should this be the next version or the version when it was added?)
|
||||||
|
* @example
|
||||||
|
* await expectAsync(aPromise).toBePending();
|
||||||
|
*/
|
||||||
|
return function toBePending() {
|
||||||
|
return {
|
||||||
|
compare: function(actual) {
|
||||||
|
if (!j$.isPromiseLike(actual)) {
|
||||||
|
throw new Error('Expected toBePending to be called on a promise.');
|
||||||
|
}
|
||||||
|
var want = {};
|
||||||
|
return Promise.race([actual, Promise.resolve(want)]).then(
|
||||||
|
function(got) { return {pass: want === got}; },
|
||||||
|
function() { return {pass: false}; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
getJasmineRequireObj().toBeRejected = function(j$) {
|
getJasmineRequireObj().toBeRejected = function(j$) {
|
||||||
/**
|
/**
|
||||||
* Expect a promise to be rejected.
|
* Expect a promise to be rejected.
|
||||||
@@ -5087,6 +5113,7 @@ getJasmineRequireObj().ObjectPath = function(j$) {
|
|||||||
|
|
||||||
getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
|
getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
|
||||||
var availableMatchers = [
|
var availableMatchers = [
|
||||||
|
'toBePending',
|
||||||
'toBeResolved',
|
'toBeResolved',
|
||||||
'toBeRejected',
|
'toBeRejected',
|
||||||
'toBeResolvedTo',
|
'toBeResolvedTo',
|
||||||
|
|||||||
51
spec/core/matchers/async/toBePendingSpec.js
Normal file
51
spec/core/matchers/async/toBePendingSpec.js
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
describe('toBePending', function() {
|
||||||
|
it('passes if the actual promise is pending', function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
||||||
|
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
|
||||||
|
actual = new Promise(function() {});
|
||||||
|
|
||||||
|
return matcher.compare(actual).then(function(result) {
|
||||||
|
expect(result).toEqual(jasmine.objectContaining({pass: true}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails if the actual promise is resolved', function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
||||||
|
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
|
||||||
|
actual = Promise.resolve();
|
||||||
|
|
||||||
|
return matcher.compare(actual).then(function(result) {
|
||||||
|
expect(result).toEqual(jasmine.objectContaining({pass: false}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails if the actual promise is rejected', function() {
|
||||||
|
jasmine.getEnv().requirePromises();
|
||||||
|
|
||||||
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
||||||
|
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
|
||||||
|
actual = Promise.reject(new Error('promise was rejected'));
|
||||||
|
|
||||||
|
return matcher.compare(actual).then(function(result) {
|
||||||
|
expect(result).toEqual(jasmine.objectContaining({pass: false}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('fails if actual is not a promise', function() {
|
||||||
|
var matchersUtil = new jasmineUnderTest.MatchersUtil(),
|
||||||
|
matcher = jasmineUnderTest.asyncMatchers.toBePending(matchersUtil),
|
||||||
|
actual = 'not a promise';
|
||||||
|
|
||||||
|
function f() {
|
||||||
|
return matcher.compare(actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
expect(f).toThrowError(
|
||||||
|
'Expected toBePending to be called on a promise.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
25
src/core/matchers/async/toBePending.js
Normal file
25
src/core/matchers/async/toBePending.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
getJasmineRequireObj().toBePending = function(j$) {
|
||||||
|
/**
|
||||||
|
* Expect a promise to be pending, ie. the promise is neither resolved nor rejected.
|
||||||
|
* @function
|
||||||
|
* @async
|
||||||
|
* @name async-matchers#toBePending
|
||||||
|
* @since 3.5.1 (should this be the next version or the version when it was added?)
|
||||||
|
* @example
|
||||||
|
* await expectAsync(aPromise).toBePending();
|
||||||
|
*/
|
||||||
|
return function toBePending() {
|
||||||
|
return {
|
||||||
|
compare: function(actual) {
|
||||||
|
if (!j$.isPromiseLike(actual)) {
|
||||||
|
throw new Error('Expected toBePending to be called on a promise.');
|
||||||
|
}
|
||||||
|
var want = {};
|
||||||
|
return Promise.race([actual, Promise.resolve(want)]).then(
|
||||||
|
function(got) { return {pass: want === got}; },
|
||||||
|
function() { return {pass: false}; }
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
|
getJasmineRequireObj().requireAsyncMatchers = function(jRequire, j$) {
|
||||||
var availableMatchers = [
|
var availableMatchers = [
|
||||||
|
'toBePending',
|
||||||
'toBeResolved',
|
'toBeResolved',
|
||||||
'toBeRejected',
|
'toBeRejected',
|
||||||
'toBeResolvedTo',
|
'toBeResolvedTo',
|
||||||
|
|||||||
Reference in New Issue
Block a user