FormidableLabs/chai-jq

Name: chai-jq

Owner: Formidable

Description: An alternate jQuery assertion library for Chai.

Created: 2013-10-25 20:15:10.0

Updated: 2017-03-13 12:14:48.0

Pushed: 2018-01-09 18:36:57.0

Homepage: http://stack.formidable.com/chai-jq/

Size: 444

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Chai-jQ

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

Usage

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

The integration notes discuss how to properly patch Chai with the plugin in different environments (browser, AMD, Node.js). The API documentation is a good starting point to learn how to use chai-jq assertions in your tests. The site's own test suites also provide a decent introduction to the API:

Assertions
Integration

chai-jq works in your browser, with AMD/RequireJS, and in Node.js with JsDom.

Standard Browser: To use in a standard HTML page, include chai-jq.js after Chai.

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

AMD Browser: To use in a RequireJS/AMD page, require in chai-jq and inject it into Chai before your test imports / runners begin:

ire(["chai", "chai-jq"], function (chai, plugin) {
 Inject plugin.
ai.use(plugin);

 Rest of your test code here...

Node.js / JsDom: To use in Node.js/JsDom, require in chai-jq and inject it into Chai before your test imports / runners begin:

chai    = require("chai");
plugin  = require("chai-jq");

nject plugin.
.use(plugin);

est of test code here...
Object Context Changes

One slight difference from how assertions in chai-jq work from Chai and other plugins is the switching of object context for certain assertions, currently:

In general usage, the object under test (e.g., the thing wrapped in an expect()) remains the current context, so you can do something like:

$elem = $("<div id=\"hi\" foo=\"bar time\" />");

ct($elem)
 Assertion object is `$elem`
o.have.$attr("id", "hi").and
 Assertion object is still `$elem`
o.contain.$attr("foo", "bar");

In the above example, the jQuery object $elem remains the object under assertion for both $attr calls. However, in the special case for one of the enumerated assertions above where:

Then, the object under assertion switches to the value of the effective method called. So, taking our example again, and calling $attr() without an expected value, we would have:

$elem = $("<div id=\"hi\" foo=\"bar time\" />");

ct($elem)
 Assertion object is `$elem`
o.have.$attr("foo").and
// Assertion object now changed to `$attr()` value: `"bar time"`
.to.equal("bar time").and
.to.match(/^b/).and
.to.not.have.length(2);

In the above example here, the object under assertion becomes the string "bar time" immediately after the call to $attr("foo") with no expected value.

Plugin API
$visible

Asserts that the element is visible.

Node.js/JsDom Note: JsDom does not currently infer zero-sized or hidden parent elements as hidden / visible appropriately.

ct($("<div>&nbsp;</div>"))
o.be.$visible;

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

$hidden

Asserts that the element is hidden.

Node.js/JsDom Note: JsDom does not currently infer zero-sized or hidden parent elements as hidden / visible appropriately.

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

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

$val(expected, [message])

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

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

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

$class(expected, [message])

Asserts that the element has a class match.

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

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

$attr(name, [expected], [message])

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.

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

Changes context to attribute string value when no expected value is provided:

ct($("<div id=\"hi\" foo=\"bar time\" />"))
o.have.$attr("foo").and
.to.equal("bar time").and
.to.match(/^b/);

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

$data(name, [expected], [message])

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

ct($("<div data-id=\"hi\" data-foo=\"bar time\" />"))
o.have.$data("id", "hi").and
o.contain.$data("foo", "bar");

Changes context to data-attribute string value when no expected value is provided:

ct($("<div data-id=\"hi\" data-foo=\"bar time\" />"))
o.have.$data("foo").and
.to.equal("bar time").and
.to.match(/^b/);

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

$prop(name, [expected], [message])

Asserts that the target has exactly the given named property.

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

Changes context to property string value when no expected value is provided:

ct($("<input type=\"checkbox\" checked=\"checked\" />"))
o.have.$prop("type").and
.to.equal("checkbox").and
.to.match(/^c.*x$/);

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

$html(expected, [message])

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.

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

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

$text(expected, [message])

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.

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

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

$css(expected, [message])

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

Node.js/JsDom Note: Computed CSS properties are not correctly inferred as of JsDom v0.8.8. Explicit ones should get matched exactly.

Browser Note: Explicit CSS properties are sometimes not matched (in contrast to Node.js), so the plugin performs an extra check against explicit style properties for a match. May still have other wonky corner cases.

PhantomJS Note: PhantomJS also is fairly wonky and unpredictable with respect to CSS / styles, especially those that come from CSS classes and not explicity style attributes.

ct($("<div style=\"width: 50px; border: 1px dotted black;\" />"))
o.have.$css("width", "50px").and
o.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, report internal test coverage with Coveralls and check complexity / static analysis with Code Climate. Here is the status for our build, coverage, and complexity:

Build Status Coverage Status Code Climate

We also do multi-browser testing of the frontend code using Sauce Labs. Here's our build matrix:

Sauce Test 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:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.