Skip to content

Commit

Permalink
test: Add tests for mixed elements and text (#1747)
Browse files Browse the repository at this point in the history
- updated tests to use functions with mixed text and html tags
- updated `addClass` function test so it will try elements without an already existing class name
  • Loading branch information
5saviahv committed Feb 24, 2021
1 parent fafae51 commit ca7cd9b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
4 changes: 4 additions & 0 deletions lib/api/manipulation.js
Expand Up @@ -520,6 +520,7 @@ exports.after = function () {
var index = siblings.indexOf(el);

// If not found, move on
/* istanbul ignore next */
if (index < 0) return;

var domSrc =
Expand Down Expand Up @@ -578,6 +579,7 @@ exports.insertAfter = function (target) {
var index = siblings.indexOf(el);

// If not found, move on
/* istanbul ignore next */
if (index < 0) return;

// Add cloned `this` element(s) after target element
Expand Down Expand Up @@ -623,6 +625,7 @@ exports.before = function () {
var index = siblings.indexOf(el);

// If not found, move on
/* istanbul ignore next */
if (index < 0) return;

var domSrc =
Expand Down Expand Up @@ -681,6 +684,7 @@ exports.insertBefore = function (target) {
var index = siblings.indexOf(el);

// If not found, move on
/* istanbul ignore next */
if (index < 0) return;

// Add cloned `this` element(s) after target element
Expand Down
60 changes: 50 additions & 10 deletions test/api/attributes.js
Expand Up @@ -613,6 +613,14 @@ describe('$(...)', function () {
expect($('.apple').hasClass('fruit')).toBe(true);
expect($('.orange').hasClass('fruit')).toBe(true);
expect($('.pear').hasClass('fruit')).toBe(true);

// mixed with text nodes
var $red = $('<html>\n<ul id=one>\n</ul>\t</html>').addClass('red');
expect($red).toHaveLength(3);
expect($red[0].type).toBe('text');
expect($red[1].type).toBe('tag');
expect($red[2].type).toBe('text');
expect($red.hasClass('red')).toBe(true);
});

it('(class class class) : should add multiple classes to the element', function () {
Expand All @@ -624,10 +632,10 @@ describe('$(...)', function () {
});

it('(fn) : should add classes returned from the function', function () {
var $fruits = $('#fruits').children();
var $fruits = $('#fruits').children().add($('#fruits'));
var args = [];
var thisVals = [];
var toAdd = ['apple red', '', undefined];
var toAdd = ['main', 'apple red', '', undefined];

$fruits.addClass(function (idx) {
args.push(Array.from(arguments));
Expand All @@ -636,15 +644,23 @@ describe('$(...)', function () {
});

expect(args).toStrictEqual([
[0, 'apple'],
[1, 'orange'],
[2, 'pear'],
[0, ''],
[1, 'apple'],
[2, 'orange'],
[3, 'pear'],
]);
expect(thisVals).toStrictEqual([$fruits[0], $fruits[1], $fruits[2]]);
expect($fruits.eq(0).hasClass('apple')).toBe(true);
expect($fruits.eq(0).hasClass('red')).toBe(true);
expect($fruits.eq(1).hasClass('orange')).toBe(true);
expect($fruits.eq(2).hasClass('pear')).toBe(true);
expect(thisVals).toStrictEqual([
$fruits[0],
$fruits[1],
$fruits[2],
$fruits[3],
]);
expect($fruits.eq(0).hasClass('main')).toBe(true);
expect($fruits.eq(0).hasClass('apple')).toBe(false);
expect($fruits.eq(1).hasClass('apple')).toBe(true);
expect($fruits.eq(1).hasClass('red')).toBe(true);
expect($fruits.eq(2).hasClass('orange')).toBe(true);
expect($fruits.eq(3).hasClass('pear')).toBe(true);
});
});

Expand Down Expand Up @@ -680,6 +696,21 @@ describe('$(...)', function () {
$('.pear').removeClass('fruit');
expect($('.pear').hasClass('fruit')).toBe(false);
expect($('.pear').hasClass('pear')).toBe(true);

// remove one class from set
var $li = $('li').removeClass('orange');
expect($li.eq(0).attr('class')).toBe('apple');
expect($li.eq(1).attr('class')).toBe('');
expect($li.eq(2).attr('class')).toBe('pear');

// mixed with text nodes
var $red = $('<html>\n<ul class=one>\n</ul>\t</html>').removeClass('one');
expect($red).toHaveLength(3);
expect($red[0].type).toBe('text');
expect($red[1].type).toBe('tag');
expect($red[2].type).toBe('text');
expect($red.eq(1).attr('class')).toBe('');
expect($red.eq(1).prop('tagName')).toBe('UL');
});

it('(single class) : should remove a single class from multiple classes on the element', function () {
Expand Down Expand Up @@ -757,6 +788,15 @@ describe('$(...)', function () {
expect($('.fruit').hasClass('apple')).toBe(false);
expect($('.fruit').hasClass('red')).toBe(true);
expect($('.fruit').hasClass('fruit')).toBe(true);

// mixed with text nodes
var $red = $('<html>\n<ul class=one>\n</ul>\t</html>').toggleClass('red');
expect($red).toHaveLength(3);
expect($red.hasClass('red')).toBe(true);
expect($red.hasClass('one')).toBe(true);
$red.toggleClass('one');
expect($red.hasClass('red')).toBe(true);
expect($red.hasClass('one')).toBe(false);
});

it('(class class, true) : should add multiple classes to the element', function () {
Expand Down

0 comments on commit ca7cd9b

Please sign in to comment.