-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.assert.js
executable file
·226 lines (180 loc) · 6.33 KB
/
jquery.assert.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* jQuery assert
* Version 1.2.0
* https://github.com/timbuethe/jquery-assert
*/
;(function( $, undefined ){
/**
* List of jquery function names that get monkey patched.
*
* TODO what about 'first', 'last', 'offsetParent' ?
*
* @type {Array}
*/
var jqueryFunctions = ['add', 'addBack', 'andSelf', 'children', 'closest', 'end', 'eq', 'filter', 'find', 'has', 'map',
'next', 'nextAll', 'nextUntil', 'not', 'parent', 'parents', 'parentsUntil', 'prev', 'prevAll', 'prevUntil', 'siblings', 'slice'];
/**
* When patched, the original jquery functions get saved here, to restore them later on.
*/
var originalFunctions = {};
/**
* Save previous settings to detect changes.
*/
var settings, previousSettings = {};
var rootjQuery = jQuery(document);
/**
* @param [options]
*/
$.assert = function (options){
settings = $.extend({
'enabled': true,
'extend-jquery': false,
'debug': false
}, options);
/**
* @private
*/
function _log(){
if(settings.debug){
console.log.apply(console, arguments);
}
}
_log('init assert ... (options: ', options, ')')
/**
* @param {Number} [expectedSize]
*/
$.fn.assertFound = function(expectedSize) {
var message;
if(settings.enabled === false){
return this;
}
// called without arguments ('.assertFound()') asserts an non empty result
if(expectedSize === undefined && this.length === 0){
message = "Assertion failed, expected to find elements using selector '" + this.selector + "', but found none."
_log(message)
throw message;
}
// if an expected size is provided, test against it
if (expectedSize !== undefined && this.length !== expectedSize) {
message = "Assertion failed, expected size: " + expectedSize + ", actual size: " + this.length + ", selector: '" + this.selector + "'.";
_log(message)
throw message;
}
return this;
};
/**
* Convenience function for '.assertFound(1)'
*/
$.fn.assertOne = function() { return this.assertFound(1); };
/**
* Convenience function for '.assertFound()'
*/
$.fn.assertNotEmpty = function() { return this.assertFound(); };
//
// do or revert monkey patching of jquery's functions
if(settings['extend-jquery'] !== previousSettings['extend-jquery']){
_log("option 'extend-jquery' has changed")
// patch 'em
if(settings['extend-jquery'] === true){
_log("exenting jQuery functions ...")
// patch core jQuery function, call extend to copy all the properties
originalFunctions['jQuery'] = jQuery
assignJQueryVariables(jQuery.extend(extendedJQuery, jQuery))
// everything else
$.each(jqueryFunctions, function(index, value) {
_log(index + ": " + value)
originalFunctions[value] = $.fn[value]
$.fn[value] = addAssert($.fn[value], value)
});
_log("exenting jQuery functions done.")
}
// restore original jquery functions
else {
_log("restore original jQuery functions ...")
// core jQuery function
if(originalFunctions['jQuery']){
assignJQueryVariables(originalFunctions['jQuery'])
}
// everything else
$.each(originalFunctions, function(name, func) {
_log('restore ' + name)
$.fn[name] = func;
});
_log("restore original jQuery functions done.")
}
}
/**
*
* @param newJQuery
*/
function assignJQueryVariables(newJQuery){
window.jQuery = newJQuery
if(window.$ === window.jQuery){
window.$ = window.jQuery
}
}
/**
* Extension for jQuery's core function. Additional argument 'expectedSize'
* can be provided to assert the number of matched elements.
*
* e.g.
* $('.foo', 5)
*
* @param selector
* @param [context]
* @param {Number} [expectedSize]
* @return {jQuery.fn.init}
*/
function extendedJQuery(selector, context, expectedSize) {
var result;
_log('extended jQuery called')
// if the expected size is provided as the last argument, call find
// and do the assertion afterwards.
if(typeof arguments[arguments.length-1] === 'number'){
// expected size maybe on 2nd or 3rd position
expectedSize = arguments[arguments.length-1];
// if only two arguments were provided, the second is expectedSize, not context
if(arguments.length === 2){
context = undefined
}
result = new jQuery.fn.init(selector, context, rootjQuery);
result.assertFound(expectedSize);
return result;
}
return new jQuery.fn.init(selector, context, rootjQuery);
}
/**
* Extended functions accepts the same parameter as the original,
* as well as an additional parameter to assert a specific size using assertFound.
*
* @param {Function} superFunction
* @param {String} [name]
* @return {Function}
*/
function addAssert(superFunction, name){
return function(){
var expectedSize, result, args = [];
_log('extended', name, 'called')
// if the expected size is provided as the last argument, call find
// and do the assertion afterwards.
if(typeof arguments[arguments.length-1] === 'number'){
// convert arguments to an ordinary array
args = Array.prototype.slice.call(arguments);
expectedSize = args.pop();
result = superFunction.apply(this, args);
result.assertFound(expectedSize);
return result;
}
// else, just delegate to super
return superFunction.apply(this, arguments);
}
}
previousSettings = settings;
_log('init assert done.')
// return jQuery, useful to asign it to a variable in noConflict mode
return window.jQuery
}; // assert(options)
// call init here so the user doesn't have to. If the user calls it anyway, e.g. to change/set
// options, that should be ok too.
$.assert();
})( jQuery );