chai-jquery

chai-jquery 是 chai 断言库的扩展,提供了一组 jQuery 特定的断言。

用法

在您的测试文件中包含 chai-jquery.js,位于 jquery.jschai.js (版本 1.0.0-rc1 或更高版本) 之后。

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

请注意,jquery.jschai.js 可以相互插入之前(顺序在此处无关紧要)。

使用断言与 chai 的 expectshould 断言。

断言

attr(name[, value])

断言选择器的第一个元素具有给定的属性,使用 .attr()。 可选地,还可以断言特定的值。返回值可用于链式操作。

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

prop(name[, value])

断言选择器的第一个元素具有给定的属性,使用 .prop()。 可选地,还可以断言特定的值。返回值可用于链式操作。

$('#header').should.have.prop('disabled');
expect($('body')).to.have.prop('disabled', false);
expect($('body')).to.have.prop('value').match(/bar/);

css(name[, value])

断言选择器的第一个元素具有给定的 CSS 属性,使用 .css()。 可选地,还可以断言计算后的值。返回值可用于链式操作。

$('#header').should.have.css('background');
expect($('body')).to.have.css('background-color', 'rgb(0, 0, 0)');
expect($('body')).to.have.css('font-family').match(/sans-serif/);

data(name[, value])

断言选择器的第一个元素具有给定的数据值,使用 .data()。 可选地,还可以断言特定的值。返回值可用于链式操作。

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

class(className)

断言选择器的第一个元素具有给定的类,使用 .hasClass()

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

id(id)

断言选择器的第一个元素具有给定的 ID,使用 .attr('id')

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

html(html)

断言选择器的第一个元素的 html 等于给定的 html,使用 .html()

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

text(text)

断言选择器的第一个元素的文本等于给定的文本,使用 .text()

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

value(value)

断言选择器的第一个元素具有给定的值,使用 .val()

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

visible

断言选择器的至少一个元素可见,使用 .is(':visible')

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

hidden

断言选择器的至少一个元素隐藏,使用 .is(':hidden')

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

selected

断言选择器的至少一个元素被选中,使用 .is(':selected')

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

checked

断言选择器的至少一个元素被选中,使用 .is(':checked')

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

enabled

断言选择器的至少一个元素启用,使用 .is(':enabled')

$('.enabled').should.be.enabled;
expect($('enabled')).to.be.enabled;

disabled

断言选择器的至少一个元素禁用,使用 .is(':disabled')

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

empty

断言选择器的至少一个元素为空,使用 .is(':empty')。如果断言的对象不是 jQuery 对象,则会调用原始实现。

$('.empty').should.be.empty;
expect($('body')).not.to.be.empty;

exist

断言选择器不为空。请注意,这会覆盖内置的 chai 断言。如果断言的对象不是 jQuery 对象,则会调用原始实现。

$('#exists').should.exist;
expect($('#nonexistent')).not.to.exist;

match(selector)

断言选择器匹配给定的选择器,使用 .is()。请注意,这会覆盖内置的 chai 断言。如果断言的对象不是 jQuery 对象,则会调用原始实现。

$('input').should.match('#foo');
expect($('#empty')).to.match(':empty');

contain(text)

断言选择器包含给定的文本,使用 :contains()。如果断言的对象不是 jQuery 对象,或者如果 contain 未作为函数调用,则会调用原始实现。

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

descendants(selector)

断言选择器包含至少一个元素,该元素具有与给定选择器匹配的后代,使用 .has()

$('body').should.have.descendants('h1');
expect($('#content')).to.have.descendants('div');

focus()

断言选择器的至少一个元素可见。请注意,此断言不使用 .is(':focus')。它使用的是 $('.element').get(0) === document.activeElement,因为 某些 webkit 浏览器中 .is(‘:focus’) 不兼容

$('#focused').should.have.focus();
expect($('#nonfocused')).not.have.focus();

贡献

要运行测试套件,请运行 npm install (需要在您的系统上安装 Node.js),并在您的 Web 浏览器中打开 test/index.html

许可证

版权所有 (c) 2012 John Firebaugh

MIT 许可证 (请参阅 LICENSE 文件)