Skip to content

Commit

Permalink
added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
5saviahv committed Jan 4, 2021
1 parent bfa114e commit 539aba4
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 8 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 lib/api/traversing.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ exports.not = function (match, container) {
});
}

return container._make(elements);
return this._make(elements);
};

/**
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
16 changes: 16 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 Expand Up @@ -822,6 +829,15 @@ describe('$(...)', function () {
expect(lis).toHaveLength(1);
});

it('(selector, container) : should reduce the set of matched elements to those that are mot contained in the provided selection', function () {
var $fruits = $('li');
var $notOrange = $('ul').not('.orange', $fruits.get());

expect($notOrange).toHaveLength(2);
expect($notOrange[0]).toBe($fruits[0]);
expect($notOrange[1]).toBe($fruits[2]);
});

it('(selection) : should reduce the set of matched elements to those that are mot contained in the provided selection', function () {
var $fruits = $('li');
var $orange = $('.orange');
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 539aba4

Please sign in to comment.