Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mix text and html tags in tests #1747

Merged
merged 1 commit into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/api/manipulation.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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