Merge branch 'codymikol-assertAsync-promiseLike-support'
- Merges #1613 from @codymikol - Fixes #1612
This commit is contained in:
@@ -261,7 +261,11 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
||||
};
|
||||
|
||||
j$.isPromise = function(obj) {
|
||||
return typeof jasmineGlobal.Promise !== 'undefined' && obj && obj.constructor === jasmineGlobal.Promise;
|
||||
return typeof jasmineGlobal.Promise !== 'undefined' && !!obj && obj.constructor === jasmineGlobal.Promise;
|
||||
};
|
||||
|
||||
j$.isPromiseLike = function(obj) {
|
||||
return !!obj && j$.isFunction_(obj.then);
|
||||
};
|
||||
|
||||
j$.fnNameFor = function(func) {
|
||||
@@ -2160,7 +2164,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
||||
throw new Error('expectAsync is unavailable because the environment does not support promises.');
|
||||
}
|
||||
|
||||
if (!j$.isPromise(this.actual)) {
|
||||
if (!j$.isPromiseLike(this.actual)) {
|
||||
throw new Error('Expected expectAsync to be called with a promise.');
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,105 @@ describe("jasmineUnderTest.util", function() {
|
||||
});
|
||||
});
|
||||
|
||||
describe("promise utils", function () {
|
||||
|
||||
var mockNativePromise,
|
||||
mockPromiseLikeObject;
|
||||
|
||||
var mockPromiseLike = function () {this.then = function () {};};
|
||||
|
||||
beforeEach(function () {
|
||||
jasmine.getEnv().requirePromises();
|
||||
mockNativePromise = new Promise(function (res, rej) {});
|
||||
mockPromiseLikeObject = new mockPromiseLike();
|
||||
});
|
||||
|
||||
describe("isPromise", function () {
|
||||
|
||||
it("should return true when passed a native promise", function () {
|
||||
expect(jasmineUnderTest.isPromise(mockNativePromise)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for promise like objects", function () {
|
||||
expect(jasmineUnderTest.isPromise(mockPromiseLikeObject)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for strings", function () {
|
||||
expect(jasmineUnderTest.isPromise("hello")).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for numbers", function () {
|
||||
expect(jasmineUnderTest.isPromise(3)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for null", function () {
|
||||
expect(jasmineUnderTest.isPromise(null)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for undefined", function () {
|
||||
expect(jasmineUnderTest.isPromise(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for arrays", function () {
|
||||
expect(jasmineUnderTest.isPromise([])).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for objects", function () {
|
||||
expect(jasmineUnderTest.isPromise({})).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for boolean values", function () {
|
||||
expect(jasmineUnderTest.isPromise(true)).toBe(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("isPromiseLike", function () {
|
||||
|
||||
it("should return true when passed a native promise", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(mockNativePromise)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return true for promise like objects", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(mockPromiseLikeObject)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false if then is not a function", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike({then:{its:"Not a function :O"}})).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for strings", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike("hello")).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for numbers", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(3)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for null", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(null)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for undefined", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for arrays", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike([])).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for objects", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike({})).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for boolean values", function () {
|
||||
expect(jasmineUnderTest.isPromiseLike(true)).toBe(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("isUndefined", function() {
|
||||
it("reports if a variable is defined", function() {
|
||||
var a;
|
||||
|
||||
@@ -19,7 +19,7 @@ getJasmineRequireObj().AsyncExpectation = function(j$) {
|
||||
throw new Error('expectAsync is unavailable because the environment does not support promises.');
|
||||
}
|
||||
|
||||
if (!j$.isPromise(this.actual)) {
|
||||
if (!j$.isPromiseLike(this.actual)) {
|
||||
throw new Error('Expected expectAsync to be called with a promise.');
|
||||
}
|
||||
|
||||
|
||||
@@ -120,7 +120,11 @@ getJasmineRequireObj().base = function(j$, jasmineGlobal) {
|
||||
};
|
||||
|
||||
j$.isPromise = function(obj) {
|
||||
return typeof jasmineGlobal.Promise !== 'undefined' && obj && obj.constructor === jasmineGlobal.Promise;
|
||||
return typeof jasmineGlobal.Promise !== 'undefined' && !!obj && obj.constructor === jasmineGlobal.Promise;
|
||||
};
|
||||
|
||||
j$.isPromiseLike = function(obj) {
|
||||
return !!obj && j$.isFunction_(obj.then);
|
||||
};
|
||||
|
||||
j$.fnNameFor = function(func) {
|
||||
|
||||
Reference in New Issue
Block a user