Permalink
Browse files

Breaking change: change `contains` behavior, add `have`

The `contains` assertion now uses jQuery's `:contains` selector. The previous
behavior of the `contains` assertion is now handled by the `have` assertion,
corresponding to jQuery's `.has()` function.

Bump to 1.0.0-rc1.

Fixes #1.
  • Loading branch information...
1 parent 4de61d3 commit 29cd83caa29acca15d3da95a766960bd257067e6 @jfirebaugh jfirebaugh committed May 5, 2012
Showing with 92 additions and 23 deletions.
  1. +6 −0 History.md
  2. +13 −4 README.md
  3. +24 −4 chai-jquery.js
  4. +1 −1 package.json
  5. +47 −13 test/chai-jquery-spec.js
  6. +1 −1 test/index.html
View
@@ -0,0 +1,6 @@
+# 1.0.0-rc1
+
+* Compatibility with chai 1.0.0-rc1
+* Breaking change: the `contains` assertion now uses jQuery's `:contains`
+ selector. The previous behavior of the `contains` assertion is now handled by
+ the `have` assertion, corresponding to jQuery's `.has()` function.
View
@@ -106,11 +106,20 @@ not a jQuery object, or if `be` is not called as a function, the original implem
expect($('#empty')).to.be(':empty');
### `contain(selector)`
-Assert that the selection contains at least one instance of the given selector, using [`.find()`](http://api.jquery.com/find/).
-If the object asserted against is not a jQuery object, the original implementation will be called.
+Assert that the selection contains the given text, using [`:contains()`](http://api.jquery.com/contains-selector/).
+If the object asserted against is not a jQuery object, or if `contain` is not called as a function, the original
+implementation will be called.
- $('body').should.contain('h1');
- expect($('#content')).to.contain('section');
+ $('body').should.contain('text');
+ expect($('#content')).to.contain('text');
+
+### `have(selector)`
+Assert that the selection contains at least one element which has a descendant matching the given selector,
+using [`.has()`](http://api.jquery.com/has/). If the object asserted against is not a jQuery object, or if `have`
+is not called as a function, the original implementation will be called.
+
+ $('body').should.have('h1');
+ expect($('#content')).to.have('div');
## License
View
@@ -172,20 +172,40 @@
utils.overwriteProperty(chai.Assertion, 'contain', function (_super) {
return function () {
_super.apply(this, arguments);
- var contain = function (selector) {
+ var contain = function (text) {
var obj = flag(this, 'object');
if (obj instanceof jQuery) {
this.assert(
- obj.find(selector).length > 0
+ obj.is(':contains(\'' + text + '\')')
, 'expected #{this} to contain #{exp}'
, 'expected #{this} not to contain #{exp}'
- , selector
+ , text
);
}
};
contain.__proto__ = this;
return contain;
}
});
-}));
+ utils.overwriteProperty(chai.Assertion, 'have', function (_super) {
+ return function () {
+ _super.apply(this, arguments);
+ var have = function (selector) {
+ var obj = flag(this, 'object');
+ if (obj instanceof jQuery) {
+ this.assert(
+ // Using find() rather than has() to work around a jQuery bug:
+ // http://bugs.jquery.com/ticket/11706
+ obj.find(selector).length > 0
+ , 'expected #{this} to have #{exp}'
+ , 'expected #{this} not to have #{exp}'
+ , selector
+ );
+ }
+ };
+ have.__proto__ = this;
+ return have;
+ }
+ });
+}));
View
@@ -3,7 +3,7 @@
"name": "chai-jquery",
"description": "jQuery assertions for the chai assertion library",
"keywords": [ "test", "assertion", "assert", "testing", "jQuery" ],
- "version": "0.0.1",
+ "version": "1.0.0-rc1",
"repository": {
"type": "git",
"url": "https://github.com/jfirebaugh/chai-jquery"
View
@@ -11,14 +11,14 @@ describe("jQuery assertions", function(){
try {
obj();
- this.assert(false, 'expected ' + this.inspect + ' to fail');
+ this.assert(false, 'expected #{this} to fail');
} catch (err) {
this.assert(
err instanceof chai.AssertionError
- , 'expected ' + this.inspect + ' to fail, but it threw ' + inspect(err));
+ , 'expected #{this} to fail, but it threw ' + inspect(err));
this.assert(
err.message === message
- , 'expected ' + this.inspect + ' to fail with ' + inspect(message) + ', but got ' + inspect(err.message))
+ , 'expected #{this} to fail with ' + inspect(message) + ', but got ' + inspect(err.message))
}
return this;
@@ -512,29 +512,63 @@ describe("jQuery assertions", function(){
describe("contain", function(){
it("preserves existing behavior on non-jQuery objects", function(){
+ ("example text").should.contain('example');
({foo: 1, bar: 2}).should.contain.keys('foo');
});
+ var subject = $('<div><span>example text</span></div>');
+
+ it("passes when the selection contains the given text", function(){
+ subject.should.contain('example');
+ });
+
+ it("passes negated when the selection does not contain the given text", function(){
+ subject.should.not.contain('nonesuch');
+ });
+
+ it("fails when the selection does not contain the given text", function(){
+ (function(){
+ subject.should.contain('nonesuch');
+ }).should.fail("expected " + inspect(subject) + " to contain 'nonesuch'");
+ });
+
+ it("fails negated when the selection contains the given text", function(){
+ (function(){
+ subject.should.not.contain("example");
+ }).should.fail("expected " + inspect(subject) + " not to contain 'example'");
+ });
+
+ it("handles quotes", function(){
+ $('<div>"quote"</div>').should.contain('"quote"');
+ $("<div>'quote'</div>").should.contain("'quote'");
+ });
+ });
+
+ describe("have", function(){
+ it("preserves existing behavior on non-jQuery objects", function(){
+ ({foo: 1, bar: 2}).should.have.property('foo');
+ });
+
var subject = $('<div><span></span></div>');
- it("passes when the selection matches the given selector", function(){
- subject.should.contain('span');
+ it("passes when the selection has the given selector", function(){
+ subject.should.have('span');
});
- it("passes negated when the selection does not match the given selector", function(){
- subject.should.not.contain('div');
+ it("passes negated when the selection does not have the given selector", function(){
+ subject.should.not.have('div');
});
- it("fails when the selection does not match the given selector", function(){
+ it("fails when the selection does not have the given selector", function(){
(function(){
- subject.should.contain('div');
- }).should.fail("expected " + inspect(subject) + " to contain 'div'");
+ subject.should.have('div');
+ }).should.fail("expected " + inspect(subject) + " to have 'div'");
});
- it("fails negated when the selection matches the given selector", function(){
+ it("fails negated when the selection has the given selector", function(){
(function(){
- subject.should.not.contain("span");
- }).should.fail("expected " + inspect(subject) + " not to contain 'span'");
+ subject.should.not.have("span");
+ }).should.fail("expected " + inspect(subject) + " not to have 'span'");
});
});
});
View
@@ -3,7 +3,7 @@
<title>Mocha</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
- <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
+ <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script src="../node_modules/mocha/mocha.js"></script>
<script src="../node_modules/chai/chai.js"></script>
<script src="../chai-jquery.js"></script>

0 comments on commit 29cd83c

Please sign in to comment.