chai-jquery
chai-jquery 是 chai 斷言函式庫的擴充功能,提供了一組特定於 jQuery 的斷言。
用法
在您的測試檔案中,在 jquery.js
和 chai.js
(1.0.0-rc1 或更新版本) 之後,包含 chai-jquery.js
<script src="jquery.js"></script>
<script src="chai.js"></script>
<script src="chai-jquery.js"></script>
請注意,jquery.js
和 chai.js
可以互相在前後插入 (順序在這裡不重要)。
使用 chai 的 expect
或 should
斷言來使用這些斷言。
斷言
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),並在您的網頁瀏覽器中開啟 test/index.html
。
授權
版權所有 (c) 2012 John Firebaugh
MIT 授權 (請參閱 LICENSE 檔案)