-
Notifications
You must be signed in to change notification settings - Fork 3
/
qunit-assert-classes.js
169 lines (133 loc) · 5.17 KB
/
qunit-assert-classes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
( function( factory ) {
if ( typeof define === "function" && define.amd ) {
// AMD. Register as an anonymous module.
define( [
"qunit"
], factory );
} else {
// Browser globals
factory( QUnit );
}
}( function( QUnit ) {
function inArray( haystack, needle ) {
for ( var i = 0; i < haystack.length; i++ ) {
if (
( needle instanceof RegExp && needle.test( haystack[ i ] ) )||
( typeof needle === "string" && haystack[ i ] === needle )
) {
return true;
}
}
return false;
}
function check( element, search ) {
var i, classAttribute, elementClassArray,
missing = [],
found = [];
if ( element.jquery && element.length !== 1 ) {
throw new Error( "Class checks can only be performed on a single element on a collection" );
}
element = element.jquery ? element[ 0 ] : element;
classAttribute = element.getAttribute( "class" );
if ( classAttribute ) {
elementClassArray = splitClasses( classAttribute );
if ( search instanceof RegExp ) {
if ( inArray( elementClassArray, search ) ) {
found.push( search );
} else {
missing.push( search );
}
} else {
for( i = 0; i < search.length; i++ ) {
if ( !inArray( elementClassArray, search[ i ] ) ) {
missing.push( search[ i ] );
} else {
found.push( search[ i ] );
}
}
}
} else {
missing = search;
}
return {
missing: missing,
found: found,
element: element,
classAttribute: classAttribute
};
}
function splitClasses( classes ) {
return classes.match( /\S+/g ) || [];
}
function pluralize( message, classes ) {
return message + ( classes.length > 1 ? "es" : "" );
}
QUnit.extend( QUnit.assert, {
hasClasses: function( element, classes, message ) {
var classArray = splitClasses( classes ),
results = check( element, classArray );
message = message || pluralize( "Element must have class", classArray );
this.push( !results.missing.length, results.found.join( " " ), classes, message );
},
lacksClasses: function( element, classes, message ) {
var classArray = splitClasses( classes ),
results = check( element, classArray );
message = message || pluralize( "Element must not have class", classArray );
this.push( !results.found.length, results.found.join( " " ), classes, message );
},
hasClassesStrict: function( element, classes, message ) {
var result,
classArray = splitClasses( classes ),
results = check( element, classArray );
message = message || pluralize( "Element must only have class", classArray );
result = !results.missing.length && results.element.getAttribute( "class" ) &&
splitClasses( results.element.getAttribute( "class" ) ).length ===
results.found.length;
this.push( result, results.found.join( " " ), classes, message );
},
hasClassRegex: function( element, regex, message ) {
var results = check( element, regex );
message = message || "Element must have class matching " + regex;
this.push( !!results.found.length, results.found.join( " " ), regex, message );
},
lacksClassRegex: function( element, regex, message ) {
var results = check( element, regex );
message = message || "Element must not have class matching " + regex;
this.push( results.missing.length, results.missing.join( " " ), regex, message );
},
hasClassStart: function( element, partialClass, message ) {
var results = check( element, new RegExp( "^" + partialClass ) );
message = message || "Element must have class starting with " + partialClass;
this.push( results.found.length, results.found.join( " " ), partialClass, message );
},
lacksClassStart: function( element, partialClass, message ) {
var results = check( element, new RegExp( "^" + partialClass ) );
message = message || "Element must not have class starting with " + partialClass;
this.push( results.missing.length, results.missing.join( " " ), partialClass, message );
},
hasClassPartial: function( element, partialClass, message ) {
var results = check( element, new RegExp( partialClass ) );
message = message || "Element must have class containing '" + partialClass + "'";
this.push( results.found.length, results.found.join( " " ), partialClass, message );
},
lacksClassPartial: function( element, partialClass, message ) {
var results = check( element, new RegExp( partialClass ) );
message = message || "Element must not have class containing '" + partialClass + "'";
this.push( results.missing.length, results.missing.join( " " ), partialClass, message );
},
lacksAllClasses: function( element, message ) {
element = element.jquery ? element[ 0 ] : element;
var classAttribute = element.getAttribute( "class" ) || "",
classes = splitClasses( classAttribute );
message = message || "Element must not have any classes";
this.push( !classes.length, !classes.length, true, message );
},
hasSomeClass: function( element, message ) {
element = element.jquery ? element[ 0 ] : element;
var classAttribute = element.getAttribute( "class" ) || "",
classes = splitClasses( classAttribute );
message = message || "Element must have a class";
this.push( classes.length, classes.length, true, message );
}
});
} ) );