Skip to content

Commit c8d15a2

Browse files
committed
Tests: further improvements QUnit 2.0 migration
* Remove QUnit jshint globals * Extend QUnit.assert methods * Use assert.async instead of start/stop/done Ref b930d14
1 parent 3dd3d13 commit c8d15a2

File tree

16 files changed

+294
-272
lines changed

16 files changed

+294
-272
lines changed

test/.jshintrc

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,6 @@
2727
"Promise": false,
2828
"Symbol": false,
2929
"QUnit": false,
30-
"ok": false,
31-
"equal": false,
32-
"asyncTest": false,
33-
"notEqual": false,
34-
"deepEqual": false,
35-
"strictEqual": false,
36-
"notStrictEqual": false,
37-
"start": false,
38-
"stop": false,
39-
"expect": false,
40-
"throws": false,
4130
"ajaxTest": false,
4231
"testIframe": false,
4332
"testIframeWithCallback": false,
@@ -52,8 +41,6 @@
5241

5342
"jQuery": true,
5443
"sinon": true,
55-
"module": true,
56-
"test": true,
5744
"amdDefined": true,
5845
"fireNative": true,
5946
"Globals": true,

test/data/testinit.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ this.q = function() {
3838
* @example t("Check for something", "//[a]", ["foo", "baar"]);
3939
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
4040
*/
41-
this.t = function( a, b, c ) {
41+
QUnit.assert.t = function( a, b, c ) {
4242
var f = jQuery( b ).get(),
4343
s = "",
4444
i = 0;
@@ -47,7 +47,7 @@ this.t = function( a, b, c ) {
4747
s += ( s && "," ) + '"' + f[ i ].id + '"';
4848
}
4949

50-
deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
50+
this.deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
5151
};
5252

5353
this.createDashboardXML = function() {
@@ -199,7 +199,7 @@ this.ajaxTest = function( title, expect, options ) {
199199
if ( !completed ) {
200200
completed = true;
201201
delete ajaxTest.abort;
202-
ok( false, "aborted " + reason );
202+
assert.ok( false, "aborted " + reason );
203203
jQuery.each( requests, function( i, request ) {
204204
request.abort();
205205
} );

test/data/testrunner.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ QUnit.config.requireExpects = true;
2424
* @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
2525
* @param {string} key
2626
*/
27-
QUnit.expectJqData = function( env, elems, key ) {
27+
QUnit.assert.expectJqData = function( env, elems, key ) {
2828
var i, elem, expando;
2929

3030
// As of jQuery 2.0, there will be no "cache"-data is
@@ -64,7 +64,7 @@ QUnit.expectJqData = function( env, elems, key ) {
6464
// Since this method was called it means some data was
6565
// expected to be found, but since there is nothing, fail early
6666
// (instead of in teardown).
67-
notStrictEqual(
67+
this.notStrictEqual(
6868
expando,
6969
undefined,
7070
"Target for expectJqData must have an expando, " +
@@ -92,7 +92,7 @@ QUnit.config.urlConfig.push( {
9292
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
9393
* teardown function on all modules' lifecycle object.
9494
*/
95-
window.moduleTeardown = function() {
95+
window.moduleTeardown = function( assert ) {
9696
var i, expectedKeys, actualKeys,
9797
cacheLength = 0;
9898

@@ -103,15 +103,15 @@ window.moduleTeardown = function() {
103103
expectedKeys = expectedDataKeys[ i ];
104104
actualKeys = jQuery.cache[ i ] ? Object.keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
105105
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
106-
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
106+
assert.deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
107107
}
108108
delete jQuery.cache[ i ];
109109
delete expectedDataKeys[ i ];
110110
}
111111

112112
// In case it was removed from cache before (or never there in the first place)
113113
for ( i in expectedDataKeys ) {
114-
deepEqual(
114+
assert.deepEqual(
115115
expectedDataKeys[ i ],
116116
undefined,
117117
"No unexpected keys were left in jQuery.cache (#" + i + ")"
@@ -125,12 +125,12 @@ window.moduleTeardown = function() {
125125

126126
// Check for (and clean up, if possible) incomplete animations/requests/etc.
127127
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
128-
equal( jQuery.timers.length, 0, "No timers are still running" );
128+
assert.equal( jQuery.timers.length, 0, "No timers are still running" );
129129
splice.call( jQuery.timers, 0, jQuery.timers.length );
130130
jQuery.fx.stop();
131131
}
132132
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
133-
equal( jQuery.active, oldActive, "No AJAX requests are still active" );
133+
assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
134134
if ( ajaxTest.abort ) {
135135
ajaxTest.abort( "active requests" );
136136
}
@@ -148,7 +148,7 @@ window.moduleTeardown = function() {
148148
// if we unconditionally assert any of these,
149149
// the test will fail with too many assertions :|
150150
if ( cacheLength !== oldCacheLength ) {
151-
equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
151+
assert.equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
152152
oldCacheLength = cacheLength;
153153
}
154154
};

test/unit/ajax.js

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ QUnit.module( "ajax", {
172172
firstTime = false;
173173
jQuery.ajax( this );
174174
} else {
175-
ok ( true, "Test retrying with jQuery.ajax(this) works" );
175+
assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
176176
jQuery.ajax( {
177177
url: url( "data/errorWithText.php" ),
178178
data: {
@@ -1848,20 +1848,28 @@ QUnit.module( "ajax", {
18481848
jQuery( document ).off( "ajaxStart ajaxStop" );
18491849
} );
18501850

1851-
QUnit.asyncTest( "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1, function( assert ) {
1852-
jQuery.ajaxSetup( {
1853-
type: "POST"
1854-
} );
1851+
QUnit.test(
1852+
"jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1,
1853+
function( assert ) {
1854+
var done = assert.async();
18551855

1856-
jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
1857-
assert.equal( method, "GET" );
1858-
QUnit.start();
1859-
} );
1860-
} );
1856+
jQuery.ajaxSetup( {
1857+
type: "POST"
1858+
} );
18611859

1862-
QUnit.asyncTest( "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2, function() {
1863-
jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", start );
1864-
} );
1860+
jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
1861+
assert.equal( method, "GET" );
1862+
done();
1863+
} );
1864+
}
1865+
);
1866+
1867+
QUnit.test(
1868+
"#11402 - jQuery.domManip() - script in comments are properly evaluated", 2,
1869+
function( assert ) {
1870+
jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", assert.async() );
1871+
}
1872+
);
18651873

18661874
//----------- jQuery.get()
18671875

@@ -1926,37 +1934,43 @@ QUnit.module( "ajax", {
19261934

19271935
//----------- jQuery.getScript()
19281936

1929-
QUnit.asyncTest( "jQuery.getScript( String, Function ) - with callback", 2, function( assert ) {
1930-
Globals.register( "testBar" );
1931-
jQuery.getScript( url( "data/testbar.php" ), function() {
1932-
assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
1933-
QUnit.start();
1934-
} );
1935-
} );
1937+
QUnit.test( "jQuery.getScript( String, Function ) - with callback", 2,
1938+
function( assert ) {
1939+
var done = assert.async();
1940+
1941+
Globals.register( "testBar" );
1942+
jQuery.getScript( url( "data/testbar.php" ), function() {
1943+
assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
1944+
done();
1945+
} );
1946+
}
1947+
);
19361948

1937-
QUnit.asyncTest( "jQuery.getScript( String, Function ) - no callback", 1, function() {
1949+
QUnit.test( "jQuery.getScript( String, Function ) - no callback", 1, function( assert ) {
19381950
Globals.register( "testBar" );
1939-
jQuery.getScript( url( "data/testbar.php" ) ).done( start );
1951+
jQuery.getScript( url( "data/testbar.php" ) ).done( assert.async() );
19401952
} );
19411953

1942-
QUnit.asyncTest( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
1954+
QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
1955+
var done = assert.async();
1956+
19431957
Globals.register( "testBar" );
19441958
jQuery.getScript( url( "data/testbar.php" ), function( data, _, jqXHR ) {
19451959
assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
1946-
QUnit.start();
1960+
done();
19471961
} );
19481962
} );
19491963

19501964
// //----------- jQuery.fn.load()
19511965

19521966
// check if load can be called with only url
1953-
QUnit.asyncTest( "jQuery.fn.load( String )", 2, function( assert ) {
1967+
QUnit.test( "jQuery.fn.load( String )", 2, function( assert ) {
19541968
jQuery.ajaxSetup( {
19551969
beforeSend: function() {
19561970
assert.strictEqual( this.type, "GET", "no data means GET request" );
19571971
}
19581972
} );
1959-
jQuery( "#first" ).load( "data/name.html", start );
1973+
jQuery( "#first" ).load( "data/name.html", assert.async() );
19601974
} );
19611975

19621976
QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
@@ -1971,23 +1985,23 @@ QUnit.module( "ajax", {
19711985
} );
19721986

19731987
// check if load can be called with url and null data
1974-
QUnit.asyncTest( "jQuery.fn.load( String, null )", 2, function( assert ) {
1988+
QUnit.test( "jQuery.fn.load( String, null )", 2, function( assert ) {
19751989
jQuery.ajaxSetup( {
19761990
beforeSend: function() {
19771991
assert.strictEqual( this.type, "GET", "no data means GET request" );
19781992
}
19791993
} );
1980-
jQuery( "#first" ).load( "data/name.html", null, start );
1994+
jQuery( "#first" ).load( "data/name.html", null, assert.async() );
19811995
} );
19821996

19831997
// check if load can be called with url and undefined data
1984-
QUnit.asyncTest( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
1998+
QUnit.test( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
19851999
jQuery.ajaxSetup( {
19862000
beforeSend: function() {
19872001
assert.strictEqual( this.type, "GET", "no data means GET request" );
19882002
}
19892003
} );
1990-
jQuery( "#first" ).load( "data/name.html", undefined, start );
2004+
jQuery( "#first" ).load( "data/name.html", undefined, assert.async() );
19912005
} );
19922006

19932007
// check if load can be called with only url

test/unit/attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,7 @@ var testToggleClass = function( valueObj, assert ) {
12671267

12681268
// Cleanup
12691269
e.removeClass( "testD" );
1270-
QUnit.expectJqData( this, e[ 0 ], "__className__" );
1270+
assert.expectJqData( this, e[ 0 ], "__className__" );
12711271
};
12721272

12731273
QUnit.test( "toggleClass(String|boolean|undefined[, boolean])", function( assert ) {
@@ -1470,7 +1470,7 @@ QUnit.test( "option value not trimmed when setting via parent select", function(
14701470
} );
14711471

14721472
QUnit.test( "Insignificant white space returned for $(option).val() (#14858)", function( assert ) {
1473-
expect ( 3 );
1473+
assert.expect( 3 );
14741474

14751475
var val = jQuery( "<option></option>" ).val();
14761476
assert.equal( val.length, 0, "Empty option should have no value" );

test/unit/core.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ QUnit.test( "jQuery()", function( assert ) {
3030
// few here but beware of modular builds where these methods may be excluded.
3131
if ( jQuery.fn.click ) {
3232
expected++;
33-
attrObj[ "click" ] = function() { ok( exec, "Click executed." ); };
33+
attrObj[ "click" ] = function() { assert.ok( exec, "Click executed." ); };
3434
}
3535
if ( jQuery.fn.width ) {
3636
expected++;
@@ -1303,7 +1303,9 @@ QUnit.test( "jQuery.proxy", function( assert ) {
13031303
assert.expect( 9 );
13041304

13051305
var test2, test3, test4, fn, cb,
1306-
test = function() { equal( this, thisObject, "Make sure that scope is set properly." ); },
1306+
test = function() {
1307+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
1308+
},
13071309
thisObject = { foo: "bar", method: test };
13081310

13091311
// Make sure normal works
@@ -1319,15 +1321,21 @@ QUnit.test( "jQuery.proxy", function( assert ) {
13191321
assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
13201322

13211323
// Partial application
1322-
test2 = function( a ) { equal( a, "pre-applied", "Ensure arguments can be pre-applied." ); };
1324+
test2 = function( a ) {
1325+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
1326+
};
13231327
jQuery.proxy( test2, null, "pre-applied" )();
13241328

13251329
// Partial application w/ normal arguments
1326-
test3 = function( a, b ) { equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." ); };
1330+
test3 = function( a, b ) {
1331+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
1332+
};
13271333
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
13281334

13291335
// Test old syntax
1330-
test4 = { "meth": function( a ) { equal( a, "boom", "Ensure old syntax works." ); } };
1336+
test4 = { "meth": function( a ) {
1337+
assert.equal( a, "boom", "Ensure old syntax works." );
1338+
} };
13311339
jQuery.proxy( test4, "meth" )( "boom" );
13321340

13331341
// jQuery 1.9 improved currying with `this` object
@@ -1386,7 +1394,7 @@ QUnit.test( "jQuery.parseHTML", function( assert ) {
13861394

13871395
if ( jQuery.support.createHTMLDocument ) {
13881396
QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
1389-
expect ( 1 );
1397+
assert.expect ( 1 );
13901398

13911399
Globals.register( "parseHTMLError" );
13921400

0 commit comments

Comments
 (0)