Custom jasmine matchers
beforeEach(function() {
this.addMatchers({
toBeTrue: function() {
return this.actual === true;
},
toBeFalse: function() {
return this.actual === false;
},
toHaveClass: function(className) {
return this.actual.hasClass(className);
},
toBeHidden: function() {
return !this.actual.is(":visible");
},
toBeVisible: function() {
return this.actual.is(":visible");
},
toExist: function() {
return this.actual.length > 0;
},
toHaveText: function(text) {
return this.actual.text().trim() === text;
},
toBeEmpty: function() {
return this.actual.length === 0;
},
toBeObject: function() {
return compareConstructor(this.actual.constructor, Object);
},
toBeArray: function() {
return compareConstructor(this.actual.constructor, Array);
},
toBeString: function() {
return compareConstructor(this.actual.constructor, String);
},
toBeFunction: function() {
return compareConstructor(this.actual, Function);
},
toBeTypeof: function(b) {
return compareConstructor(this.actual, b);
},
toBeJqueryWrapped: function(selector) {
if (selector && this.actual && this.actual.selector !== selector) return false;
return checkElementExistence(this.actual);
},
toHaveAttribute: function(attr, value) {
if (!value) {
return !!this.actual.attr(attr);
} else {
return this.actual.attr(attr) === value;
}
}
});
function compareConstructor(a, b) {
if (!a) {
return false;
}
return a.constructor == b;
}
function checkElementExistence(element) {
if (typeof element === "undefined") return false;
if (typeof element.selector === "undefined") return false;
if (!element.length) return false;
return compareConstructor(element, jQuery);
}
});