Skip to content

Commit 2f0cedc

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 Ref c8d15a2
1 parent f71e32d commit 2f0cedc

File tree

16 files changed

+293
-271
lines changed

16 files changed

+293
-271
lines changed

test/.jshintrc

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

5241
"jQuery": true,
5342
"sinon": true,
54-
"module": true,
55-
"test": true,
5643
"amdDefined": true,
5744
"fireNative": true,
5845
"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
@@ -43,7 +43,7 @@ function keys( o ) {
4343
* @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
4444
* @param {string} key
4545
*/
46-
QUnit.expectJqData = function( env, elems, key ) {
46+
QUnit.assert.expectJqData = function( env, elems, key ) {
4747
var i, elem, expando;
4848

4949
// As of jQuery 2.0, there will be no "cache"-data is
@@ -83,7 +83,7 @@ QUnit.expectJqData = function( env, elems, key ) {
8383
// Since this method was called it means some data was
8484
// expected to be found, but since there is nothing, fail early
8585
// (instead of in teardown).
86-
notStrictEqual(
86+
this.notStrictEqual(
8787
expando,
8888
undefined,
8989
"Target for expectJqData must have an expando, " +
@@ -111,7 +111,7 @@ QUnit.config.urlConfig.push( {
111111
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
112112
* teardown function on all modules' lifecycle object.
113113
*/
114-
window.moduleTeardown = function() {
114+
window.moduleTeardown = function( assert ) {
115115
var i,
116116
expectedKeys, actualKeys,
117117
cacheLength = 0;
@@ -123,15 +123,15 @@ window.moduleTeardown = function() {
123123
expectedKeys = expectedDataKeys[ i ];
124124
actualKeys = jQuery.cache[ i ] ? keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
125125
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
126-
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
126+
assert.deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
127127
}
128128
delete jQuery.cache[ i ];
129129
delete expectedDataKeys[ i ];
130130
}
131131

132132
// In case it was removed from cache before (or never there in the first place)
133133
for ( i in expectedDataKeys ) {
134-
deepEqual(
134+
assert.deepEqual(
135135
expectedDataKeys[ i ],
136136
undefined,
137137
"No unexpected keys were left in jQuery.cache (#" + i + ")"
@@ -145,12 +145,12 @@ window.moduleTeardown = function() {
145145

146146
// Check for (and clean up, if possible) incomplete animations/requests/etc.
147147
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
148-
equal( jQuery.timers.length, 0, "No timers are still running" );
148+
assert.equal( jQuery.timers.length, 0, "No timers are still running" );
149149
splice.call( jQuery.timers, 0, jQuery.timers.length );
150150
jQuery.fx.stop();
151151
}
152152
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
153-
equal( jQuery.active, oldActive, "No AJAX requests are still active" );
153+
assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
154154
if ( ajaxTest.abort ) {
155155
ajaxTest.abort( "active requests" );
156156
}
@@ -168,7 +168,7 @@ window.moduleTeardown = function() {
168168
// if we unconditionally assert any of these,
169169
// the test will fail with too many assertions :|
170170
if ( cacheLength !== oldCacheLength ) {
171-
equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
171+
assert.equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
172172
oldCacheLength = cacheLength;
173173
}
174174
};

test/unit/ajax.js

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ QUnit.module( "ajax", {
186186
firstTime = false;
187187
jQuery.ajax( this );
188188
} else {
189-
ok ( true, "Test retrying with jQuery.ajax(this) works" );
189+
assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
190190
jQuery.ajax( {
191191
url: url( "data/errorWithText.php" ),
192192
data: {
@@ -1686,20 +1686,28 @@ QUnit.module( "ajax", {
16861686
jQuery( document ).off( "ajaxStart ajaxStop" );
16871687
} );
16881688

1689-
QUnit.asyncTest( "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1, function( assert ) {
1690-
jQuery.ajaxSetup( {
1691-
type: "POST"
1692-
} );
1689+
QUnit.test(
1690+
"jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1,
1691+
function( assert ) {
1692+
var done = assert.async();
16931693

1694-
jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
1695-
assert.equal( method, "GET" );
1696-
QUnit.start();
1697-
} );
1698-
} );
1694+
jQuery.ajaxSetup( {
1695+
type: "POST"
1696+
} );
16991697

1700-
QUnit.asyncTest( "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2, function() {
1701-
jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", start );
1702-
} );
1698+
jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
1699+
assert.equal( method, "GET" );
1700+
done();
1701+
} );
1702+
}
1703+
);
1704+
1705+
QUnit.test(
1706+
"#11402 - jQuery.domManip() - script in comments are properly evaluated", 2,
1707+
function( assert ) {
1708+
jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", assert.async() );
1709+
}
1710+
);
17031711

17041712
//----------- jQuery.get()
17051713

@@ -1764,37 +1772,43 @@ QUnit.module( "ajax", {
17641772

17651773
//----------- jQuery.getScript()
17661774

1767-
QUnit.asyncTest( "jQuery.getScript( String, Function ) - with callback", 2, function( assert ) {
1768-
Globals.register( "testBar" );
1769-
jQuery.getScript( url( "data/testbar.php" ), function() {
1770-
assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
1771-
QUnit.start();
1772-
} );
1773-
} );
1775+
QUnit.test( "jQuery.getScript( String, Function ) - with callback", 2,
1776+
function( assert ) {
1777+
var done = assert.async();
1778+
1779+
Globals.register( "testBar" );
1780+
jQuery.getScript( url( "data/testbar.php" ), function() {
1781+
assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
1782+
done();
1783+
} );
1784+
}
1785+
);
17741786

1775-
QUnit.asyncTest( "jQuery.getScript( String, Function ) - no callback", 1, function() {
1787+
QUnit.test( "jQuery.getScript( String, Function ) - no callback", 1, function( assert ) {
17761788
Globals.register( "testBar" );
1777-
jQuery.getScript( url( "data/testbar.php" ) ).done( start );
1789+
jQuery.getScript( url( "data/testbar.php" ) ).done( assert.async() );
17781790
} );
17791791

1780-
QUnit.asyncTest( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
1792+
QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
1793+
var done = assert.async();
1794+
17811795
Globals.register( "testBar" );
17821796
jQuery.getScript( url( "data/testbar.php" ), function( data, _, jqXHR ) {
17831797
assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
1784-
QUnit.start();
1798+
done();
17851799
} );
17861800
} );
17871801

17881802
// //----------- jQuery.fn.load()
17891803

17901804
// check if load can be called with only url
1791-
QUnit.asyncTest( "jQuery.fn.load( String )", 2, function( assert ) {
1805+
QUnit.test( "jQuery.fn.load( String )", 2, function( assert ) {
17921806
jQuery.ajaxSetup( {
17931807
beforeSend: function() {
17941808
assert.strictEqual( this.type, "GET", "no data means GET request" );
17951809
}
17961810
} );
1797-
jQuery( "#first" ).load( "data/name.html", start );
1811+
jQuery( "#first" ).load( "data/name.html", assert.async() );
17981812
} );
17991813

18001814
QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
@@ -1809,23 +1823,23 @@ QUnit.module( "ajax", {
18091823
} );
18101824

18111825
// check if load can be called with url and null data
1812-
QUnit.asyncTest( "jQuery.fn.load( String, null )", 2, function( assert ) {
1826+
QUnit.test( "jQuery.fn.load( String, null )", 2, function( assert ) {
18131827
jQuery.ajaxSetup( {
18141828
beforeSend: function() {
18151829
assert.strictEqual( this.type, "GET", "no data means GET request" );
18161830
}
18171831
} );
1818-
jQuery( "#first" ).load( "data/name.html", null, start );
1832+
jQuery( "#first" ).load( "data/name.html", null, assert.async() );
18191833
} );
18201834

18211835
// check if load can be called with url and undefined data
1822-
QUnit.asyncTest( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
1836+
QUnit.test( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
18231837
jQuery.ajaxSetup( {
18241838
beforeSend: function() {
18251839
assert.strictEqual( this.type, "GET", "no data means GET request" );
18261840
}
18271841
} );
1828-
jQuery( "#first" ).load( "data/name.html", undefined, start );
1842+
jQuery( "#first" ).load( "data/name.html", undefined, assert.async() );
18291843
} );
18301844

18311845
// 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
@@ -1266,7 +1266,7 @@ var testToggleClass = function( valueObj, assert ) {
12661266

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

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

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

14741474
var val = jQuery( "<option></option>" ).val();
14751475
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++;
@@ -1285,7 +1285,9 @@ QUnit.test( "jQuery.proxy", function( assert ) {
12851285
assert.expect( 9 );
12861286

12871287
var test2, test3, test4, fn, cb,
1288-
test = function() { equal( this, thisObject, "Make sure that scope is set properly." ); },
1288+
test = function() {
1289+
assert.equal( this, thisObject, "Make sure that scope is set properly." );
1290+
},
12891291
thisObject = { foo: "bar", method: test };
12901292

12911293
// Make sure normal works
@@ -1301,15 +1303,21 @@ QUnit.test( "jQuery.proxy", function( assert ) {
13011303
assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
13021304

13031305
// Partial application
1304-
test2 = function( a ) { equal( a, "pre-applied", "Ensure arguments can be pre-applied." ); };
1306+
test2 = function( a ) {
1307+
assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
1308+
};
13051309
jQuery.proxy( test2, null, "pre-applied" )();
13061310

13071311
// Partial application w/ normal arguments
1308-
test3 = function( a, b ) { equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." ); };
1312+
test3 = function( a, b ) {
1313+
assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
1314+
};
13091315
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
13101316

13111317
// Test old syntax
1312-
test4 = { "meth": function( a ) { equal( a, "boom", "Ensure old syntax works." ); } };
1318+
test4 = { "meth": function( a ) {
1319+
assert.equal( a, "boom", "Ensure old syntax works." );
1320+
} };
13131321
jQuery.proxy( test4, "meth" )( "boom" );
13141322

13151323
// jQuery 1.9 improved currying with `this` object
@@ -1365,7 +1373,7 @@ QUnit.test( "jQuery.parseHTML", function( assert ) {
13651373

13661374
if ( jQuery.support.createHTMLDocument ) {
13671375
QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
1368-
expect ( 1 );
1376+
assert.expect ( 1 );
13691377

13701378
Globals.register( "parseHTMLError" );
13711379

0 commit comments

Comments
 (0)