var expect = require('expect.js'), fixtures = require('../fixtures'), cheerio = require('../..'); describe('cheerio', function() { describe('.html', function() { it('() : should return innerHTML; $.html(obj) should return outerHTML', function() { var $div = cheerio('div', '
foobar
'); var span = $div.children()[1]; expect(cheerio(span).html()).to.equal('bar'); expect(cheerio.html(span)).to.equal('bar'); }); it('() : should accept an object, an array, or a cheerio object', function() { var $span = cheerio('foo'); expect(cheerio.html($span[0])).to.equal('foo'); expect(cheerio.html($span)).to.equal('foo'); }); it('() : should be able to set to an empty string', function() { var $elem = cheerio('foo').html(''); expect(cheerio.html($elem)).to.equal(''); }); it('() : of empty cheerio object should return null', function() { expect(cheerio().html()).to.be(null); }); it('(selector) : should return the outerHTML of the selected element', function() { var $ = cheerio.load(fixtures.fruits); expect($.html('.pear')).to.equal('
  • Pear
  • '); }); }); describe('.load', function() { it('(html) : should retain original root after creating a new node', function() { var $html = cheerio.load(''); expect($html('body')).to.have.length(1); $html('', { xmlMode : true }); // console.log($html('script')[0].type); // expect($html('script')[0].type).to.be('tag'); // }); it('(buffer) : should accept a buffer', function() { var $html = cheerio.load(new Buffer('
    foo
    ')); expect($html.html()).to.be('
    foo
    '); }); }); describe('.clone', function() { it('() : should return a copy', function() { var $src = cheerio('
    foobarbaz
    ').children(); var $elem = $src.clone(); expect($elem.length).to.equal(3); expect($elem.parent()).to.have.length(0); expect($elem.text()).to.equal($src.text()); $src.text('rofl'); expect($elem.text()).to.not.equal($src.text()); }); it('() : should preserve parsing options', function() { var $ = cheerio.load('
    π
    ', { decodeEntities: false }); var $div = $('div'); expect($div.text()).to.equal($div.clone().text()); }); }); describe('.parseHTML', function() { it('() : returns null', function() { expect(cheerio.parseHTML()).to.equal(null); }); it('(null) : returns null', function() { expect(cheerio.parseHTML(null)).to.equal(null); }); it('("") : returns null', function() { expect(cheerio.parseHTML('')).to.equal(null); }); it('(largeHtmlString) : parses large HTML strings', function() { var html = new Array(10).join('
    '); var nodes = cheerio.parseHTML(html); expect(nodes.length).to.be.greaterThan(4); expect(nodes).to.be.an('array'); }); it('("'; expect(cheerio.parseHTML(html)).to.have.length(0); }); it('("'; expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i); }); it('("scriptAndNonScript) : preserves non-script nodes', function() { var html = '
    '; expect(cheerio.parseHTML(html)[0].tagName).to.match(/div/i); }); it('(scriptAndNonScript, true) : Preserves script position', function() { var html = '
    '; expect(cheerio.parseHTML(html, true)[0].tagName).to.match(/script/i); }); it('(text) : returns a text node', function() { expect(cheerio.parseHTML('text')[0].type).to.be('text'); }); it('(\\ttext) : preserves leading whitespace', function() { expect(cheerio.parseHTML('\t
    ')[0].data).to.equal('\t'); }); it('( text) : Leading spaces are treated as text nodes', function() { expect(cheerio.parseHTML('
    ')[0].type).to.be('text'); }); it('(html) : should preserve content', function() { var html = '
    test div
    '; expect(cheerio(cheerio.parseHTML(html)[0]).html()).to.equal('test div'); }); it('(malformedHtml) : should not break', function() { expect(cheerio.parseHTML('')).to.have.length(1); }); it('(garbageInput) : should not cause an error', function() { expect(cheerio.parseHTML('<#if>

    This is a test.

    <#/if>') || true).to.be.ok(); }); it('(text) : should return an array that is not effected by DOM manipulation methods', function() { var $ = cheerio.load('
    '); var elems = $.parseHTML(''); $('div').append(elems); expect(elems).to.have.length(2); }); }); describe('.contains', function() { var $; beforeEach(function() { $ = cheerio.load(fixtures.food); }); it('(container, contained) : should correctly detect the provided element', function() { var $food = $('#food'); var $fruits = $('#fruits'); var $apple = $('.apple'); expect($.contains($food[0], $fruits[0])).to.equal(true); expect($.contains($food[0], $apple[0])).to.equal(true); }); it('(container, other) : should not detect elements that are not contained', function() { var $fruits = $('#fruits'); var $vegetables = $('#vegetables'); var $apple = $('.apple'); expect($.contains($vegetables[0], $apple[0])).to.equal(false); expect($.contains($fruits[0], $vegetables[0])).to.equal(false); expect($.contains($vegetables[0], $fruits[0])).to.equal(false); expect($.contains($fruits[0], $fruits[0])).to.equal(false); expect($.contains($vegetables[0], $vegetables[0])).to.equal(false); }); }); describe('.root', function() { it('() : should return a cheerio-wrapped root object', function() { var $html = cheerio.load('
    foobar
    '); $html.root().append('
    '); expect($html.html()).to.equal('
    foobar
    '); }); }); });