2015-08-16 06:59:58 +00:00
|
|
|
( function() {
|
2014-02-18 16:13:48 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
var oldCacheLength = 0,
|
2013-09-06 21:11:04 +00:00
|
|
|
oldActive = 0,
|
|
|
|
|
|
|
|
expectedDataKeys = {},
|
|
|
|
splice = [].splice,
|
|
|
|
ajaxSettings = jQuery.ajaxSettings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* QUnit configuration
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Max time for stop() and asyncTest() until it aborts test
|
|
|
|
// and start()'s the next test.
|
2015-04-26 15:11:10 +00:00
|
|
|
QUnit.config.testTimeout = 12e4; // 2 minutes
|
2013-09-06 21:11:04 +00:00
|
|
|
|
|
|
|
// Enforce an "expect" argument or expect() call in all test bodies.
|
|
|
|
QUnit.config.requireExpects = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
|
|
|
|
* @param {string} key
|
|
|
|
*/
|
2015-09-08 00:26:29 +00:00
|
|
|
QUnit.assert.expectJqData = function( env, elems, key ) {
|
2014-12-05 17:28:40 +00:00
|
|
|
var i, elem, expando;
|
2013-09-06 21:11:04 +00:00
|
|
|
|
|
|
|
// As of jQuery 2.0, there will be no "cache"-data is
|
|
|
|
// stored and managed completely below the API surface
|
|
|
|
if ( jQuery.cache ) {
|
2014-12-05 17:28:40 +00:00
|
|
|
env.checkJqData = true;
|
2013-09-06 21:11:04 +00:00
|
|
|
|
|
|
|
if ( elems.jquery && elems.toArray ) {
|
|
|
|
elems = elems.toArray();
|
|
|
|
}
|
2016-08-14 10:54:16 +00:00
|
|
|
if ( !Array.isArray( elems ) ) {
|
2013-09-06 21:11:04 +00:00
|
|
|
elems = [ elems ];
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 0; i < elems.length; i++ ) {
|
2014-02-18 16:13:48 +00:00
|
|
|
elem = elems[ i ];
|
2013-09-06 21:11:04 +00:00
|
|
|
|
|
|
|
// jQuery.data only stores data for nodes in jQuery.cache,
|
|
|
|
// for other data targets the data is stored in the object itself,
|
|
|
|
// in that case we can't test that target for memory leaks.
|
|
|
|
// But we don't have to since in that case the data will/must will
|
|
|
|
// be available as long as the object is not garbage collected by
|
|
|
|
// the js engine, and when it is, the data will be removed with it.
|
|
|
|
if ( !elem.nodeType ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
// Fixes false positives for dataTests(window), dataTests({}).
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
expando = elem[ jQuery.expando ];
|
|
|
|
|
|
|
|
if ( expando === undefined ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
// In this case the element exists fine, but
|
|
|
|
// jQuery.data (or internal data) was never (in)directly
|
|
|
|
// called.
|
|
|
|
// 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).
|
2015-09-08 00:26:29 +00:00
|
|
|
this.notStrictEqual(
|
2014-07-17 17:25:59 +00:00
|
|
|
expando,
|
|
|
|
undefined,
|
|
|
|
"Target for expectJqData must have an expando, " +
|
|
|
|
"for else there can be no data to expect."
|
|
|
|
);
|
2013-09-06 21:11:04 +00:00
|
|
|
} else {
|
2014-02-18 16:13:48 +00:00
|
|
|
if ( expectedDataKeys[ expando ] ) {
|
|
|
|
expectedDataKeys[ expando ].push( key );
|
2013-09-06 21:11:04 +00:00
|
|
|
} else {
|
2014-02-18 16:13:48 +00:00
|
|
|
expectedDataKeys[ expando ] = [ key ];
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.config.urlConfig.push( {
|
2013-09-06 21:11:04 +00:00
|
|
|
id: "jqdata",
|
|
|
|
label: "Always check jQuery.data",
|
2014-07-17 17:25:59 +00:00
|
|
|
tooltip: "Trigger QUnit.expectJqData detection for all tests " +
|
|
|
|
"instead of just the ones that call it"
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-09-06 21:11:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures that tests have cleaned up properly after themselves. Should be passed as the
|
|
|
|
* teardown function on all modules' lifecycle object.
|
|
|
|
*/
|
2015-09-08 00:26:29 +00:00
|
|
|
window.moduleTeardown = function( assert ) {
|
2014-02-18 16:13:48 +00:00
|
|
|
var i, expectedKeys, actualKeys,
|
2013-09-06 21:11:04 +00:00
|
|
|
cacheLength = 0;
|
|
|
|
|
|
|
|
// Only look for jQuery data problems if this test actually
|
|
|
|
// provided some information to compare against.
|
|
|
|
if ( QUnit.urlParams.jqdata || this.checkJqData ) {
|
|
|
|
for ( i in jQuery.cache ) {
|
2014-02-18 16:13:48 +00:00
|
|
|
expectedKeys = expectedDataKeys[ i ];
|
|
|
|
actualKeys = jQuery.cache[ i ] ? Object.keys( jQuery.cache[ i ] ) : jQuery.cache[ i ];
|
2013-09-06 21:11:04 +00:00
|
|
|
if ( !QUnit.equiv( expectedKeys, actualKeys ) ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.deepEqual( actualKeys, expectedKeys, "Expected keys exist in jQuery.cache" );
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
2014-02-18 16:13:48 +00:00
|
|
|
delete jQuery.cache[ i ];
|
|
|
|
delete expectedDataKeys[ i ];
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
// In case it was removed from cache before (or never there in the first place)
|
|
|
|
for ( i in expectedDataKeys ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.deepEqual(
|
2014-07-17 17:25:59 +00:00
|
|
|
expectedDataKeys[ i ],
|
|
|
|
undefined,
|
|
|
|
"No unexpected keys were left in jQuery.cache (#" + i + ")"
|
|
|
|
);
|
2014-02-18 16:13:48 +00:00
|
|
|
delete expectedDataKeys[ i ];
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset data register
|
|
|
|
expectedDataKeys = {};
|
|
|
|
|
|
|
|
// Check for (and clean up, if possible) incomplete animations/requests/etc.
|
|
|
|
if ( jQuery.timers && jQuery.timers.length !== 0 ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.equal( jQuery.timers.length, 0, "No timers are still running" );
|
2013-09-06 21:11:04 +00:00
|
|
|
splice.call( jQuery.timers, 0, jQuery.timers.length );
|
|
|
|
jQuery.fx.stop();
|
|
|
|
}
|
|
|
|
if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.equal( jQuery.active, oldActive, "No AJAX requests are still active" );
|
2013-09-06 21:11:04 +00:00
|
|
|
if ( ajaxTest.abort ) {
|
2015-08-16 06:59:58 +00:00
|
|
|
ajaxTest.abort( "active requests" );
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
|
|
|
oldActive = jQuery.active;
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:52:48 +00:00
|
|
|
Globals.cleanup();
|
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
for ( i in jQuery.cache ) {
|
|
|
|
++cacheLength;
|
|
|
|
}
|
|
|
|
|
2014-07-17 17:25:59 +00:00
|
|
|
// Because QUnit doesn't have a mechanism for retrieving
|
|
|
|
// the number of expected assertions for a test,
|
|
|
|
// if we unconditionally assert any of these,
|
|
|
|
// the test will fail with too many assertions :|
|
2013-09-06 21:11:04 +00:00
|
|
|
if ( cacheLength !== oldCacheLength ) {
|
2015-09-08 00:26:29 +00:00
|
|
|
assert.equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
|
2013-09-06 21:11:04 +00:00
|
|
|
oldCacheLength = cacheLength;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.done( function() {
|
|
|
|
|
2014-04-05 14:23:03 +00:00
|
|
|
// Remove our own fixtures outside #qunit-fixture
|
|
|
|
supportjQuery( "#qunit ~ *" ).remove();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-04-05 14:23:03 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
QUnit.testDone( function() {
|
2013-09-06 21:11:04 +00:00
|
|
|
|
2014-04-05 14:23:03 +00:00
|
|
|
// Ensure jQuery events and data on the fixture are properly removed
|
|
|
|
jQuery( "#qunit-fixture" ).empty();
|
2015-08-16 06:59:58 +00:00
|
|
|
|
2014-04-05 14:23:03 +00:00
|
|
|
// ...even if the jQuery under test has a broken .empty()
|
|
|
|
supportjQuery( "#qunit-fixture" ).empty();
|
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
// Reset internal jQuery state
|
|
|
|
jQuery.event.global = {};
|
|
|
|
if ( ajaxSettings ) {
|
|
|
|
jQuery.ajaxSettings = jQuery.extend( true, {}, ajaxSettings );
|
|
|
|
} else {
|
|
|
|
delete jQuery.ajaxSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup globals
|
|
|
|
Globals.cleanup();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-12-11 16:34:05 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
// Register globals for cleanup and the cleanup code itself
|
2015-08-16 06:59:58 +00:00
|
|
|
window.Globals = ( function() {
|
2013-09-06 21:11:04 +00:00
|
|
|
var globals = {};
|
2014-01-27 19:43:27 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
return {
|
|
|
|
register: function( name ) {
|
2014-01-27 19:43:27 +00:00
|
|
|
window[ name ] = globals[ name ] = true;
|
2013-09-06 21:11:04 +00:00
|
|
|
},
|
2014-01-27 19:43:27 +00:00
|
|
|
|
2013-09-06 21:11:04 +00:00
|
|
|
cleanup: function() {
|
2014-01-27 19:43:27 +00:00
|
|
|
var name;
|
|
|
|
|
|
|
|
for ( name in globals ) {
|
|
|
|
delete window[ name ];
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
2014-01-27 19:43:27 +00:00
|
|
|
|
|
|
|
globals = {};
|
2013-09-06 21:11:04 +00:00
|
|
|
}
|
|
|
|
};
|
2015-08-16 06:59:58 +00:00
|
|
|
} )();
|
Implement expectation test instead of using _removeData. Close gh-997.
* Removed inline usage of QUnit.reset() because it is messing with the
expectation model as reset does .empty() which does a recursive cleanData
on everything in #qunit-fixture, so any expectJqData above .reset() would
fail negatively.
Instead of calling reset inline, either updated the following assertions to
take previous assertions' state into account, or broke the test() up into
2 tests at the point where it would call QUnit.reset.
* After introducing the new memory leak discovery a whole bunch of tests were
failing as they didn't clean up everything. However I didn't (yet) add
QUnit.expectJqData calls all over the place because in most if not all of
these cases it is valid data storage. For example in test "data()", there
will be an internal data key for "parsedAttrs". This particular test isn't
intending to test for memory leaks, so therefor I made the new discovery
system only push failures when the test contains at least 1 call to
QUnit.expectJqData.
When not, we'll assume that whatever data is being stored is acceptable
because the relevant elements still exist in the DOM anyway (QUnit.reset
will remove the elements and clean up the data automatically).
I did add a "Always check jQuery.data" mode in the test suite that will
trigger it everywhere. Maybe one day we'll include a call to everywhere,
but for now I'm keeping the status quo: Only consider data left in storage
to be a problem if the test says so ("opt-in").
* Had to move #fx-tests inside the fixture because ".remove()" test would
otherwise remove stuff permanently and cause random other tests to fail
as "#hide div" would yield an empty collection.
(Why wasn't this in the fixture in the first place?)
As a result moving fx-tests into the fixture a whole bunch of tests failed
that relied on arbitrary stuff about the document-wide or fixture-wide
state (e.g. number of divs etc.). So I had to adjust various tests to
limit their sample data to not be so variable and unlimited...
* Moved out tests for expando cleanup into a separate test.
* Fixed implied global variable 'pass' in effects.js that was causing
"TypeError: boolean is not a function" in *UNRELATED* dimensions.js that
uses a global variable "pass = function () {};" ...
* Removed spurious calls to _removeData. The new test exposed various failures
e.g. where div[0] isn't being assigned any data anyway.
(queue.js and attributes.js toggleClass).
* Removed spurious clean up at the bottom of test() functions that are
already covered by the teardown (calling QUnit.reset or removeClass to
supposedly undo any changes).
* Documented the parentheses-less magic line in toggleClass. It appeared that
it would always keep the current class name if there was any (since the
assignment started with "this.className || ...".
Adding parentheses + spacing is 8 bytes (though only 1 in gzip apparently).
Only added the comment for now, though I prefer clarity with logical
operators, I'd rather not face the yayMinPD[1] in this test-related commit.
* Updated QUnit urlConfig to the new format (raw string is deprecated).
* Clean up odd htmlentities in test titles, QUnit escapes this.
(^\s+test\(.*)(>\;) → $1>
(^\s+test\(.*)(<\;) → $1<
[1] jQuery MinJsGz Release Police Department (do the same, download less)
2012-10-17 08:33:47 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} )();
|