This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
jquery.testHelper.js
262 lines (216 loc) · 6.15 KB
/
jquery.testHelper.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
* mobile support unit tests
*/
(function( $ ) {
$.testHelper = {
// This function takes sets of files to load asynchronously. Each set will be loaded after
// the previous set has completed loading. That is, each require and it's dependencies in a
// set will be loaded asynchronously, but each set will be run in serial.
asyncLoad: function( seq ) {
require({
baseUrl: "../../../js"
});
function loadSeq( seq, i ){
if( !seq[i] ){
$( document ).ready( function() {
var $fixture = $( '#qunit-fixture' );
if ( $fixture.length ) {
QUnit.config.fixture = $fixture.html();
}
QUnit.start();
});
return;
}
require( seq[i], function() {
loadSeq(seq, i + 1);
});
}
// stop qunit from running the tests until everything is in the page
QUnit.config.autostart = false;
loadSeq( seq, 0 );
},
excludeFileProtocol: function(callback){
var message = "Tests require script reload and cannot be run via file: protocol";
if (location.protocol == "file:") {
test(message, function(){
ok(false, message);
});
} else {
callback();
}
},
// TODO prevent test suite loads when the browser doesn't support push state
// and push-state false is defined.
setPushState: function() {
if( $.support.pushState && location.search.indexOf( "push-state" ) >= 0 ) {
$.support.pushState = false;
}
},
pushStateRedirect: function( filename ) {
var search, pairs = [];
search = location.search.replace( "?", "");
if( search ){
pairs = search.split( "&" );
}
pairs.push( "push-state=false" );
location.href = location.href.toString()
.replace(/\/[^\/]*\?|\/[^\/]*$/, "/" + filename )
.replace( search, "") + "?" + pairs.join( "&" );
},
reloads: {},
reloadModule: function(libName){
var deferred = $.Deferred(),
context;
// where a module loader isn't defined use the old way
if( !window.require ) {
this.reloadLib( libName );
deferred.resolve();
return deferred;
}
if(this.reloads[libName] === undefined) {
this.reloads[libName] = {
count: 0
};
}
//Clear internal cache of module inside of require
context = require.s.contexts._;
delete context.defined[libName];
delete context.specified[libName];
delete context.loaded[libName];
delete context.urlFetched[require.toUrl(libName + '.js')];
require(
{
baseUrl: "../../../js"
}, [libName],
function() {
deferred.resolve();
}
);
return deferred;
},
reloadLib: function(libName){
if(this.reloads[libName] === undefined) {
this.reloads[libName] = {
lib: $("script[src$='" + libName + "']"),
count: 0
};
}
var lib = this.reloads[libName].lib.clone(),
src = lib.attr('src');
//NOTE append "cache breaker" to force reload
lib.attr('src', src + "?" + this.reloads[libName].count++);
$("body").append(lib);
},
rerunQunit: function(){
var self = this;
QUnit.init();
$("script:not([src*='.\/'])").each(function(i, elem){
var src = elem.src.split("/");
self.reloadLib(src[src.length - 1]);
});
QUnit.start();
},
alterExtend: function(extraExtension){
var extendFn = $.extend;
$.extend = function(object, extension){
// NOTE extend the object as normal
var result = extendFn.apply(this, arguments);
// NOTE add custom extensions
result = extendFn(result, extraExtension);
return result;
};
},
hideActivePageWhenComplete: function() {
if( $('#qunit-testresult').length > 0 ) {
$('.ui-page-active').css('display', 'none');
} else {
setTimeout($.testHelper.hideActivePageWhenComplete, 500);
}
},
openPage: function(hash){
location.href = location.href.split('#')[0] + hash;
},
sequence: function(fns, interval){
$.each(fns, function(i, fn){
setTimeout(fn, i * interval);
});
},
pageSequence: function( fns ){
this.eventSequence( "pagechange", fns );
},
eventSequence: function( event, fns, timedOut ){
var seq = [];
$.each(fns, function( i, fn ) {
seq.push( fn );
if( i !== fns.length - 1) seq.push( event );
});
this.eventCascade( seq );
},
eventCascade: function( sequence, timedOut ) {
var fn = sequence.shift(),
event = sequence.shift(),
self = this;
if( fn === undefined ) {
return;
}
if( event ){
// if a pagechange or defined event is never triggered
// continue in the sequence to alert possible failures
var warnTimer = setTimeout(function() {
self.eventCascade( sequence, true );
}, 2000);
// bind the recursive call to the event
$.mobile.pageContainer.one(event, function() {
clearTimeout( warnTimer );
// Let the current stack unwind before we fire off the next item in the sequence.
// TODO setTimeout(self.pageSequence, 0, sequence);
setTimeout(function(){ self.eventCascade(sequence); }, 0);
});
}
// invoke the function which should, in some fashion,
// trigger the next event
fn( timedOut );
},
deferredSequence: function(fns) {
var fn = fns.shift(),
deferred = $.Deferred(),
self = this, res;
if (fn) {
res = fn();
if ( res && $.type( res.done ) === "function" ) {
res.done(function() {
self.deferredSequence( fns ).done(function() {
deferred.resolve();
});
});
} else {
self.deferredSequence( fns ).done(function() {
deferred.resolve();
});
}
} else {
deferred.resolve();
}
return deferred;
},
decorate: function(opts){
var thisVal = opts.self || window;
return function(){
var returnVal;
opts.before && opts.before.apply(thisVal, arguments);
returnVal = opts.fn.apply(thisVal, arguments);
opts.after && opts.after.apply(thisVal, arguments);
return returnVal;
};
},
assertUrlLocation: function( args ) {
var parts = $.mobile.path.parseUrl( location.href ),
pathnameOnward = location.href.replace( parts.domain, "" );
if( $.support.pushState ) {
same( pathnameOnward, args.hashOrPush || args.push, args.report );
} else {
same( parts.hash, "#" + (args.hashOrPush || args.hash), args.report );
}
}
};
})(jQuery);