Chai-jQ

chai-jq is an alternate plugin for the Chai assertion library to provide jQuery-specific assertions.

Usage

Add chai-jq.js after your Chai script include.

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

You can install chai-jq via the following package managers:

  • NPM: npm install chai-jq
  • Bower: bower install chai-jq

To see some of the plugin's assertions in action, see the test page for the project.

Plugin API

.$visible

Asserts that the element is visible.

expect($("<div>&nbsp;</div>"))
  .to.be.$visible;

See: http://api.jquery.com/visible-selector/

.$hidden

Asserts that the element is hidden.

expect($("<div style=\"display: none\" />"))
  .to.be.$hidden;

See: http://api.jquery.com/hidden-selector/

.$val(string|regexp)

Asserts that the element value matches a string or regular expression.

expect($("<input value='foo' />"))
  .to.have.$val("foo").and
  .to.have.$val(/^foo/);

See: http://api.jquery.com/val/

.$class(string)

Asserts that the element has a class match.

expect($("<div class='foo bar' />"))
  .to.have.$class("foo").and
  .to.have.$class("bar");

See: http://api.jquery.com/hasClass/

.$attr(name, string)

Asserts that the target has exactly the given named attribute, or asserts the target contains a subset of the attribute when using the include or contain modifiers.

expect($("<div id=\"hi\" foo=\"bar time\" />"))
  .to.have.$attr("id", "hi").and
  .to.contain.$attr("foo", "bar");

See: http://api.jquery.com/attr/

.$prop(name, value)

Asserts that the target has exactly the given named property.

expect($("<input type=\"checkbox\" checked=\"checked\" />"))
  .to.have.$prop("checked", true).and
  .to.have.$prop("type", "checkbox");

See: http://api.jquery.com/prop/

.$html(string)

Asserts that the target has exactly the given HTML, or asserts the target contains a subset of the HTML when using the include or contain modifiers.

expect($("<div><span>foo</span></div>"))
  .to.have.$html("<span>foo</span>").and
  .to.contain.$html("foo");

See: http://api.jquery.com/html/

.$text(string)

Asserts that the target has exactly the given text, or asserts the target contains a subset of the text when using the include or contain modifiers.

expect($("<div><span>foo</span> bar</div>"))
  .to.have.$text("foo bar").and
  .to.contain.$text("foo");

See: http://api.jquery.com/text/

.$css(name, string)

Asserts that the target has exactly the given CSS property.

expect($("<div style=\"width: 50px; border: 1px dotted black;\" />"))
  .to.have.$css("width", "50px").and
  .to.have.$css("border-top-style", "dotted");

See: http://api.jquery.com/css/

Contributions

Please see the Contributions Guide for how to help out with the plugin.

We test all changes with Travis CI. Here's our current build status:

Build Status

Licenses

All code not otherwise specified is Copyright 2013 Ryan Roemer. Released under the MIT License.

This repository contains various libraries from other folks, and are licensed as follows:

  • jQuery is Copyright jQuery Foundation and licensed under the MIT license.

  • Mocha is Copyright TJ Holowaychuk and licensed under the MIT license.

  • Chai is Copyright Jake Luer and licensed under the BSD license.

  • Sinon.JS is Copyright Christian Johansen and licensed under the BSD license.

  • Mocha-PhantomJS is Copyright Ken Collins and licensed under the MIT license.

  • Pure is Copyright Yahoo! and licensed under the MIT license.

History

0.0.2

  • Extension of API with: $css, $visible, $hidden. [atimb]

0.0.1

  • Initial release with API: $val, $class, $attr, $prop, $html, $text. [ryan-roemer]

Contributing

Community contributions are most welcome. Here are a couple of guidelines for contributions.

  • Use GitHub pull requests.
  • Reference existing open GitHub issues in commits where relevant.
  • Styles, Tests: Always run grunt check to run all style and test checks. This is a test library -- it should pass its own tests. ;)
  • Generation: Please run grunt to correctly generate all appropriate files before finishing up and submitting your pull request.

And, thanks!