diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 040b3c2162..be4d1d2077 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -413,11 +413,7 @@ exports.wrapAll = function (wrapper) { wrapper = wrapper.call(this[0]); } - var wrap = this._make(wrapper); - - if (this[0].parent) { - wrap = wrap.insertBefore(this[0]); - } + var wrap = this._make(wrapper).insertBefore(this[0]); // if html is given as wrapper, wrap may contain text elements var elInsertLocation = { children: wrap }; diff --git a/test/__fixtures__/fixtures.js b/test/__fixtures__/fixtures.js index c07185574a..cdfa0cccb7 100644 --- a/test/__fixtures__/fixtures.js +++ b/test/__fixtures__/fixtures.js @@ -23,7 +23,7 @@ exports.divcontainers = [ '
Fourth
', '', '
', - '

', + '
\n\n

\n\n
', '
', ].join(''); diff --git a/test/api/manipulation.js b/test/api/manipulation.js index 6cd832deae..710c54cf75 100644 --- a/test/api/manipulation.js +++ b/test/api/manipulation.js @@ -369,6 +369,12 @@ describe('$(...)', function () { expect($container[0].children[0]).toBe($wrap[0]); }); + it('(html) : should wrap elements with it', function () { + var parent = doc('

').wrapAll('

').parent(); + expect(parent).toHaveLength(1); + expect(parent.is('div')).toBe(true); + }); + it('(selector) : should find element from dom, wrap elements with it', function () { $inner.wrapAll('#new'); var $container = doc('.container'); @@ -387,6 +393,36 @@ describe('$(...)', function () { expect($new[0].parent).toBe($container[0]); expect($container[0].children[0]).toBe($new[0]); }); + + it('(function) : check execution', function () { + var $container = doc('.container'); + var p = $container[0].parent; + + var result = $container.wrapAll(function () { + return "
"; + }); + + expect(result.parent()).toHaveLength(1); + expect($container.eq(0).parent().parent().is('.red')).toBe(true); + expect($container.eq(1).parent().parent().is('.red')).toBe(true); + expect($container.eq(0).parent().parent().parent().is(p)).toBe(true); + }); + + it('(function) : check execution characteristics', function () { + var $new = doc('#new'); + var i = 0; + + doc('no-result').wrapAll(function () { + i++; + return ''; + }); + expect(i).toBeFalsy(); + + $new.wrapAll(function (index) { + expect(this).toBe($new[0]); + expect(index).toBe(undefined); + }); + }); }); describe('.append', function () { @@ -797,7 +833,7 @@ describe('$(...)', function () { describe('.after', function () { it('() : should do nothing', function () { - expect($('#fruits').after()[0].tagName).toBe('ul'); + expect($fruits.after()[0].tagName).toBe('ul'); }); it('(html) : should add element as next sibling', function () { diff --git a/test/api/traversing.js b/test/api/traversing.js index 92da250c4f..d8e8e40c7b 100644 --- a/test/api/traversing.js +++ b/test/api/traversing.js @@ -599,6 +599,13 @@ describe('$(...)', function () { expect(result).toHaveLength(1); expect(result[0].attribs.id).toBe('fruits'); }); + + it('(cheerio object) : should return all parents until body element', function () { + var body = $('body')[0]; + var result = $('.carrot').parentsUntil(body); + expect(result).toHaveLength(2); + expect(result.eq(0).is('ul#vegetables')).toBe(true); + }); }); describe('.parent', function () { diff --git a/test/cheerio.js b/test/cheerio.js index 83c0194678..fe73fe466f 100644 --- a/test/cheerio.js +++ b/test/cheerio.js @@ -226,6 +226,10 @@ describe('cheerio', function () { expect($empty.each).toBe(cheerio.prototype.each); }); + it('cheerio.html(null) should return a "" string', function () { + expect(cheerio.html(null)).toBe(''); + }); + it('should set html(number) as a string', function () { var $elem = cheerio('
'); $elem.html(123); diff --git a/test/parse.js b/test/parse.js index 0d475472d9..b0634cda27 100644 --- a/test/parse.js +++ b/test/parse.js @@ -210,6 +210,15 @@ describe('parse', function () { expect(root.childNodes[0].type).toBe('directive'); }); + it('should simply return root ', function () { + var oldroot = parse(basic, defaultOpts, true); + var root = parse(oldroot, defaultOpts, true); + expect(root).toBe(oldroot); + rootTest(root); + expect(root.childNodes).toHaveLength(1); + expect(root.childNodes[0].tagName).toBe('html'); + }); + it('should expose the DOM level 1 API', function () { var root = parse( '

',