Skip to content

Instantly share code, notes, and snippets.

@pivotal-creationmix
Created August 25, 2010 06:34
Show Gist options
  • Save pivotal-creationmix/548969 to your computer and use it in GitHub Desktop.
Save pivotal-creationmix/548969 to your computer and use it in GitHub Desktop.
beforeEach(function() {
this.addMatchers({
toBeVisible: function() {
var actual = $(this.actual);
var basicMessage = "be visible";
return this._reportWithNonExistant(
actual.is(':visible'),
basicMessage,
"Expected '$(" + actual.selector + ")' to " + basicMessage + " but was not"
);
},
toNotBeVisible : function() {
var actual = $(this.actual);
var basicMessage = "be hidden";
return this._reportWithNonExistant(
!actual.is(':visible'),
basicMessage,
"Expected '$(" + actual.selector + ")' to " + basicMessage + " but was not"
);
},
toBeSameJqueryObjectAs : function(expected) {
var errorMessage = null;
var basicMessage = "to be the same jQuery object as '$(" + expected.selector + ")'";
if (!expected || !this.actual || this.actual.length != expected.length) {
errorMessage = "but was not";
} else {
for (var i = 0; i < expected.length; i++) {
if (this.actual.get(i) != expected.get(i)) {
errorMessage = "but were not";
break;
}
}
}
return this._reportWithNonExistant(errorMessage == null, basicMessage, "Expected '$(" + this.actual.selector + ")' to " + basicMessage + " " + errorMessage);
},
_reportWithNonExistant: function(test, basicMessage, fullMessage) {
var actual = $(this.actual);
if (actual.length > 0) {
if (test) {
return test
} else {
throw new Error(fullMessage)
}
} else {
throw new Error('Expected $("' + actual.selector + '") to ' + basicMessage + ' but the element did not exist.')
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment