Skip to content

Commit

Permalink
Tests: further improvements QUnit 2.0 migration
Browse files Browse the repository at this point in the history
* Remove QUnit jshint globals
* Extend QUnit.assert methods
* Use assert.async instead of start/stop/done

Ref b930d14
Ref c8d15a2
  • Loading branch information
markelog committed Sep 8, 2015
1 parent f71e32d commit 2f0cedc
Show file tree
Hide file tree
Showing 16 changed files with 293 additions and 271 deletions.
13 changes: 0 additions & 13 deletions test/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,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,
Expand All @@ -51,8 +40,6 @@

"jQuery": true,
"sinon": true,
"module": true,
"test": true,
"amdDefined": true,
"fireNative": true,
"Globals": true,
Expand Down
6 changes: 3 additions & 3 deletions test/data/testinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
} );
Expand Down
16 changes: 8 additions & 8 deletions test/data/testrunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function keys( o ) {
* @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
Expand Down Expand Up @@ -83,7 +83,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, " +
Expand Down Expand Up @@ -111,7 +111,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;
Expand All @@ -123,15 +123,15 @@ window.moduleTeardown = function() {
expectedKeys = expectedDataKeys[ i ];
actualKeys = jQuery.cache[ i ] ? 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 + ")"
Expand All @@ -145,12 +145,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" );
}
Expand All @@ -168,7 +168,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;
}
};
Expand Down
74 changes: 44 additions & 30 deletions test/unit/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,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: {
Expand Down Expand Up @@ -1686,20 +1686,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()

Expand Down Expand Up @@ -1764,37 +1772,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 ) {
Expand All @@ -1809,23 +1823,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
Expand Down
4 changes: 2 additions & 2 deletions test/unit/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,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 ) {
Expand Down Expand Up @@ -1469,7 +1469,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" );
Expand Down
20 changes: 14 additions & 6 deletions test/unit/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand Down Expand Up @@ -1285,7 +1285,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
Expand All @@ -1301,15 +1303,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
Expand Down Expand Up @@ -1365,7 +1373,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" );

Expand Down
Loading

0 comments on commit 2f0cedc

Please sign in to comment.