diff options
author | Timmy Willison <4timmywil@gmail.com> | 2018-01-02 16:45:10 -0500 |
---|---|---|
committer | Timmy Willison <4timmywil@gmail.com> | 2018-01-08 11:43:53 -0500 |
commit | 80f57f8a13debaab87b99f73631669699da3e1a5 (patch) | |
tree | e89842f0becc28188d0d95142fe5d5ac2c30eb90 /test/unit/attributes.js | |
parent | a88b48eab1cdbb9dac05679a0d1c0edd7cc4afd7 (diff) | |
download | jquery-80f57f8a13debaab87b99f73631669699da3e1a5.tar.gz jquery-80f57f8a13debaab87b99f73631669699da3e1a5.zip |
Attributes: allow array param in add/remove/toggleClass
+30 bytes instead of +182
Thanks to @faisaliyk for the first pass on this feature.
Fixes gh-3532
Close gh-3917
Diffstat (limited to 'test/unit/attributes.js')
-rw-r--r-- | test/unit/attributes.js | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 3ab023eaf..b446fe1c3 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -12,6 +12,10 @@ function functionReturningObj( value ) { }; } +function arrayFromString( value ) { + return value ? value.split( " " ) : []; +} + /* ======== local reference ======= bareObj and functionReturningObj can be used to test passing functions to setters @@ -1261,6 +1265,10 @@ QUnit.test( "addClass(Function)", function( assert ) { testAddClass( functionReturningObj, assert ); } ); +QUnit.test( "addClass(Array)", function( assert ) { + testAddClass( arrayFromString, assert ); +} ); + QUnit.test( "addClass(Function) with incoming value", function( assert ) { assert.expect( 52 ); var pass, i, @@ -1334,6 +1342,10 @@ QUnit.test( "removeClass(Function) - simple", function( assert ) { testRemoveClass( functionReturningObj, assert ); } ); +QUnit.test( "removeClass(Array) - simple", function( assert ) { + testRemoveClass( arrayFromString, assert ); +} ); + QUnit.test( "removeClass(Function) with incoming value", function( assert ) { assert.expect( 52 ); @@ -1432,6 +1444,10 @@ QUnit.test( "toggleClass(Function[, boolean])", function( assert ) { testToggleClass( functionReturningObj, assert ); } ); +QUnit.test( "toggleClass(Array[, boolean])", function( assert ) { + testToggleClass( arrayFromString, assert ); +} ); + QUnit.test( "toggleClass(Function[, boolean]) with incoming value", function( assert ) { assert.expect( 14 ); @@ -1567,6 +1583,40 @@ QUnit.test( "addClass, removeClass, hasClass on many elements", function( assert "Did not find a class when not present" ); } ); +QUnit.test( "addClass, removeClass, hasClass on many elements - Array", function( assert ) { + assert.expect( 16 ); + + var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" ); + + elem.addClass( [ "hi" ] ); + assert.equal( elem[ 0 ].className, "hi", "Check single added class" ); + assert.equal( elem[ 1 ].className, "hi", "Check single added class" ); + assert.equal( elem[ 2 ].className, "hi", "Check single added class" ); + + elem.addClass( [ "foo", "bar" ] ); + assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" ); + assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" ); + assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" ); + + elem.removeClass(); + assert.equal( elem[ 0 ].className, "", "Remove all classes" ); + assert.equal( elem[ 1 ].className, "", "Remove all classes" ); + assert.equal( elem[ 2 ].className, "", "Remove all classes" ); + + elem.addClass( [ "hi", "foo", "bar", "baz" ] ); + elem.removeClass( [ "foo" ] ); + assert.equal( elem[ 0 ].className, "hi bar baz", "Check removal of one class" ); + assert.equal( elem[ 1 ].className, "hi bar baz", "Check removal of one class" ); + assert.equal( elem[ 2 ].className, "hi bar baz", "Check removal of one class" ); + + elem.removeClass( [ "bar baz" ] ); + assert.equal( elem[ 0 ].className, "hi", "Check removal of two classes" ); + assert.equal( elem[ 1 ].className, "hi", "Check removal of two classes" ); + assert.equal( elem[ 2 ].className, "hi", "Check removal of two classes" ); + + assert.ok( elem.hasClass( "hi" ), "Check has1" ); +} ); + QUnit.test( "addClass, removeClass, hasClass on elements with classes with non-HTML whitespace (gh-3072, gh-3003)", function( assert ) { assert.expect( 9 ); |