Skip to content
Browse files

Added jQuery selector matchers

  • Loading branch information...
1 parent 3925d7b commit de2b3eaaa9ee61a2d9c80a14593256de239fc3d4 @nathansobo nathansobo committed May 16, 2008
Showing with 73 additions and 0 deletions.
  1. +28 −0 lib/screw.matchers.js
  2. +45 −0 spec/matchers_spec.js
View
28 lib/screw.matchers.js
@@ -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);
View
45 spec/matchers_spec.js
@@ -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.
Something went wrong with that request. Please try again.