Skip to content

Commit 8e5d1ca

Browse files
committed
Tests: More improvments for testrunner
Conform to style guide and simplify it
1 parent 92cff8b commit 8e5d1ca

File tree

2 files changed

+36
-59
lines changed

2 files changed

+36
-59
lines changed

test/data/testinit.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ original$ = this.$ = "replaced";
1616

1717
/**
1818
* Returns an array of elements with the given IDs
19-
* @example q("main", "foo", "bar")
19+
* @example q( "main", "foo", "bar" )
2020
* @result [<div id="main">, <span id="foo">, <input id="bar">]
2121
*/
2222
this.q = function() {
@@ -38,15 +38,15 @@ this.q = function() {
3838
* @result returns true if "//[a]" return two elements with the IDs 'foo' and 'baar'
3939
*/
4040
this.t = function( a, b, c ) {
41-
var f = jQuery(b).get(),
41+
var f = jQuery( b ).get(),
4242
s = "",
4343
i = 0;
4444

4545
for ( ; i < f.length; i++ ) {
4646
s += ( s && "," ) + '"' + f[ i ].id + '"';
4747
}
4848

49-
deepEqual(f, q.apply( q, c ), a + " (" + b + ")");
49+
deepEqual( f, q.apply( q, c ), a + " (" + b + ")" );
5050
};
5151

5252
this.createDashboardXML = function() {
@@ -98,27 +98,27 @@ this.createWithFriesXML = function() {
9898
this.createXMLFragment = function() {
9999
var xml, frag;
100100
if ( window.ActiveXObject ) {
101-
xml = new ActiveXObject("msxml2.domdocument");
101+
xml = new ActiveXObject( "msxml2.domdocument" );
102102
} else {
103103
xml = document.implementation.createDocument( "", "", null );
104104
}
105105

106106
if ( xml ) {
107-
frag = xml.createElement("data");
107+
frag = xml.createElement( "data" );
108108
}
109109

110110
return frag;
111111
};
112112

113113
fireNative = document.createEvent ?
114114
function( node, type ) {
115-
var event = document.createEvent('HTMLEvents');
115+
var event = document.createEvent( "HTMLEvents" );
116+
116117
event.initEvent( type, true, true );
117118
node.dispatchEvent( event );
118119
} :
119120
function( node, type ) {
120-
var event = document.createEventObject();
121-
node.fireEvent( 'on' + type, event );
121+
node.fireEvent( "on" + type, document.createEventObject() );
122122
};
123123

124124
/**
@@ -233,31 +233,28 @@ this.testIframe = function( fileName, name, fn ) {
233233
};
234234

235235
this.testIframeWithCallback = function( title, fileName, func ) {
236-
237-
test( title, function() {
236+
asyncTest( title, 1, function() {
238237
var iframe;
239238

240-
// Expect one assertion, but allow overrides
241-
expect( 1 );
242-
243-
stop();
244239
window.iframeCallback = function() {
245-
var self = this,
246-
args = arguments;
240+
var args = arguments;
241+
247242
setTimeout(function() {
248-
window.iframeCallback = undefined;
243+
this.iframeCallback = undefined;
244+
249245
iframe.remove();
250-
func.apply( self, args );
246+
func.apply( this, args );
251247
func = function() {};
248+
252249
start();
253-
}, 0 );
250+
});
254251
};
255252
iframe = jQuery( "<div/>" ).css({ position: "absolute", width: "500px", left: "-600px" })
256253
.append( jQuery( "<iframe/>" ).attr( "src", url( "./data/" + fileName ) ) )
257254
.appendTo( "#qunit-fixture" );
258255
});
259256
};
260-
window.iframeCallback = undefined;
257+
this.iframeCallback = undefined;
261258

262259
// Tests are always loaded async
263260
QUnit.config.autostart = false;

test/data/testrunner.js

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
define(function() {
2-
3-
// Allow subprojects to test against their own fixtures
4-
var qunitModule = QUnit.module,
5-
qunitTest = QUnit.test,
6-
// Store the old counts so that we only assert on tests that have actually leaked,
7-
// instead of asserting every time a test has leaked sometime in the past
8-
oldCacheLength = 0,
1+
(function() {
2+
3+
// Store the old counts so that we only assert on tests that have actually leaked,
4+
// instead of asserting every time a test has leaked sometime in the past
5+
var oldCacheLength = 0,
96
oldActive = 0,
107

118
expectedDataKeys = {},
12-
reset,
139
splice = [].splice,
1410
ajaxSettings = jQuery.ajaxSettings;
1511

16-
1712
/**
1813
* QUnit configuration
1914
*/
@@ -45,7 +40,7 @@ QUnit.expectJqData = function( elems, key ) {
4540
}
4641

4742
for ( i = 0; i < elems.length; i++ ) {
48-
elem = elems[i];
43+
elem = elems[ i ];
4944

5045
// jQuery.data only stores data for nodes in jQuery.cache,
5146
// for other data targets the data is stored in the object itself,
@@ -69,10 +64,10 @@ QUnit.expectJqData = function( elems, key ) {
6964
// (instead of in teardown).
7065
notStrictEqual( expando, undefined, "Target for expectJqData must have an expando, for else there can be no data to expect." );
7166
} else {
72-
if ( expectedDataKeys[expando] ) {
73-
expectedDataKeys[expando].push( key );
67+
if ( expectedDataKeys[ expando ] ) {
68+
expectedDataKeys[ expando ].push( key );
7469
} else {
75-
expectedDataKeys[expando] = [ key ];
70+
expectedDataKeys[ expando ] = [ key ];
7671
}
7772
}
7873
}
@@ -90,26 +85,25 @@ QUnit.config.urlConfig.push({
9085
* teardown function on all modules' lifecycle object.
9186
*/
9287
window.moduleTeardown = function() {
93-
var i,
94-
expectedKeys, actualKeys,
88+
var i, expectedKeys, actualKeys,
9589
cacheLength = 0;
9690

9791
// Only look for jQuery data problems if this test actually
9892
// provided some information to compare against.
9993
if ( QUnit.urlParams.jqdata || this.checkJqData ) {
10094
for ( i in jQuery.cache ) {
101-
expectedKeys = expectedDataKeys[i];
102-
actualKeys = jQuery.cache[i] ? Object.keys( jQuery.cache[i] ) : jQuery.cache[i];
95+
expectedKeys = expectedDataKeys[ i ];
96+
actualKeys = jQuery.cache[ i ] ? Object.keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
10397
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
10498
deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
10599
}
106-
delete jQuery.cache[i];
107-
delete expectedDataKeys[i];
100+
delete jQuery.cache[ i ];
101+
delete expectedDataKeys[ i ];
108102
}
109103
// In case it was removed from cache before (or never there in the first place)
110104
for ( i in expectedDataKeys ) {
111-
deepEqual( expectedDataKeys[i], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" );
112-
delete expectedDataKeys[i];
105+
deepEqual( expectedDataKeys[ i ], undefined, "No unexpected keys were left in jQuery.cache (#" + i + ")" );
106+
delete expectedDataKeys[ i ];
113107
}
114108
}
115109

@@ -143,18 +137,7 @@ window.moduleTeardown = function() {
143137
}
144138
};
145139

146-
QUnit.done(function() {
147-
// Remove our own fixtures outside #qunit-fixture
148-
supportjQuery("#qunit ~ *").remove();
149-
});
150-
151-
// jQuery-specific post-test cleanup
152-
reset = function () {
153-
154-
// Ensure jQuery events and data on the fixture are properly removed
155-
jQuery("#qunit-fixture").empty();
156-
// ...even if the jQuery under test has a broken .empty()
157-
supportjQuery("#qunit-fixture").empty();
140+
QUnit.testDone(function() {
158141

159142
// Reset internal jQuery state
160143
jQuery.event.global = {};
@@ -166,10 +149,7 @@ reset = function () {
166149

167150
// Cleanup globals
168151
Globals.cleanup();
169-
jQuery("#qunit-fixture")[0].innerHTML = QUnit.config.fixture;
170-
};
171-
172-
QUnit.testDone(reset);
152+
});
173153

174154
// Register globals for cleanup and the cleanup code itself
175155
window.Globals = (function() {
@@ -192,4 +172,4 @@ window.Globals = (function() {
192172
};
193173
})();
194174

195-
});
175+
})();

0 commit comments

Comments
 (0)