Skip to content

Commit

Permalink
refactor: Simplify wrapAll, add some tests (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv authored Jan 4, 2021
1 parent e9f7728 commit b6d3840
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
6 changes: 1 addition & 5 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion test/__fixtures__/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ exports.divcontainers = [
'<div class="inner">Fourth</div>',
'</div>',
'<div id="new"><div>',
'<div><p><em><b></b></em></p></div>',
'<div>\n\n<p><em><b></b></em></p>\n\n</div>',
'</div>',
].join('');

Expand Down
38 changes: 37 additions & 1 deletion test/api/manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<p>').wrapAll('<div></div>').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');
Expand All @@ -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 "<div class='red'><div class='tmp'></div></div>";
});

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 () {
Expand Down Expand Up @@ -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 () {
Expand Down
7 changes: 7 additions & 0 deletions test/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
4 changes: 4 additions & 0 deletions test/cheerio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div>');
$elem.html(123);
Expand Down
9 changes: 9 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<div><a></a><span></span><p></p></div>',
Expand Down

0 comments on commit b6d3840

Please sign in to comment.