chai-jquery
chai-jquery is an extension to the chai assertion library that provides a set of jQuery-specific assertions.
Usage
Include chai-jquery.js
in your test file, after jquery.js
and chai.js
(version 1.0.0-rc1 or later):
Note that jquery.js
and chai.js
can be inserted one before another (order does not matter here).
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()
.
Optionally, assert a particular value as well. The return value is available for chaining.
shouldhave;tohave;tohave;
prop(name[, value])
Assert that the first element of the selection has the given property, using .prop()
.
Optionally, assert a particular value as well. The return value is available for chaining.
shouldhave;tohave;tohave;
css(name[, value])
Assert that the first element of the selection has the given CSS property, using .css()
.
Optionally, assert the computed value as well. The return value is available for chaining.
shouldhave;tohave;tohave;
data(name[, value])
Assert that the first element of the selection has the given data value, using .data()
.
Optionally, assert a particular value as well. The return value is available for chaining.
shouldhavedata'foo';tohavedata'foo' 'bar';tohavedata'foo';
class(className)
Assert that the first element of the selection has the given class, using .hasClass()
.
shouldhave;tohave;
id(id)
Assert that the first element of the selection has the given id, using .attr('id')
.
shouldhaveid'#main';tohaveid'foo';
html(html)
Assert that the html of the first element of the selection is equal to the given html, using .html()
.
shouldhave;tohave;
text(text)
Assert that the text of the first element of the selection is equal to the given text, using .text()
.
shouldhavetext'John Doe';tohavetext'Chai Tea';
value(value)
Assert that the first element of the selection has the given value, using .val()
.
shouldhavevalue'John Doe';tohavevalue'2012';
visible
Assert that at least one element of the selection is visible, using .is(':visible')
.
shouldbevisible;tobevisible;
hidden
Assert that at least one element of the selection is hidden, using .is(':hidden')
.
shouldbehidden;tobehidden;
selected
Assert that at least one element of the selection is selected, using .is(':selected')
.
shouldbeselected;nottobeselected;
checked
Assert that at least one element of the selection is checked, using .is(':checked')
.
shouldbechecked;nottobechecked;
enabled
Assert that at least one element of the selection is enabled, using .is(':enabled')
.
shouldbeenabled;tobeenabled;
disabled
Assert that at least one element of the selection is disabled, using .is(':disabled')
.
shouldbedisabled;nottobedisabled;
empty
Assert that at least one element of the selection is empty, using .is(':empty')
.
If the object asserted against is not a jQuery object, the original implementation will be called.
shouldbeempty;nottobeempty;
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.
shouldexist;nottoexist;
match(selector)
Assert that the selection matches a given selector, using .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.
should;to;
contain(text)
Assert that the selection contains the given text, using :contains()
.
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.
should;to;
descendants(selector)
Assert that the selection contains at least one element which has a descendant matching the given selector,
using .has()
.
shouldhave;tohave;
focus()
Assert that at least one element of the selection is visible. Note that this assertion does not use .is(':focus')
.
It rather uses $('.element').get(0) === document.activeElement
, because of incompatibility of .is(':focus') in certain webkit browsers.
shouldhave;nothave;
Contributing
To run the test suite, run npm install
(requires
Node.js to be installed on your system), and open
test/index.html
in your web browser.
License
Copyright (c) 2012 John Firebaugh
MIT License (see the LICENSE file)