Skip to content

Commit

Permalink
Remove be, rename have
Browse files Browse the repository at this point in the history
  • Loading branch information
charlierudolph authored and jfirebaugh committed Oct 11, 2014
1 parent 6d7a26a commit f0bb642
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 108 deletions.
24 changes: 8 additions & 16 deletions README.md
Expand Up @@ -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/).
Expand All @@ -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

Expand Down
50 changes: 9 additions & 41 deletions chai-jquery.js
Expand Up @@ -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(
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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);
}
}
});
}));
56 changes: 5 additions & 51 deletions test/chai-jquery-spec.js
Expand Up @@ -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 = $('<div></div>');

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');
Expand Down Expand Up @@ -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 = $('<div><span></span></div>');

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'");
});
});
Expand Down

0 comments on commit f0bb642

Please sign in to comment.