From 305eefe6abadbac021a84d031cb4649022657ac7 Mon Sep 17 00:00:00 2001 From: cjqed Date: Tue, 24 Dec 2013 11:35:41 -0500 Subject: [PATCH] Tests: Avoid use of QUnit.reset() in tests by splitting them Closes gh-1470 Ref #1457 --- test/data/testrunner.js | 17 +- test/unit/attributes.js | 23 +- test/unit/core.js | 2 - test/unit/css.js | 12 +- test/unit/effects.js | 6 +- test/unit/manipulation.js | 640 ++++++++++++++++++++++++++++---------- test/unit/wrap.js | 90 ++++-- 7 files changed, 564 insertions(+), 226 deletions(-) diff --git a/test/data/testrunner.js b/test/data/testrunner.js index 34f47381e..b25c4146f 100644 --- a/test/data/testrunner.js +++ b/test/data/testrunner.js @@ -13,7 +13,7 @@ var oldStart = window.start, expectedDataKeys = {}, splice = [].splice, - reset = QUnit.reset, + reset, ajaxSettings = jQuery.ajaxSettings; @@ -157,8 +157,7 @@ window.moduleTeardown = function() { oldActive = jQuery.active; } - // Allow QUnit.reset to clean up any attached elements before checking for leaks - QUnit.reset(); + reset(); for ( i in jQuery.cache ) { ++cacheLength; @@ -187,8 +186,8 @@ QUnit.done(function() { supportjQuery("#qunit ~ *").remove(); }); -// jQuery-specific QUnit.reset -QUnit.reset = function() { +// jQuery-specific post-test cleanup +reset = function() { // Ensure jQuery events and data on the fixture are properly removed jQuery("#qunit-fixture").empty(); @@ -206,10 +205,11 @@ QUnit.reset = function() { // Cleanup globals Globals.cleanup(); - // Let QUnit reset the fixture - reset.apply( this, arguments ); + jQuery("#qunit-fixture")[0].innerHTML = QUnit.config.fixture; }; +QUnit.testDone(reset); + // Register globals for cleanup and the cleanup code itself // Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs window.Globals = (function() { @@ -351,7 +351,7 @@ function testSubproject( label, subProjectURL, risTests, complete ) { // WARNING: UNDOCUMENTED INTERFACE QUnit.config.fixture = fixtureHTML; - QUnit.reset(); + reset(); if ( supportjQuery("#qunit-fixture").html() !== fixtureHTML ) { ok( false, "Copied subproject fixture" ); return; @@ -359,7 +359,6 @@ function testSubproject( label, subProjectURL, risTests, complete ) { fixtureReplaced = true; } - fn.apply( this, arguments ); }; } diff --git a/test/unit/attributes.js b/test/unit/attributes.js index 4f08e0e40..8ba2b4459 100644 --- a/test/unit/attributes.js +++ b/test/unit/attributes.js @@ -639,7 +639,7 @@ test( "removeAttr(Multi String, variable space width)", function() { }); test( "prop(String, Object)", function() { - expect( 31 ); + expect( 17 ); equal( jQuery("#text1").prop("value"), "Test", "Check for value attribute" ); equal( jQuery("#text1").prop( "value", "Test2" ).prop("defaultValue"), "Test", "Check for defaultValue attribute" ); @@ -667,7 +667,11 @@ test( "prop(String, Object)", function() { equal( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" ); jQuery("#table").prop( "frameborder", 1 ); equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" ); - QUnit.reset(); +}); + +test( "prop(String, Object) on null/undefined", function() { + + expect( 14 ); var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form, body = document.body, @@ -799,16 +803,19 @@ test( "removeProp(String)", function() { }); }); -test( "val()", function() { - expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) ); - - var checks, $button; +test( "val() after modification", function() { + expect( 1 ); document.getElementById("text1").value = "bla"; equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" ); - QUnit.reset(); +}); +test( "val()", function() { + + expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) ); + + var checks, $button; equal( jQuery("#text1").val(), "Test", "Check for value of input element" ); // ticket #1714 this caused a JS error in IE equal( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" ); @@ -923,7 +930,6 @@ if ( "value" in document.createElement("meter") && var testVal = function( valueObj ) { expect( 8 ); - QUnit.reset(); jQuery("#text1").val( valueObj("test") ); equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" ); @@ -978,7 +984,6 @@ test( "val(Array of Numbers) (Bug #7123)", function() { test( "val(Function) with incoming value", function() { expect( 10 ); - QUnit.reset(); var oldVal = jQuery("#text1").val(); jQuery("#text1").val(function( i, val ) { diff --git a/test/unit/core.js b/test/unit/core.js index bc21b0468..678350a3e 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -592,7 +592,6 @@ test("jQuery('html')", function() { var s, div, j; - QUnit.reset(); jQuery["foo"] = false; s = jQuery("")[0]; ok( s, "Creating a script" ); @@ -608,7 +607,6 @@ test("jQuery('html')", function() { equal( div.childNodes[1].nodeType, 1, "Paragraph." ); equal( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." ); - QUnit.reset(); ok( jQuery("")[0], "Creating a link" ); ok( !jQuery("", "" ].join("")) ); - - QUnit.reset(); - fixture.html( valueObj( fixture.text() ) ); - ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" ); } test( "html(String|Number)", function() { @@ -1491,11 +1721,29 @@ test( "html(Function)", function() { testHtml( manipulationFunctionReturningObj ); }); -test( "html(Function) with incoming value", function() { +test( "html( $.text() )", function() { - expect( 18 ); + expect( 1 ); - var els, actualhtml, pass, j, $div, $div2, insert; + var fixture = jQuery("#qunit-fixture"); + fixture.html( fixture.text() ); + ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" ); +}); + +test( "html( fn ) returns $.text()", function() { + + expect( 1 ); + + var fixture = jQuery("#qunit-fixture"); + fixture.html( manipulationFunctionReturningObj( fixture.text() ) ); + ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" ); +}); + +test( "html(Function) with incoming value -- direct selection", function() { + + expect( 4 ); + + var els, actualhtml, pass; els = jQuery("#foo > p"); actualhtml = els.map(function() { @@ -1514,9 +1762,14 @@ test( "html(Function) with incoming value", function() { } }); ok( pass, "Set HTML" ); +}); + +test( "html(Function) with incoming value -- jQuery.contents()", function() { + + expect( 14 ); + + var actualhtml, j, $div, $div2, insert; - QUnit.reset(); - // using contents will get comments regular, text, and comment nodes j = jQuery("#nonnodes").contents(); actualhtml = j.map(function() { return jQuery( this ).html(); @@ -1584,52 +1837,51 @@ test( "clone()/html() don't expose jQuery/Sizzle expandos (#12858)", function() ok( expected.test( $content.html() ), "html()" ); }); -function testRemove( method ) { - var markup, div, - first = jQuery("#ap").children().first(); +test( "remove() no filters", function() { + + expect( 3 ); + + var first = jQuery("#ap").children().first(); first.data("foo", "bar"); - jQuery("#ap").children()[ method ](); + jQuery("#ap").children().remove(); ok( jQuery("#ap").text().length > 10, "Check text is not removed" ); equal( jQuery("#ap").children().length, 0, "Check remove" ); - equal( first.data("foo"), method === "remove" ? null : "bar", "first data" ); + equal( first.data("foo"), null, "first data" ); - QUnit.reset(); - jQuery("#ap").children()[ method ]("a"); +}); + +test( "remove() with filters", function() { + + expect( 8 ); + + var markup, div; + jQuery("#ap").children().remove("a"); ok( jQuery("#ap").text().length > 10, "Check text is not removed" ); equal( jQuery("#ap").children().length, 1, "Check filtered remove" ); - jQuery("#ap").children()[ method ]("a, code"); + jQuery("#ap").children().remove("a, code"); equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" ); // Positional and relative selectors markup = "
1234
"; div = jQuery( markup ); div.children().remove("span:nth-child(2n)"); - equal( div.text(), "13", "relative selector in " + method ); + equal( div.text(), "13", "relative selector in remove" ); div = jQuery( markup ); div.children().remove("span:first"); - equal( div.text(), "234", "positional selector in " + method ); + equal( div.text(), "234", "positional selector in remove" ); div = jQuery( markup ); div.children().remove("span:last"); - equal( div.text(), "123", "positional selector in " + method ); + equal( div.text(), "123", "positional selector in remove" ); // using contents will get comments regular, text, and comment nodes // Handle the case where no comment is in the document ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" ); - jQuery("#nonnodes").contents()[ method ](); + jQuery("#nonnodes").contents().remove(); equal( jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works" ); - - // manually clean up detached elements - if (method === "detach") { - first.remove(); - } -} - -test( "remove()", 11, function() { - testRemove("remove"); }); test( "remove() event cleaning ", 1, function() { @@ -1671,8 +1923,52 @@ test( "remove() in document order #13779", 1, function() { jQuery.cleanData = cleanData; }); -test( "detach()", 11, function() { - testRemove("detach"); +test("detach() no filters", function () { + + expect(3); + + var first = jQuery("#ap").children().first(); + + first.data("foo", "bar"); + + jQuery("#ap").children().detach(); + ok(jQuery("#ap").text().length > 10, "Check text is not removed"); + equal(jQuery("#ap").children().length, 0, "Check remove"); + + equal(first.data("foo"), "bar"); + first.remove(); + +}); + +test("detach() with filters", function () { + + expect(8); + + var markup, div; + jQuery("#ap").children().detach("a"); + ok(jQuery("#ap").text().length > 10, "Check text is not removed"); + equal(jQuery("#ap").children().length, 1, "Check filtered remove"); + + jQuery("#ap").children().detach("a, code"); + equal(jQuery("#ap").children().length, 0, "Check multi-filtered remove"); + + // Positional and relative selectors + markup = "
1234
"; + div = jQuery(markup); + div.children().detach("span:nth-child(2n)"); + equal(div.text(), "13", "relative selector in detach"); + div = jQuery(markup); + div.children().detach("span:first"); + equal(div.text(), "234", "positional selector in detach"); + div = jQuery(markup); + div.children().detach("span:last"); + equal(div.text(), "123", "positional selector in detach"); + + // using contents will get comments regular, text, and comment nodes + // Handle the case where no comment is in the document + ok(jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works"); + jQuery("#nonnodes").contents().detach(); + equal(jQuery("#nonnodes").contents().length, 0, "Check node,textnode,comment remove works"); }); test( "detach() event cleaning ", 1, function() { diff --git a/test/unit/wrap.js b/test/unit/wrap.js index c4c469fd9..3fbef44be 100644 --- a/test/unit/wrap.js +++ b/test/unit/wrap.js @@ -31,12 +31,10 @@ function testWrap( val ) { equal( defaultText, result, "Check for wrapping of on-the-fly html" ); ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); - QUnit.reset(); result = jQuery("#first").wrap( val(document.getElementById("empty")) ).parent(); ok( result.is("ol"), "Check for element wrapping" ); equal( result.text(), defaultText, "Check for element wrapping" ); - QUnit.reset(); jQuery("#check1").on( "click", function() { var checkbox = this; @@ -97,8 +95,6 @@ function testWrap( val ) { j.parent().trigger("click"); - // clean up attached elements - QUnit.reset(); } test( "wrap(String|Element)", function() { @@ -136,15 +132,15 @@ test( "wrap(String) consecutive elements (#10177)", function() { }); }); -function testWrapAll( val ) { +test( "wrapAll(String)", function() { - expect( 8 ); + expect( 5 ); var prev, p, result; prev = jQuery("#firstp")[ 0 ].previousSibling; p = jQuery("#firstp,#first")[ 0 ].parentNode; - result = jQuery("#firstp,#first").wrapAll( val("
") ); + result = jQuery("#firstp,#first").wrapAll( "
" ); equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" ); ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); @@ -152,25 +148,67 @@ function testWrapAll( val ) { equal( jQuery("#first").parent().parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); equal( jQuery("#first").parent().parent()[ 0 ].parentNode, p, "Correct Parent" ); - QUnit.reset(); +}); + +test( "wrapAll(Element)", function() { + + expect( 3 ); + + var prev, p; + prev = jQuery("#firstp")[ 0 ].previousSibling; p = jQuery("#first")[ 0 ].parentNode; - jQuery("#firstp,#first").wrapAll( val(document.getElementById("empty")) ); + jQuery("#firstp,#first").wrapAll( document.getElementById("empty") ); equal( jQuery("#first").parent()[ 0 ], jQuery("#firstp").parent()[ 0 ], "Same Parent" ); equal( jQuery("#first").parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); equal( jQuery("#first").parent()[ 0 ].parentNode, p, "Correct Parent" ); -} - -test( "wrapAll(String|Element)", function() { - testWrapAll( manipulationBareObj ); }); -function testWrapInner( val ) { +test( "wrapInner(String)", function() { - expect( 11 ); + expect( 6 ); - var num, div; + var num; + + num = jQuery("#first").children().length; + jQuery("#first").wrapInner( "
" ); + + equal( jQuery("#first").children().length, 1, "Only one child" ); + ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); + equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); + + num = jQuery("#first").html("foo
test
test2
").children().length; + jQuery("#first").wrapInner( "
" ); + equal( jQuery("#first").children().length, 1, "Only one child" ); + ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); + equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); +}); + +test( "wrapInner(Element)", function() { + + expect( 5 ); + + var num, + div = jQuery("
"); + + num = jQuery("#first").children().length; + jQuery("#first").wrapInner( document.getElementById("empty") ); + equal( jQuery("#first").children().length, 1, "Only one child" ); + ok( jQuery("#first").children().is("#empty"), "Verify Right Element" ); + equal( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); + + div.wrapInner( "" ); + equal( div.children().length, 1, "The contents were wrapped." ); + equal( div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted." ); +}); + +test( "wrapInner(Function) returns String", function() { + + expect( 6 ); + + var num, + val = manipulationFunctionReturningObj; num = jQuery("#first").children().length; jQuery("#first").wrapInner( val("
") ); @@ -179,32 +217,30 @@ function testWrapInner( val ) { ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); - QUnit.reset(); num = jQuery("#first").html("foo
test
test2
").children().length; jQuery("#first").wrapInner( val("
") ); equal( jQuery("#first").children().length, 1, "Only one child" ); ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); +}); + +test( "wrapInner(Function) returns Element", function() { + + expect( 5 ); + + var num, + val = manipulationFunctionReturningObj, + div = jQuery("
"); - QUnit.reset(); num = jQuery("#first").children().length; jQuery("#first").wrapInner( val(document.getElementById("empty")) ); equal( jQuery("#first").children().length, 1, "Only one child" ); ok( jQuery("#first").children().is("#empty"), "Verify Right Element" ); equal( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); - div = jQuery("
"); div.wrapInner( val("") ); equal( div.children().length, 1, "The contents were wrapped." ); equal( div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted." ); -} - -test( "wrapInner(String|Element)", function() { - testWrapInner( manipulationBareObj ); -}); - -test( "wrapInner(Function)", function() { - testWrapInner( manipulationFunctionReturningObj ); }); test( "unwrap()", function() {