Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Feb 13, 2012
0 parents commit b66cd19
Show file tree
Hide file tree
Showing 8 changed files with 907 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.settings.xml
node_modules/
.DS_Store
*.swp
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2012 John Firebaugh

MIT License

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
119 changes: 119 additions & 0 deletions README.md
@@ -0,0 +1,119 @@
# chai-jquery

chai-jquery is an extension to the [chai](http://chaijs.com/) assertion library that
provides a set of jQuery-specific assertions.

## Usage

Include `chai-jquery.js` in your test file, after `chai.js` (version 0.3.3 or later):

<script src="chai-jquery.js"></script>

Use the assertions with chai's `expect` or `should` assertions.

## Assertions

### `attr(name[, value])`
Assert that the first element of the selection has the given attribute, using [`.attr()`](http://api.jquery.com/attr/).
Optionally, assert a particular value as well. The return value is available for chaining.

$('#header').should.have.attr('foo');
expect($('body')).to.have.attr('foo', 'bar');
expect($('body')).to.have.attr('foo').match(/bar/);

### `data(name[, value])`
Assert that the first element of the selection has the given data value, using [`.data()`](http://api.jquery.com/data/).
Optionally, assert a particular value as well. The return value is available for chaining.

$('#header').should.have.data('foo');
expect($('body')).to.have.data('foo', 'bar');
expect($('body')).to.have.data('foo').match(/bar/);

### `class(className)`
Assert that the first element of the selection has the given class, using [`.hasClass()`](http://api.jquery.com/hasClass/).

$('#header').should.have.class('foo');
expect($('body')).to.have.class('foo');

### `id(id)`
Assert that the first element of the selection has the given id, using `.attr('id')`.

$('.header').should.have.id('#main');
expect($('body')).to.have.id('foo');

### `html(html)`
Assert that the first element of the selection has the given html, using [`.html()`](http://api.jquery.com/html/).

$('.name').should.have.html('<em>John Doe</em>');
expect($('#title')).to.have.html('Chai Tea');

### `text(text)`
Assert that the first element of the selection has the given text, using [`.text()`](http://api.jquery.com/text/).

$('.name').should.have.text('John Doe');
expect($('#title')).to.have.text('Chai Tea');

### `value(value)`
Assert that the first element of the selection has the given value, using [`.val()`](http://api.jquery.com/val/).

$('.name').should.have.value('John Doe');
expect($('.year')).to.have.value('2012');

### `visible`
Assert that the first element of the selection is visible, using [`.is(':visible')`](http://api.jquery.com/:visible/).

$('.name').should.be.visible;
expect($('.year')).to.be.visible;

### `hidden`
Assert that the first element of the selection is hidden, using [`.is(':hidden')`](http://api.jquery.com/:hidden/).

$('.name').should.be.hidden;
expect($('.year')).to.be.hidden;

### `selected`
Assert that the first element of the selection is selected, using [`.is(':selected')`](http://api.jquery.com/:selected/).

$('option').should.be.selected;
expect($('option')).not.to.be.selected;

### `checked`
Assert that the first element of the selection is checked, using [`.is(':checked')`](http://api.jquery.com/:checked/).

$('.checked').should.be.checked;
expect($('input')).not.to.be.checked;

### `disabled`
Assert that the first element of the selection is disabled, using [`.is(':disabled')`](http://api.jquery.com/:disabled/).

$('.disabled').should.be.disabled;
expect($('input')).not.to.be.disabled;

### `exist`
Assert that the selection is not empty. 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.

$('#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.

$('input').should.match('#foo');
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.

$('body').should.contain('h1');
expect($('#content')).to.contain('section');

## License

Copyright (c) 2012 John Firebaugh

MIT License (see the LICENSE file)
172 changes: 172 additions & 0 deletions chai-jquery.js
@@ -0,0 +1,172 @@
chai.use(function (chai) {
var inspect = chai.inspect;

jQuery.fn.inspect = function (depth) {
var el = jQuery('<div />').append(this.clone());
if (depth) {
var children = el.children();
while (depth-- > 0)
children = children.children();
children.html('...');
}
return el.html();
};

chai.Assertion.prototype.attr = function (name, val) {
var actual = this.obj.attr(name);

if (!this.negate || undefined === val) {
this.assert(
undefined !== actual
, 'expected ' + this.inspect + ' to have a ' + inspect(name) + ' attribute'
, 'expected ' + this.inspect + ' not to have a ' + inspect(name) + ' attribute');
}

if (undefined !== val) {
this.assert(
val === actual
, 'expected ' + this.inspect + ' to have a ' + inspect(name) + ' attribute with the value ' +
inspect(val) + ', but the value was ' + inspect(actual)
, 'expected ' + this.inspect + ' not to have a ' + inspect(name) + ' attribute with the value ' +
inspect(val));
}

this.obj = actual;
return this;
};

chai.Assertion.prototype.data = function (name, val) {
// Work around a chai bug (https://github.com/logicalparadox/chai/issues/16)
if (this.negate && undefined !== val && undefined === this.obj.data(name)) {
return this;
}

var assertion = new chai.Assertion(this.obj.data());
if (this.negate)
assertion = assertion.not;
return assertion.property(name, val);
};

chai.Assertion.prototype.class = function (className) {
this.assert(
this.obj.hasClass(className)
, 'expected ' + this.inspect + ' to have class ' + inspect(className)
, 'expected ' + this.inspect + ' not to have class ' + inspect(className));
return this;
};

chai.Assertion.prototype.id = function (id) {
this.assert(
this.obj.attr('id') === id
, 'expected ' + this.inspect + ' to have id ' + inspect(id)
, 'expected ' + this.inspect + ' not to have id ' + inspect(id));
return this;
};

chai.Assertion.prototype.html = function (html) {
this.assert(
this.obj.html() === html
, 'expected ' + this.inspect + ' to have HTML ' + inspect(html)
, 'expected ' + this.inspect + ' not to have HTML ' + inspect(html));
return this;
};

chai.Assertion.prototype.text = function (text) {
this.assert(
this.obj.text() === text
, 'expected ' + this.inspect + ' to have text ' + inspect(text)
, 'expected ' + this.inspect + ' not to have text ' + inspect(text));
return this;
};

chai.Assertion.prototype.value = function (value) {
this.assert(
this.obj.val() === value
, 'expected ' + this.inspect + ' to have value ' + inspect(value)
, 'expected ' + this.inspect + ' not to have value ' + inspect(value));
return this;
};

// data

jQuery.each(['visible', 'hidden', 'selected', 'checked', 'disabled'], function (i, attr) {
Object.defineProperty(chai.Assertion.prototype, attr, {
get: function () {
this.assert(
this.obj.is(':' + attr)
, 'expected ' + this.inspect + ' to be ' + attr
, 'expected ' + this.inspect + ' not to be ' + attr);
return this;
}
});
});

function override(name, getter) {
var _super = Object.getOwnPropertyDescriptor(chai.Assertion.prototype, name);
Object.defineProperty(chai.Assertion.prototype, name, {
get: getter(_super.get)
});
}

override('exist', function (_super) {
return function () {
if (this.obj instanceof jQuery) {
this.assert(
this.obj.length > 0
, 'expected ' + inspect(this.obj.selector) + ' to exist'
, 'expected ' + inspect(this.obj.selector) + ' not to exist');
return this;
} else {
return _super.call(this);
}
};
});

override('be', function (_super) {
return function () {
var be = function (selector) {
if (this.obj instanceof jQuery) {
this.assert(
this.obj.is(selector)
, 'expected ' + this.inspect + ' to be ' + inspect(selector)
, 'expected ' + this.inspect + ' not to be ' + inspect(selector));
return this;
} else {
return _super.call(this);
}
};
be.__proto__ = this;
return be;
}
});

var match = chai.Assertion.prototype.match;
chai.Assertion.prototype.match = function (selector) {
if (this.obj instanceof jQuery) {
this.assert(
this.obj.is(selector)
, 'expected ' + this.inspect + ' to match ' + inspect(selector)
, 'expected ' + this.inspect + ' not to match ' + inspect(selector));
return this;
} else {
return match.call(this, selector);
}
};

override('contain', function (_super) {
return function () {
_super.call(this);
var contain = function (selector) {
if (this.obj instanceof jQuery) {
this.assert(
this.obj.find(selector).length > 0
, 'expected ' + this.inspect + ' to contain ' + inspect(selector)
, 'expected ' + this.inspect + ' not to contain ' + inspect(selector));
return this;
}
};
contain.__proto__ = this;
return contain;
}
});
});
23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"author": "John Firebaugh <john.firebaugh@gmail.com>",
"name": "chai-jquery",
"description": "jQuery assertions for the chai assertion library",
"keywords": [ "test", "assertion", "assert", "testing", "jQuery" ],
"version": "0.0.1",
"repository": {
"type": "git",
"url": "https://github.com/jfirebaugh/chai-jquery"
},
"bugs": {
"url": "https://github.com/jfirebaugh/chai-jquery/issues"
},
"main": "./index",
"engines": {
"node": ">= 0.4.0"
},
"dependencies": {},
"devDependencies": {
"chai": "0.3.3",
"mocha": "*"
}
}

0 comments on commit b66cd19

Please sign in to comment.