Skip to content

Instantly share code, notes, and snippets.

@patocallaghan
Last active December 20, 2015 15:29
Show Gist options
  • Select an option

  • Save patocallaghan/6154561 to your computer and use it in GitHub Desktop.

Select an option

Save patocallaghan/6154561 to your computer and use it in GitHub Desktop.
Chai jQuery plugin examples. Uses `expect` style. Examples from http://chaijs.com/plugins/chai-jquery. #sinon #chai #javascript #jquery

#Chai jQuery

##.attr(name[, value])

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

##.css(name[, value])

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

##.data(name[, value])

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

##.class(class)

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

##.id(id)

expect($('body')).to.have.id('foo');

##.html(html)

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

##.text(text)

expect($('#title')).to.have.text('Chai Tea');

##.value(value)

expect($('.year')).to.have.value('2012');

##.visible is(':visible')

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

##.hidden is(':hidden')

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

##.selected is(':selected')

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

##.checked is(':checked')

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

##.empty is(':empty')

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

##.exist

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

##.match(selector) / .be(selector)

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

##.contain

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

##.have

expect($('body')).to.not.have('h1');
expect($('#content')).to.have('div');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment