Skip to content

Commit 4959c81

Browse files
committed
Build: Add qunit-assert-classes plug-in for classes tests
1 parent 8ea36f5 commit 4959c81

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

Gruntfile.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ grunt.initConfig({
229229
"qunit/qunit.css": "qunit/qunit/qunit.css",
230230
"qunit/LICENSE.txt": "qunit/LICENSE.txt",
231231

232+
"qunit-assert-classes/qunit-assert-classes.js": "qunit-assert-classes/qunit-assert-classes.js",
233+
"qunit-assert-classes/LICENSE.txt": "qunit-assert-classes/LICENSE",
234+
232235
"jquery-mousewheel/jquery.mousewheel.js": "jquery-mousewheel/jquery.mousewheel.js",
233236
"jquery-mousewheel/LICENSE.txt": "jquery-mousewheel/LICENSE.txt",
234237

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"jquery-simulate": "1.0.0",
1616
"jshint": "2.4.4",
1717
"qunit": "1.17.1",
18+
"qunit-assert-classes": "0.1.5",
1819

1920
"jquery-1.7.0": "jquery#1.7.0",
2021
"jquery-1.7.1": "jquery#1.7.1",
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Alexander Schmitz
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
( function( QUnit ) {
2+
function inArray( haystack, needle ) {
3+
for ( var i = 0; i < haystack.length; i++ ) {
4+
if ( haystack[ i ] === needle ) {
5+
return true;
6+
}
7+
}
8+
return false;
9+
}
10+
function check( element, classes, stateVal, message ) {
11+
var i, result, classAttribute, elementClassArray,
12+
classArray = classes.split( " " ),
13+
missing = [],
14+
found = [];
15+
16+
if ( element.jquery && element.length !== 1 ) {
17+
throw( "Class checks can only be performed on a single element on a collection" );
18+
}
19+
element = element.jquery ? element[ 0 ] : element;
20+
classAttribute = element.getAttribute( "class" );
21+
message = message || "Element must " + ( stateVal? "" : "not " ) + "have classes";
22+
if ( classAttribute ) {
23+
elementClassArray = classAttribute.split( " " );
24+
for( i = 0; i < classArray.length; i++ ) {
25+
if ( !inArray( elementClassArray, classArray[ i ] ) ) {
26+
missing.push( classArray[ i ] );
27+
} else {
28+
found.push( classArray[ i ] );
29+
}
30+
}
31+
} else {
32+
missing = classArray;
33+
}
34+
35+
result = stateVal ? !missing.length : !found.length;
36+
QUnit.push( result, classes, result ? classes : found.join( " " ), message );
37+
}
38+
39+
QUnit.extend( QUnit.assert, {
40+
hasClasses: function( element, classes, message ) {
41+
check( element, classes, true, message );
42+
},
43+
lacksClasses: function( element, classes, message ) {
44+
check( element, classes, false, message );
45+
}
46+
});
47+
})( QUnit );

0 commit comments

Comments
 (0)