Navigation Menu

Skip to content

Commit

Permalink
Added jQuery selector matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
nathansobo committed May 17, 2008
1 parent 3925d7b commit de2b3ea
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/screw.matchers.js
Expand Up @@ -112,6 +112,34 @@ Screw.Matchers = (function($) {
failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not be false' : ' to be false');
}
},

match_selector: {
match: function(expected, actual) {
if (!(actual instanceof jQuery)) {
throw expected.toString() + " must be an instance of jQuery to match against a selector"
}

return actual.is(expected);
},

failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not match selector ' : ' to match selector ') + expected;
}
},

contain_selector: {
match: function(expected, actual) {
if (!(actual instanceof jQuery)) {
throw expected.toString() + " must be an instance of jQuery to match against a selector"
}

return actual.find(expected).length > 0;
},

failure_message: function(expected, actual, not) {
return 'expected ' + $.print(actual) + (not ? ' to not contain selector ' : ' to contain selector ') + expected;
}
}
}
})(jQuery);
45 changes: 45 additions & 0 deletions spec/matchers_spec.js
Expand Up @@ -187,6 +187,51 @@ Screw.Unit(function() {
});
});

describe('#match_selector', function() {
var elt;
before(function() {
elt = $("<div class='foo'></div>");
});

it("matches a jQuery element against the expected selector", function() {
expect(elt).to(match_selector, 'div.foo');
expect(elt).to_not(match_selector, 'div.bar');
});

describe(".failure_message", function() {
it("prints 'expected [actual] to (not) match selector [expected]", function() {
var message = false;
try { expect(elt).to(match_selector, 'div.bar') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div class="foo"> ]) to match selector div.bar');

try { expect(elt).to_not(match_selector, 'div.foo') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div class="foo"> ]) to not match selector div.foo');
});
});
});

describe('#contain_selector', function() {
var elt;
before(function() {
elt = $("<div><div class='foo'></div></div>");
});

it("matches a jQuery element against the expected selector", function() {
expect(elt).to(contain_selector, 'div.foo');
expect(elt).to_not(contain_selector, 'div.bar');
});

describe(".failure_message", function() {
it("prints 'expected [actual] to (not) match selector [expected]", function() {
var message = false;
try { expect(elt).to(contain_selector, 'div.bar') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div> ]) to contain selector div.bar');

try { expect(elt).to_not(contain_selector, 'div.foo') } catch(e) { message = e }
expect(message).to(equal, 'expected $([ <div> ]) to not contain selector div.foo');
});
});
});

});
});

0 comments on commit de2b3ea

Please sign in to comment.