From f0bb642258af93f1e5323106735d7d212467c449 Mon Sep 17 00:00:00 2001 From: charlierudolph Date: Tue, 3 Jun 2014 09:01:02 -0700 Subject: [PATCH] Remove be, rename have --- README.md | 24 ++++++----------- chai-jquery.js | 50 +++++++---------------------------- test/chai-jquery-spec.js | 56 ++++------------------------------------ 3 files changed, 22 insertions(+), 108 deletions(-) diff --git a/README.md b/README.md index 4073579..de24ba5 100644 --- a/README.md +++ b/README.md @@ -125,14 +125,12 @@ against is not a jQuery object, the original implementation will be called. $('#exists').should.exist; expect($('#nonexistent')).not.to.exist; -### `match(selector)` / `be(selector)` -Assert that the selection matches a given selector, using [`.is()`](http://api.jquery.com/is/). Note that the -built-in behavior of the `match` function and `be` property is preserved -- if the object asserted against is -not a jQuery object, or if `be` is not called as a function, the original implementation will be called. Otherwise, -`match` and `be` are synonyms -- use whichever one reads better. +### `match(selector)` +Assert that the selection matches a given selector, using [`.is()`](http://api.jquery.com/is/). Note that this overrides +the built-in chai assertion. If the object asserted against is not a jQuery object, the original implementation will be called. $('input').should.match('#foo'); - expect($('#empty')).to.be(':empty'); + expect($('#empty')).to.match(':empty'); ### `contain(text)` Assert that the selection contains the given text, using [`:contains()`](http://api.jquery.com/contains-selector/). @@ -142,18 +140,12 @@ implementation will be called. $('body').should.contain('text'); expect($('#content')).to.contain('text'); -### `have(selector)` +### `descendants(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. +using [`.has()`](http://api.jquery.com/has/). - $('body').should.have('h1'); - expect($('#content')).to.have('div'); - -Note that this assertion has the unfortunate side effect of causing assertions such as -`expect(selection).to.have.length(2)` to fail. The technical cause is that the `have` property must be a function, -and functions have a built-in `length` property that cannot be modified. As as workaround, write the assertion -as `expect(selection).to.be.of.length(2)` instead. + $('body').should.have.descendants('h1'); + expect($('#content')).to.have.descendants('div'); ## Contributing diff --git a/chai-jquery.js b/chai-jquery.js index a80eea5..819a27d 100644 --- a/chai-jquery.js +++ b/chai-jquery.js @@ -145,6 +145,15 @@ ); }); + chai.Assertion.addMethod('descendants', function (selector) { + this.assert( + flag(this, 'object').has(selector).length > 0 + , 'expected #{this} to have #{exp}' + , 'expected #{this} not to have #{exp}' + , selector + ); + }); + $.each(['visible', 'hidden', 'selected', 'checked', 'enabled', 'disabled'], function (i, attr) { chai.Assertion.addProperty(attr, function () { this.assert( @@ -182,27 +191,6 @@ }; }); - chai.Assertion.overwriteProperty('be', function (_super) { - return function () { - var obj = flag(this, 'object'); - if (obj instanceof $) { - var be = function (selector) { - this.assert( - obj.is(selector) - , 'expected #{this} to be #{exp}' - , 'expected #{this} not to be #{exp}' - , selector - ); - }; - setPrototypeOf(be, this); - return be; - } - else { - _super.call(this); - } - } - }); - chai.Assertion.overwriteMethod('match', function (_super) { return function (selector) { var obj = flag(this, 'object'); @@ -240,24 +228,4 @@ }; } ); - - chai.Assertion.overwriteProperty('have', function (_super) { - return function () { - var obj = flag(this, 'object'); - if (obj instanceof $) { - var have = function (selector) { - this.assert( - obj.has(selector).length > 0 - , 'expected #{this} to have #{exp}' - , 'expected #{this} not to have #{exp}' - , selector - ); - }; - setPrototypeOf(have, this); - return have; - } else { - _super.call(this); - } - } - }); })); diff --git a/test/chai-jquery-spec.js b/test/chai-jquery-spec.js index 95db081..56b0518 100644 --- a/test/chai-jquery-spec.js +++ b/test/chai-jquery-spec.js @@ -690,44 +690,6 @@ describe("jQuery assertions", function(){ }); }); - describe("be", function(){ - it("preserves existing behavior on non-jQuery objects", function(){ - ("hello").should.be.equal("hello"); - }); - - it("preserves length assertion on non-jQuery objects", function(){ - (['foo','bar']).should.be.of.length(2); - }); - - it("preserves existing behavior when used incorrectly", function(){ - (function(){ - (1 + 1).should.be(3); - }).should.throw(TypeError, "is not a function"); - }); - - var subject = $('
'); - - it("passes when the selection matches the given selector", function(){ - subject.should.be(':empty'); - }); - - it("passes negated when the selection does not match the given selector", function(){ - subject.should.not.be(':parent'); - }); - - it("fails when the selection does not match the given selector", function(){ - (function(){ - subject.should.be(':parent'); - }).should.fail("expected " + inspect(subject) + " to be ':parent'"); - }); - - it("fails negated when the selection matches the given selector", function(){ - (function(){ - subject.should.not.be(":empty"); - }).should.fail("expected " + inspect(subject) + " not to be ':empty'"); - }); - }); - describe("contain", function(){ it("preserves existing behavior on non-jQuery objects", function(){ "example text".should.contain('example'); @@ -771,34 +733,26 @@ describe("jQuery assertions", function(){ }); }); - describe("have", function(){ - it("preserves existing behavior on non-jQuery objects", function(){ - ({foo: 1, bar: 2}).should.have.property('foo'); - }); - - it("preserves length assertion on non-jQuery objects", function(){ - (['foo','bar']).should.have.length(2); - }); - + describe("descendants", function(){ var subject = $('
'); it("passes when the selection has the given selector", function(){ - subject.should.have('span'); + subject.should.have.descendants('span'); }); it("passes negated when the selection does not have the given selector", function(){ - subject.should.not.have('div'); + subject.should.not.have.descendants('div'); }); it("fails when the selection does not have the given selector", function(){ (function(){ - subject.should.have('div'); + subject.should.have.descendants('div'); }).should.fail("expected " + inspect(subject) + " to have 'div'"); }); it("fails negated when the selection has the given selector", function(){ (function(){ - subject.should.not.have("span"); + subject.should.not.have.descendants("span"); }).should.fail("expected " + inspect(subject) + " not to have 'span'"); }); });