Permalink
Browse files

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\(.*)(&gt\;) → $1>
  (^\s+test\(.*)(&lt\;) → $1<

[1] jQuery MinJsGz Release Police Department (do the same, download less)
  • Loading branch information...
1 parent 8121309 commit 36c9ecb0f50aec9063725b0c45e0cca98ef87084 @Krinkle Krinkle committed with dmethvin Oct 17, 2012
View
@@ -127,13 +127,17 @@ jQuery.fn.extend({
self[ state ? "addClass" : "removeClass" ]( className );
}
+ // Toggle whole class name
} else if ( type === "undefined" || type === "boolean" ) {
if ( this.className ) {
// store className if set
jQuery._data( this, "__className__", this.className );
}
- // toggle whole className
+ // If the element has a class name or if we're passed "false",
+ // then remove the whole classname (if there was one, the above saved it).
+ // Otherwise bring back whatever was previously saved (if anything),
+ // falling back to the empty string if nothing was stored.
this.className = this.className || value === false ? "" : jQuery._data( this, "__className__" ) || "";
}
});
View
@@ -145,52 +145,6 @@ function url( value ) {
}
(function () {
- // 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,
- oldFragmentsLength = 0,
- oldTimersLength = 0,
- oldActive = 0;
-
- /**
- * Ensures that tests have cleaned up properly after themselves. Should be passed as the
- * teardown function on all modules' lifecycle object.
- */
- this.moduleTeardown = function () {
- var i, fragmentsLength = 0, cacheLength = 0;
-
- // Allow QUnit.reset to clean up any attached elements before checking for leaks
- QUnit.reset();
-
- for ( i in jQuery.cache ) {
- ++cacheLength;
- }
-
- jQuery.fragments = {};
-
- for ( i in jQuery.fragments ) {
- ++fragmentsLength;
- }
-
- // 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 :|
- if ( cacheLength !== oldCacheLength ) {
- equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
- oldCacheLength = cacheLength;
- }
- if ( fragmentsLength !== oldFragmentsLength ) {
- equal( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" );
- oldFragmentsLength = fragmentsLength;
- }
- if ( jQuery.timers && jQuery.timers.length !== oldTimersLength ) {
- equal( jQuery.timers.length, oldTimersLength, "No timers are still running" );
- oldTimersLength = jQuery.timers.length;
- }
- if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
- equal( jQuery.active, 0, "No AJAX requests are still active" );
- oldActive = jQuery.active;
- }
- };
this.testIframe = function( fileName, name, fn ) {
@@ -10,6 +10,7 @@ var Sizzle = Sizzle || jQuery.find;
// Allow subprojects to test against their own fixtures
var qunitModule = QUnit.module,
qunitTest = QUnit.test;
+
function testSubproject( label, url, risTests ) {
var sub, fixture, fixtureHTML,
fixtureReplaced = false;
@@ -131,10 +132,152 @@ function testSubproject( label, url, risTests ) {
* QUnit hooks
*/
(function() {
- // jQuery-specific QUnit.reset
- var reset = QUnit.reset,
+ // 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,
+ oldFragmentsLength = 0,
+ oldTimersLength = 0,
+ oldActive = 0,
+
+ expectedDataKeys = {},
+
+ reset = QUnit.reset,
ajaxSettings = jQuery.ajaxSettings;
+ function keys(o) {
+ var ret, key;
+ if ( Object.keys ) {
+ ret = Object.keys( o );
+ } else {
+ ret = [];
+ for ( key in o ) {
+ ret.push( key );
+ }
+ }
+ ret.sort();
+ return ret;
+ }
+
+ /**
+ * @param {jQuery|HTMLElement|Object|Array} elems Target (or array of targets) for jQuery.data.
+ * @param {string} key
+ */
+ QUnit.expectJqData = function( elems, key ) {
+ var i, elem, expando;
+ QUnit.current_testEnvironment.checkJqData = true;
+
+ if ( elems.jquery && elems.toArray ) {
+ elems = elems.toArray();
+ }
+ if ( !jQuery.isArray( elems ) ) {
+ elems = [ elems ];
+ }
+
+ for ( i = 0; i < elems.length; i++ ) {
+ elem = elems[i];
+
+ // 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 ) {
+ // Fixes false positives for dataTests(window), dataTests({}).
+ continue;
+ }
+
+ expando = elem[ jQuery.expando ];
+
+ if ( expando === undefined ) {
+ // 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).
+ notStrictEqual( expando, undefined, 'Target for expectJqData must have an expando, for else there can be no data to expect.' );
+ } else {
+ if ( expectedDataKeys[expando] ) {
+ expectedDataKeys[expando].push( key );
+ } else {
+ expectedDataKeys[expando] = [ key ];
+ }
+ }
+ }
+ };
+ QUnit.config.urlConfig.push( {
+ id: 'jqdata',
+ label: 'Always check jQuery.data',
+ tooltip: 'Trigger "QUnit.expectJqData" detection for all tests instead of just the ones that call it'
+ } );
+
+ /**
+ * Ensures that tests have cleaned up properly after themselves. Should be passed as the
+ * teardown function on all modules' lifecycle object.
+ */
+ this.moduleTeardown = function() {
+ var i,
+ expectedKeys, actualKeys,
+ fragmentsLength = 0,
+ 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 ) {
+ 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' );
+ }
+ 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( expectedDataKeys[i], undefined, 'No unexpected keys were left in jQuery.cache (#' + i + ')' );
+ delete expectedDataKeys[i];
+ }
+ }
+
+ // Reset data register
+ expectedDataKeys = {};
+
+ // Allow QUnit.reset to clean up any attached elements before checking for leaks
+ QUnit.reset();
+
+ for ( i in jQuery.cache ) {
+ ++cacheLength;
+ }
+
+ jQuery.fragments = {};
+
+ for ( i in jQuery.fragments ) {
+ ++fragmentsLength;
+ }
+
+ // 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 :|
+ if ( cacheLength !== oldCacheLength ) {
+ equal( cacheLength, oldCacheLength, "No unit tests leak memory in jQuery.cache" );
+ oldCacheLength = cacheLength;
+ }
+ if ( fragmentsLength !== oldFragmentsLength ) {
+ equal( fragmentsLength, oldFragmentsLength, "No unit tests leak memory in jQuery.fragments" );
+ oldFragmentsLength = fragmentsLength;
+ }
+ if ( jQuery.timers && jQuery.timers.length !== oldTimersLength ) {
+ equal( jQuery.timers.length, oldTimersLength, "No timers are still running" );
+ oldTimersLength = jQuery.timers.length;
+ }
+ if ( jQuery.active !== undefined && jQuery.active !== oldActive ) {
+ equal( jQuery.active, 0, "No AJAX requests are still active" );
+ oldActive = jQuery.active;
+ }
+ };
+
+ // jQuery-specific QUnit.reset
QUnit.reset = function() {
// Ensure jQuery events and data on the fixture are properly removed
View
@@ -17,12 +17,20 @@
(function() {
var src = "../dist/jquery.js";
- QUnit.config.urlConfig.push( "min" );
+ QUnit.config.urlConfig.push( {
+ id: "min",
+ label: "Load minified",
+ tooltip: "Load the minified version of the jQuery build"
+ } );
if ( QUnit.urlParams.min ) {
src = "../dist/jquery.min.js";
}
- QUnit.config.urlConfig.push( "noqsa" );
+ QUnit.config.urlConfig.push( {
+ id: "noqsa",
+ label: "Disable querySelectorAll",
+ tooltip: "Disable the native document.querySelectorAll"
+ } );
if ( QUnit.urlParams.noqsa ) {
document.querySelectorAll = null;
}
@@ -281,34 +289,34 @@ <h2 id="qunit-userAgent"></h2>
</em>
<span id="siblingspan"></span>
</div>
- </div>
- </dl>
- <div id="fx-test-group" style="position:absolute;width:1px;height:1px;overflow:hidden;">
- <div id="fx-queue" name="test">
- <div id="fadein" class='chain test' name='div'>fadeIn<div>fadeIn</div></div>
- <div id="fadeout" class='chain test out'>fadeOut<div>fadeOut</div></div>
+ <div id="fx-test-group" style="position:absolute;width:1px;height:1px;overflow:hidden;">
+ <div id="fx-queue" name="test">
+ <div id="fadein" class='chain test' name='div'>fadeIn<div>fadeIn</div></div>
+ <div id="fadeout" class='chain test out'>fadeOut<div>fadeOut</div></div>
- <div id="show" class='chain test'>show<div>show</div></div>
- <div id="hide" class='chain test out'>hide<div>hide</div></div>
+ <div id="show" class='chain test'>show<div>show</div></div>
+ <div id="hide" class='chain test out'>hide<div>hide</div></div>
- <div id="togglein" class='chain test'>togglein<div>togglein</div></div>
- <div id="toggleout" class='chain test out'>toggleout<div>toggleout</div></div>
+ <div id="togglein" class='chain test'>togglein<div>togglein</div></div>
+ <div id="toggleout" class='chain test out'>toggleout<div>toggleout</div></div>
- <div id="slideup" class='chain test'>slideUp<div>slideUp</div></div>
- <div id="slidedown" class='chain test out'>slideDown<div>slideDown</div></div>
+ <div id="slideup" class='chain test'>slideUp<div>slideUp</div></div>
+ <div id="slidedown" class='chain test out'>slideDown<div>slideDown</div></div>
- <div id="slidetogglein" class='chain test'>slideToggleIn<div>slideToggleIn</div></div>
- <div id="slidetoggleout" class='chain test out'>slideToggleOut<div>slideToggleOut</div></div>
+ <div id="slidetogglein" class='chain test'>slideToggleIn<div>slideToggleIn</div></div>
+ <div id="slidetoggleout" class='chain test out'>slideToggleOut<div>slideToggleOut</div></div>
- <div id="fadetogglein" class='chain test'>fadeToggleIn<div>fadeToggleIn</div></div>
- <div id="fadetoggleout" class='chain test out'>fadeToggleOut<div>fadeToggleOut</div></div>
+ <div id="fadetogglein" class='chain test'>fadeToggleIn<div>fadeToggleIn</div></div>
+ <div id="fadetoggleout" class='chain test out'>fadeToggleOut<div>fadeToggleOut</div></div>
- <div id="fadeto" class='chain test'>fadeTo<div>fadeTo</div></div>
- </div>
+ <div id="fadeto" class='chain test'>fadeTo<div>fadeTo</div></div>
+ </div>
- <div id="fx-tests"></div>
+ <div id="fx-tests"></div>
+ </div>
</div>
+ </dl>
<map name="imgmap" id="imgmap">
<area shape="rect" coords="0,0,200,50">
</map>
@@ -1231,7 +1231,7 @@ var testToggleClass = function(valueObj) {
// multiple class names
e.addClass("testA testB");
- ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
+ ok( e.is(".testA.testB"), "Assert 2 different classes present" );
e.toggleClass( valueObj("testB testC") );
ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
e.toggleClass( valueObj("testA testC") );
@@ -1258,7 +1258,7 @@ var testToggleClass = function(valueObj) {
// Cleanup
e.removeClass("testD");
- jQuery._removeData( e[ 0 ], "__className__" );
+ QUnit.expectJqData( e[ 0 ], "__className__" );
};
test( "toggleClass(String|boolean|undefined[, boolean])", function() {
@@ -1269,10 +1269,12 @@ test( "toggleClass(Function[, boolean])", function() {
testToggleClass( functionReturningObj );
});
-test( "toggleClass(Fucntion[, boolean]) with incoming value", function() {
+test( "toggleClass(Function[, boolean]) with incoming value", function() {
expect( 14 );
- var e = jQuery("#firstp"), old = e.attr("class") || "";
+ var e = jQuery("#firstp"),
+ old = e.attr("class") || "";
+
ok( !e.is(".test"), "Assert class not present" );
e.toggleClass(function( i, val ) {
@@ -1316,10 +1318,6 @@ test( "toggleClass(Fucntion[, boolean]) with incoming value", function() {
return "test";
}, false );
ok( !e.is(".test"), "Assert class not present" );
-
- // Cleanup
- e.removeClass("test");
- jQuery._removeData( e[ 0 ], "__className__" );
});
test( "addClass, removeClass, hasClass", function() {
Oops, something went wrong.

0 comments on commit 36c9ecb

Please sign in to comment.