Permalink
Browse files

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
  • Loading branch information...
markelog committed Sep 8, 2015
1 parent 3dd3d13 commit c8d15a2f9f108e90d3651c31e4abf45415a30fde
View
@@ -27,17 +27,6 @@
"Promise": false,
"Symbol": false,
"QUnit": false,
- "ok": false,
- "equal": false,
- "asyncTest": false,
- "notEqual": false,
- "deepEqual": false,
- "strictEqual": false,
- "notStrictEqual": false,
- "start": false,
- "stop": false,
- "expect": false,
- "throws": false,
"ajaxTest": false,
"testIframe": false,
"testIframeWithCallback": false,
@@ -52,8 +41,6 @@
"jQuery": true,
"sinon": true,
- "module": true,
- "test": true,
"amdDefined": true,
"fireNative": true,
"Globals": true,
View
@@ -38,7 +38,7 @@ this.q = function() {
* @example t("Check for something", "//[a]", ["foo", "baar"]);
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
*/
-this.t = function( a, b, c ) {
+QUnit.assert.t = function( a, b, c ) {
var f = jQuery( b ).get(),
s = "",
i = 0;
@@ -47,7 +47,7 @@ this.t = function( a, b, c ) {
s += ( s && "," ) + '"' + f[ i ].id + '"';
}
- deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
+ this.deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
};
this.createDashboardXML = function() {
@@ -199,7 +199,7 @@ this.ajaxTest = function( title, expect, options ) {
if ( !completed ) {
completed = true;
delete ajaxTest.abort;
- ok( false, "aborted " + reason );
+ assert.ok( false, "aborted " + reason );
jQuery.each( requests, function( i, request ) {
request.abort();
} );
View
@@ -24,7 +24,7 @@ QUnit.config.requireExpects = true;
* @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
* @param {string} key
*/
-QUnit.expectJqData = function( env, elems, key ) {
+QUnit.assert.expectJqData = function( env, elems, key ) {
var i, elem, expando;
// As of jQuery 2.0, there will be no "cache"-data is
@@ -64,7 +64,7 @@ QUnit.expectJqData = function( env, elems, key ) {
// Since this method was called it means some data was
// expected to be found, but since there is nothing, fail early
// (instead of in teardown).
- notStrictEqual(
+ this.notStrictEqual(
expando,
undefined,
"Target for expectJqData must have an expando, " +
@@ -92,7 +92,7 @@ QUnit.config.urlConfig.push( {
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
* teardown function on all modules' lifecycle object.
*/
-window.moduleTeardown = function() {
+window.moduleTeardown = function( assert ) {
var i, expectedKeys, actualKeys,
cacheLength = 0;
@@ -103,15 +103,15 @@ window.moduleTeardown = function() {
expectedKeys = expectedDataKeys[ i ];
actualKeys = jQuery.cache[ i ] ? Object.keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
- deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
+ assert.deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
}
delete jQuery.cache[ i ];
delete expectedDataKeys[ i ];
}
// In case it was removed from cache before (or never there in the first place)
for ( i in expectedDataKeys ) {
- deepEqual(
+ assert.deepEqual(
expectedDataKeys[ i ],
undefined,
"No unexpected keys were left in jQuery.cache (#" + i + ")"
@@ -125,12 +125,12 @@ window.moduleTeardown = function() {
// Check for (and clean up, if possible) incomplete animations/requests/etc.
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
- equal( jQuery.timers.length, 0, "No timers are still running" );
+ assert.equal( jQuery.timers.length, 0, "No timers are still running" );
splice.call( jQuery.timers, 0, jQuery.timers.length );
jQuery.fx.stop();
}
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
- equal( jQuery.active, oldActive, "No AJAX requests are still active" );
+ assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
if ( ajaxTest.abort ) {
ajaxTest.abort( "active requests" );
}
@@ -148,7 +148,7 @@ window.moduleTeardown = function() {
// if we unconditionally assert any of these,
// the test will fail with too many assertions :|
if ( cacheLength !== oldCacheLength ) {
- equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
+ assert.equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
oldCacheLength = cacheLength;
}
};
View
@@ -172,7 +172,7 @@ QUnit.module( "ajax", {
firstTime = false;
jQuery.ajax( this );
} else {
- ok ( true, "Test retrying with jQuery.ajax(this) works" );
+ assert.ok( true, "Test retrying with jQuery.ajax(this) works" );
jQuery.ajax( {
url: url( "data/errorWithText.php" ),
data: {
@@ -1848,20 +1848,28 @@ QUnit.module( "ajax", {
jQuery( document ).off( "ajaxStart ajaxStop" );
} );
- QUnit.asyncTest( "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1, function( assert ) {
- jQuery.ajaxSetup( {
- type: "POST"
- } );
+ QUnit.test(
+ "jQuery#load() - always use GET method even if it overrided through ajaxSetup (#11264)", 1,
+ function( assert ) {
+ var done = assert.async();
- jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
- assert.equal( method, "GET" );
- QUnit.start();
- } );
- } );
+ jQuery.ajaxSetup( {
+ type: "POST"
+ } );
- QUnit.asyncTest( "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2, function() {
- jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", start );
- } );
+ jQuery( "#qunit-fixture" ).load( "data/ajax/method.php", function( method ) {
+ assert.equal( method, "GET" );
+ done();
+ } );
+ }
+ );
+
+ QUnit.test(
+ "#11402 - jQuery.domManip() - script in comments are properly evaluated", 2,
+ function( assert ) {
+ jQuery( "#qunit-fixture" ).load( "data/cleanScript.html", assert.async() );
+ }
+ );
//----------- jQuery.get()
@@ -1926,37 +1934,43 @@ QUnit.module( "ajax", {
//----------- jQuery.getScript()
- QUnit.asyncTest( "jQuery.getScript( String, Function ) - with callback", 2, function( assert ) {
- Globals.register( "testBar" );
- jQuery.getScript( url( "data/testbar.php" ), function() {
- assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
- QUnit.start();
- } );
- } );
+ QUnit.test( "jQuery.getScript( String, Function ) - with callback", 2,
+ function( assert ) {
+ var done = assert.async();
+
+ Globals.register( "testBar" );
+ jQuery.getScript( url( "data/testbar.php" ), function() {
+ assert.strictEqual( window[ "testBar" ], "bar", "Check if script was evaluated" );
+ done();
+ } );
+ }
+ );
- QUnit.asyncTest( "jQuery.getScript( String, Function ) - no callback", 1, function() {
+ QUnit.test( "jQuery.getScript( String, Function ) - no callback", 1, function( assert ) {
Globals.register( "testBar" );
- jQuery.getScript( url( "data/testbar.php" ) ).done( start );
+ jQuery.getScript( url( "data/testbar.php" ) ).done( assert.async() );
} );
- QUnit.asyncTest( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
+ QUnit.test( "#8082 - jQuery.getScript( String, Function ) - source as responseText", 2, function( assert ) {
+ var done = assert.async();
+
Globals.register( "testBar" );
jQuery.getScript( url( "data/testbar.php" ), function( data, _, jqXHR ) {
assert.strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script" );
- QUnit.start();
+ done();
} );
} );
// //----------- jQuery.fn.load()
// check if load can be called with only url
- QUnit.asyncTest( "jQuery.fn.load( String )", 2, function( assert ) {
+ QUnit.test( "jQuery.fn.load( String )", 2, function( assert ) {
jQuery.ajaxSetup( {
beforeSend: function() {
assert.strictEqual( this.type, "GET", "no data means GET request" );
}
} );
- jQuery( "#first" ).load( "data/name.html", start );
+ jQuery( "#first" ).load( "data/name.html", assert.async() );
} );
QUnit.test( "jQuery.fn.load() - 404 error callbacks", function( assert ) {
@@ -1971,23 +1985,23 @@ QUnit.module( "ajax", {
} );
// check if load can be called with url and null data
- QUnit.asyncTest( "jQuery.fn.load( String, null )", 2, function( assert ) {
+ QUnit.test( "jQuery.fn.load( String, null )", 2, function( assert ) {
jQuery.ajaxSetup( {
beforeSend: function() {
assert.strictEqual( this.type, "GET", "no data means GET request" );
}
} );
- jQuery( "#first" ).load( "data/name.html", null, start );
+ jQuery( "#first" ).load( "data/name.html", null, assert.async() );
} );
// check if load can be called with url and undefined data
- QUnit.asyncTest( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
+ QUnit.test( "jQuery.fn.load( String, undefined )", 2, function( assert ) {
jQuery.ajaxSetup( {
beforeSend: function() {
assert.strictEqual( this.type, "GET", "no data means GET request" );
}
} );
- jQuery( "#first" ).load( "data/name.html", undefined, start );
+ jQuery( "#first" ).load( "data/name.html", undefined, assert.async() );
} );
// check if load can be called with only url
View
@@ -1267,7 +1267,7 @@ var testToggleClass = function( valueObj, assert ) {
// Cleanup
e.removeClass( "testD" );
- QUnit.expectJqData( this, e[ 0 ], "__className__" );
+ assert.expectJqData( this, e[ 0 ], "__className__" );
};
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(
} );
QUnit.test( "Insignificant white space returned for $(option).val() (#14858)", function( assert ) {
- expect ( 3 );
+ assert.expect( 3 );
var val = jQuery( "<option></option>" ).val();
assert.equal( val.length, 0, "Empty option should have no value" );
View
@@ -30,7 +30,7 @@ QUnit.test( "jQuery()", function( assert ) {
// few here but beware of modular builds where these methods may be excluded.
if ( jQuery.fn.click ) {
expected++;
- attrObj[ "click" ] = function() { ok( exec, "Click executed." ); };
+ attrObj[ "click" ] = function() { assert.ok( exec, "Click executed." ); };
}
if ( jQuery.fn.width ) {
expected++;
@@ -1303,7 +1303,9 @@ QUnit.test( "jQuery.proxy", function( assert ) {
assert.expect( 9 );
var test2, test3, test4, fn, cb,
- test = function() { equal( this, thisObject, "Make sure that scope is set properly." ); },
+ test = function() {
+ assert.equal( this, thisObject, "Make sure that scope is set properly." );
+ },
thisObject = { foo: "bar", method: test };
// Make sure normal works
@@ -1319,15 +1321,21 @@ QUnit.test( "jQuery.proxy", function( assert ) {
assert.equal( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." );
// Partial application
- test2 = function( a ) { equal( a, "pre-applied", "Ensure arguments can be pre-applied." ); };
+ test2 = function( a ) {
+ assert.equal( a, "pre-applied", "Ensure arguments can be pre-applied." );
+ };
jQuery.proxy( test2, null, "pre-applied" )();
// Partial application w/ normal arguments
- test3 = function( a, b ) { equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." ); };
+ test3 = function( a, b ) {
+ assert.equal( b, "normal", "Ensure arguments can be pre-applied and passed as usual." );
+ };
jQuery.proxy( test3, null, "pre-applied" )( "normal" );
// Test old syntax
- test4 = { "meth": function( a ) { equal( a, "boom", "Ensure old syntax works." ); } };
+ test4 = { "meth": function( a ) {
+ assert.equal( a, "boom", "Ensure old syntax works." );
+ } };
jQuery.proxy( test4, "meth" )( "boom" );
// jQuery 1.9 improved currying with `this` object
@@ -1386,7 +1394,7 @@ QUnit.test( "jQuery.parseHTML", function( assert ) {
if ( jQuery.support.createHTMLDocument ) {
QUnit.asyncTest( "jQuery.parseHTML", function( assert ) {
- expect ( 1 );
+ assert.expect ( 1 );
Globals.register( "parseHTMLError" );
Oops, something went wrong.

0 comments on commit c8d15a2

Please sign in to comment.