Skip to content

Commit 4543815

Browse files
committed
Tests: Partially use new qunit interface
http://qunitjs.com/upgrade-guide-2.x/ For most of the boring work was used https://github.com/apsdehal/qunit-migrate package However, it can't update local qunit helpers, plus in some places old QUnit.asyncTest signature is still used Ref b930d14 Fixes gh-2540
1 parent c530661 commit 4543815

24 files changed

+6654
-6130
lines changed

test/data/testinit.js

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ original$ = this.$ = "replaced";
1717

1818
/**
1919
* Returns an array of elements with the given IDs
20-
* @example q("main", "foo", "bar")
20+
* @example q( "main", "foo", "bar" )
2121
* @result [<div id="main">, <span id="foo">, <input id="bar">]
2222
*/
2323
this.q = function() {
@@ -114,12 +114,12 @@ this.createXMLFragment = function() {
114114
fireNative = document.createEvent ?
115115
function( node, type ) {
116116
var event = document.createEvent( "HTMLEvents" );
117+
117118
event.initEvent( type, true, true );
118119
node.dispatchEvent( event );
119120
} :
120121
function( node, type ) {
121-
var event = document.createEventObject();
122-
node.fireEvent( "on" + type, event );
122+
node.fireEvent( "on" + type, document.createEventObject() );
123123
};
124124

125125
/**
@@ -138,16 +138,20 @@ function url( value ) {
138138

139139
// Ajax testing helper
140140
this.ajaxTest = function( title, expect, options ) {
141-
var requestOptions;
142-
if ( jQuery.isFunction( options ) ) {
143-
options = options();
144-
}
145-
options = options || [];
146-
requestOptions = options.requests || options.request || options;
147-
if ( !jQuery.isArray( requestOptions ) ) {
148-
requestOptions = [ requestOptions ];
149-
}
150-
asyncTest( title, expect, function() {
141+
QUnit.test( title, expect, function( assert ) {
142+
var requestOptions;
143+
144+
if ( jQuery.isFunction( options ) ) {
145+
options = options( assert );
146+
}
147+
options = options || [];
148+
requestOptions = options.requests || options.request || options;
149+
if ( !jQuery.isArray( requestOptions ) ) {
150+
requestOptions = [ requestOptions ];
151+
}
152+
153+
var done = assert.async();
154+
151155
if ( options.setup ) {
152156
options.setup();
153157
}
@@ -161,7 +165,9 @@ this.ajaxTest = function( title, expect, options ) {
161165
if ( options.teardown ) {
162166
options.teardown();
163167
}
164-
start();
168+
169+
// Make sure all events will be called before done()
170+
setTimeout( done );
165171
}
166172
},
167173
requests = jQuery.map( requestOptions, function( options ) {
@@ -171,7 +177,7 @@ this.ajaxTest = function( title, expect, options ) {
171177
return function( _, status ) {
172178
if ( !completed ) {
173179
if ( !handler ) {
174-
ok( false, "unexpected " + status );
180+
assert.ok( false, "unexpected " + status );
175181
} else if ( jQuery.isFunction( handler ) ) {
176182
handler.apply( this, arguments );
177183
}
@@ -180,7 +186,7 @@ this.ajaxTest = function( title, expect, options ) {
180186
};
181187

182188
if ( options.afterSend ) {
183-
options.afterSend( request );
189+
options.afterSend( request, assert );
184190
}
185191

186192
return request
@@ -203,7 +209,8 @@ this.ajaxTest = function( title, expect, options ) {
203209
};
204210

205211
this.testIframe = function( fileName, name, fn ) {
206-
asyncTest( name, function() {
212+
QUnit.test( name, function( assert ) {
213+
var done = assert.async();
207214

208215
// load fixture in iframe
209216
var iframe = loadFixture(),
@@ -212,10 +219,9 @@ this.testIframe = function( fileName, name, fn ) {
212219
if ( win && win.jQuery && win.jQuery.isReady ) {
213220
clearInterval( interval );
214221

215-
start();
216-
217222
// call actual tests passing the correct jQuery instance to use
218-
fn.call( this, win.jQuery, win, win.document );
223+
fn.call( this, win.jQuery, win, win.document, assert );
224+
done();
219225
document.body.removeChild( iframe );
220226
iframe = null;
221227
}
@@ -234,31 +240,31 @@ this.testIframe = function( fileName, name, fn ) {
234240
};
235241

236242
this.testIframeWithCallback = function( title, fileName, func ) {
237-
238-
test( title, function() {
243+
QUnit.test( title, 1, function( assert ) {
239244
var iframe;
245+
var done = assert.async();
240246

241-
// Expect one assertion, but allow overrides
242-
expect( 1 );
243-
244-
stop();
245247
window.iframeCallback = function() {
246-
var self = this,
247-
args = arguments;
248+
var args = Array.prototype.slice.call( arguments );
249+
250+
args.push( assert );
251+
248252
setTimeout( function() {
249-
window.iframeCallback = undefined;
253+
this.iframeCallback = undefined;
254+
250255
iframe.remove();
251-
func.apply( self, args );
256+
func.apply( this, args );
252257
func = function() {};
253-
start();
254-
}, 0 );
258+
259+
done();
260+
} );
255261
};
256262
iframe = jQuery( "<div/>" ).css( { position: "absolute", width: "500px", left: "-600px" } )
257263
.append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) )
258264
.appendTo( "#qunit-fixture" );
259265
} );
260266
};
261-
window.iframeCallback = undefined;
267+
this.iframeCallback = undefined;
262268

263269
// Tests are always loaded async
264270
QUnit.config.autostart = false;

0 commit comments

Comments
 (0)