Tests: More improvments for testrunner

Conform to style guide and simplify it
This commit is contained in:
Oleg Gaidarenko 2014-02-18 20:13:48 +04:00
parent 92cff8b1a3
commit 8e5d1caf7a
2 changed files with 35 additions and 58 deletions

View File

@ -16,7 +16,7 @@ original$ = this.$ = "replaced";
/** /**
* Returns an array of elements with the given IDs * Returns an array of elements with the given IDs
* @example q("main", "foo", "bar") * @example q( "main", "foo", "bar" )
* @result [<div id="main">, <span id="foo">, <input id="bar">] * @result [<div id="main">, <span id="foo">, <input id="bar">]
*/ */
this.q = function() { this.q = function() {
@ -38,7 +38,7 @@ this.q = function() {
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar' * @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
*/ */
this.t = function( a, b, c ) { this.t = function( a, b, c ) {
var f = jQuery(b).get(), var f = jQuery( b ).get(),
s = "", s = "",
i = 0; i = 0;
@ -46,7 +46,7 @@ this.t = function( a, b, c ) {
s += ( s && "," ) + '"' + f[ i ].id + '"'; s += ( s && "," ) + '"' + f[ i ].id + '"';
} }
deepEqual(f, q.apply( q, c ), a + " (" + b + ")"); deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
}; };
this.createDashboardXML = function() { this.createDashboardXML = function() {
@ -98,13 +98,13 @@ this.createWithFriesXML = function() {
this.createXMLFragment = function() { this.createXMLFragment = function() {
var xml, frag; var xml, frag;
if ( window.ActiveXObject ) { if ( window.ActiveXObject ) {
xml = new ActiveXObject("msxml2.domdocument"); xml = new ActiveXObject( "msxml2.domdocument" );
} else { } else {
xml = document.implementation.createDocument( "", "", null ); xml = document.implementation.createDocument( "", "", null );
} }
if ( xml ) { if ( xml ) {
frag = xml.createElement("data"); frag = xml.createElement( "data" );
} }
return frag; return frag;
@ -112,13 +112,13 @@ this.createXMLFragment = function() {
fireNative = document.createEvent ? fireNative = document.createEvent ?
function( node, type ) { function( node, type ) {
var event = document.createEvent('HTMLEvents'); var event = document.createEvent( "HTMLEvents" );
event.initEvent( type, true, true ); event.initEvent( type, true, true );
node.dispatchEvent( event ); node.dispatchEvent( event );
} : } :
function( node, type ) { function( node, type ) {
var event = document.createEventObject(); node.fireEvent( "on" + type, document.createEventObject() );
node.fireEvent( 'on' + type, event );
}; };
/** /**
@ -233,31 +233,28 @@ this.testIframe = function( fileName, name, fn ) {
}; };
this.testIframeWithCallback = function( title, fileName, func ) { this.testIframeWithCallback = function( title, fileName, func ) {
asyncTest( title, 1, function() {
test( title, function() {
var iframe; var iframe;
// Expect one assertion, but allow overrides
expect( 1 );
stop();
window.iframeCallback = function() { window.iframeCallback = function() {
var self = this, var args = arguments;
args = arguments;
setTimeout(function() { setTimeout(function() {
window.iframeCallback = undefined; this.iframeCallback = undefined;
iframe.remove(); iframe.remove();
func.apply( self, args ); func.apply( this, args );
func = function() {}; func = function() {};
start(); start();
}, 0 ); });
}; };
iframe = jQuery( "<div/>" ).css({ position: "absolute", width: "500px", left: "-600px" }) iframe = jQuery( "<div/>" ).css({ position: "absolute", width: "500px", left: "-600px" })
.append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) ) .append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) )
.appendTo( "#qunit-fixture" ); .appendTo( "#qunit-fixture" );
}); });
}; };
window.iframeCallback = undefined; this.iframeCallback = undefined;
// Tests are always loaded async // Tests are always loaded async
QUnit.config.autostart = false; QUnit.config.autostart = false;

View File

@ -1,19 +1,14 @@
define(function() { (function() {
// Allow subprojects to test against their own fixtures // Store the old counts so that we only assert on tests that have actually leaked,
var qunitModule = QUnit.module, // instead of asserting every time a test has leaked sometime in the past
qunitTest = QUnit.test, var oldCacheLength = 0,
// Store the old counts so that we only assert on tests that have actually leaked,
// instead of asserting every time a test has leaked sometime in the past
oldCacheLength = 0,
oldActive = 0, oldActive = 0,
expectedDataKeys = {}, expectedDataKeys = {},
reset,
splice = [].splice, splice = [].splice,
ajaxSettings = jQuery.ajaxSettings; ajaxSettings = jQuery.ajaxSettings;
/** /**
* QUnit configuration * QUnit configuration
*/ */
@ -45,7 +40,7 @@ QUnit.expectJqData = function( elems, key ) {
} }
for ( i = 0; i < elems.length; i++ ) { for ( i = 0; i < elems.length; i++ ) {
elem = elems[i]; elem = elems[ i ];
// jQuery.data only stores data for nodes in jQuery.cache, // jQuery.data only stores data for nodes in jQuery.cache,
// for other data targets the data is stored in the object itself, // for other data targets the data is stored in the object itself,
@ -69,10 +64,10 @@ QUnit.expectJqData = function( elems, key ) {
// (instead of in teardown). // (instead of in teardown).
notStrictEqual( expando, undefined, "Target for expectJqData must have an expando, for else there can be no data to expect." ); notStrictEqual( expando, undefined, "Target for expectJqData must have an expando, for else there can be no data to expect." );
} else { } else {
if ( expectedDataKeys[expando] ) { if ( expectedDataKeys[ expando ] ) {
expectedDataKeys[expando].push( key ); expectedDataKeys[ expando ].push( key );
} else { } else {
expectedDataKeys[expando] = [ key ]; expectedDataKeys[ expando ] = [ key ];
} }
} }
} }
@ -90,26 +85,25 @@ QUnit.config.urlConfig.push({
* teardown function on all modules' lifecycle object. * teardown function on all modules' lifecycle object.
*/ */
window.moduleTeardown = function() { window.moduleTeardown = function() {
var i, var i, expectedKeys, actualKeys,
expectedKeys, actualKeys,
cacheLength = 0; cacheLength = 0;
// Only look for jQuery data problems if this test actually // Only look for jQuery data problems if this test actually
// provided some information to compare against. // provided some information to compare against.
if ( QUnit.urlParams.jqdata || this.checkJqData ) { if ( QUnit.urlParams.jqdata || this.checkJqData ) {
for ( i in jQuery.cache ) { for ( i in jQuery.cache ) {
expectedKeys = expectedDataKeys[i]; expectedKeys = expectedDataKeys[ i ];
actualKeys = jQuery.cache[i] ? Object.keys( jQuery.cache[i] ) : jQuery.cache[i]; actualKeys = jQuery.cache[ i ] ? Object.keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) { if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" ); deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
} }
delete jQuery.cache[i]; delete jQuery.cache[ i ];
delete expectedDataKeys[i]; delete expectedDataKeys[ i ];
} }
// In case it was removed from cache before (or never there in the first place) // In case it was removed from cache before (or never there in the first place)
for ( i in expectedDataKeys ) { for ( i in expectedDataKeys ) {
deepEqual( expectedDataKeys[i], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" ); deepEqual( expectedDataKeys[ i ], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" );
delete expectedDataKeys[i]; delete expectedDataKeys[ i ];
} }
} }
@ -143,18 +137,7 @@ window.moduleTeardown = function() {
} }
}; };
QUnit.done(function() { QUnit.testDone(function() {
// Remove our own fixtures outside #qunit-fixture
supportjQuery("#qunit ~ *").remove();
});
// jQuery-specific post-test cleanup
reset = function () {
// Ensure jQuery events and data on the fixture are properly removed
jQuery("#qunit-fixture").empty();
// ...even if the jQuery under test has a broken .empty()
supportjQuery("#qunit-fixture").empty();
// Reset internal jQuery state // Reset internal jQuery state
jQuery.event.global = {}; jQuery.event.global = {};
@ -166,10 +149,7 @@ reset = function () {
// Cleanup globals // Cleanup globals
Globals.cleanup(); Globals.cleanup();
jQuery("#qunit-fixture")[0].innerHTML = QUnit.config.fixture; });
};
QUnit.testDone(reset);
// Register globals for cleanup and the cleanup code itself // Register globals for cleanup and the cleanup code itself
window.Globals = (function() { window.Globals = (function() {
@ -192,4 +172,4 @@ window.Globals = (function() {
}; };
})(); })();
}); })();