From ca7cd9bd69e6692a546d49bd435dc59be06bffd5 Mon Sep 17 00:00:00 2001 From: 5saviahv <49443574+5saviahv@users.noreply.github.com> Date: Wed, 24 Feb 2021 09:39:45 +0200 Subject: [PATCH] test: Add tests for mixed elements and text (#1747) - 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 --- lib/api/manipulation.js | 4 +++ test/api/attributes.js | 60 ++++++++++++++++++++++++++++++++++------- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/lib/api/manipulation.js b/lib/api/manipulation.js index 3e92a733ec..fe877146d3 100644 --- a/lib/api/manipulation.js +++ b/lib/api/manipulation.js @@ -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 = @@ -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 @@ -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 = @@ -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 diff --git a/test/api/attributes.js b/test/api/attributes.js index 65c7623894..dd0af6afe2 100644 --- a/test/api/attributes.js +++ b/test/api/attributes.js @@ -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 = $('\n\t').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 () { @@ -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)); @@ -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); }); }); @@ -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 = $('\n\t').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 () { @@ -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 = $('\n\t').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 () {