From 517846aad247aa3677d156aa426f71b8dcacdcf7 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Thu, 8 Nov 2012 10:05:10 -0500 Subject: [PATCH] No ticket: speedup test suite. Close gh-1020. --- test/unit/ajax.js | 9 +- test/unit/effects.js | 157 +++++++++--------- test/unit/manipulation.js | 326 +++++++++++++++++--------------------- test/unit/queue.js | 19 +-- 4 files changed, 235 insertions(+), 276 deletions(-) diff --git a/test/unit/ajax.js b/test/unit/ajax.js index 1f3654a60..86ded7f3f 100644 --- a/test/unit/ajax.js +++ b/test/unit/ajax.js @@ -1481,9 +1481,7 @@ jQuery.getScript( url("data/test.js"), function( data, _, jqXHR ) { equal( foobar, "bar", "Check if script was evaluated" ); strictEqual( data, jqXHR.responseText, "Same-domain script requests returns the source of the script (#8082)" ); - setTimeout(function() { - start(); - }, 1000 ); + start(); }); }); @@ -2221,7 +2219,7 @@ test( "jQuery.ajaxSetup({timeout: Number}) - with global timeout", function() { var passed = 0; - expect( 1 ); + expect( 2 ); stop(); @@ -2230,9 +2228,8 @@ }); var pass = function() { - passed++; + ok( passed++ < 2, "Error callback executed" ); if ( passed == 2 ) { - ok( true, "Check local and global callbacks after timeout" ); jQuery("#qunit-fixture").unbind("ajaxError"); start(); } diff --git a/test/unit/effects.js b/test/unit/effects.js index 22c56fe2a..9973b10b7 100644 --- a/test/unit/effects.js +++ b/test/unit/effects.js @@ -695,14 +695,13 @@ test("stop(clearQueue, gotoEnd)", function() { asyncTest( "stop( queue, ..., ... ) - Stop single queues", function() { expect( 3 ); - var foo = jQuery( "#foo" ), - saved; + var saved, + foo = jQuery("#foo").css({ width: 200, height: 200 }); - foo.css( "width", 200 ).css( "height", 200 ); foo.animate({ width: 400 },{ - duration: 1000, + duration: 500, complete: function() { equal( parseFloat( foo.css("width") ), 400, "Animation completed for standard queue" ); equal( parseFloat( foo.css("height") ), saved, "Height was not changed after the second stop"); @@ -715,7 +714,7 @@ asyncTest( "stop( queue, ..., ... ) - Stop single queues", function() { },{ duration: 1000, queue: "height" - }).dequeue( "height" ).stop( "height", false, true ); + }).dequeue("height").stop( "height", false, true ); equal( parseFloat( foo.css("height") ), 400, "Height was stopped with gotoEnd" ); @@ -745,16 +744,6 @@ test("toggle()", function() { ok( x.is(":visible"), "is visible again" ); }); -jQuery.checkOverflowDisplay = function(){ - var o = jQuery.css( this, "overflow" ); - - equal(o, "visible", "Overflow should be visible: " + o); - equal(jQuery.css( this, "display" ), "inline", "Display shouldn't be tampered with."); - - jQuery(this).remove(); - start(); -}; - test( "jQuery.fx.prototype.cur() - <1.8 Back Compat", 7, function() { var div = jQuery( "
" ).appendTo( "#qunit-fixture" ).css({ color: "#ABC", @@ -809,29 +798,31 @@ test( "jQuery.fx.prototype.cur() - <1.8 Back Compat", 7, function() { jQuery( div ).remove(); }); -test("JS Overflow and Display", function() { - expect(2); +test("Overflow and Display", function() { + expect(4); stop(); - jQuery.makeTest( "JS Overflow and Display" ) - .addClass("widewidth") - .css({ overflow: "visible", display: "inline" }) - .addClass("widewidth") - .text("Some sample text.") - .before("text before") - .after("text after") - .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay); -}); -test("CSS Overflow and Display", function() { - expect(2); - stop(); - jQuery.makeTest( "CSS Overflow and Display" ) - .addClass("overflow inline") + var + testClass = jQuery.makeTest("Overflow and Display") + .addClass("overflow inline"), + testStyle = jQuery.makeTest("Overflow and Display (inline style)") + .css({ overflow: "visible", display: "inline" }), + remaining = 2, + done = function() { + equal( jQuery.css( this, "overflow" ), "visible", "Overflow should be 'visible'" ); + equal( jQuery.css( this, "display" ), "inline", "Display should be 'inline'" ); + + if ( --remaining === 0 ) { + start(); + } + }; + + testClass.add( testStyle ) .addClass("widewidth") .text("Some sample text.") .before("text before") .after("text after") - .animate({ opacity: 0.5 }, "slow", jQuery.checkOverflowDisplay); + .animate({ opacity: 0.5 }, "slow", done ); }); jQuery.each({ @@ -1126,57 +1117,63 @@ test("jQuery.show('fast') doesn't clear radio buttons (bug #1095)", function () }); }); -jQuery.expandedEach = jQuery.each; -jQuery.expandedEach({ - "slideToggle": function( $elem ) { - return parseFloat( $elem.css("height") ); - }, - "fadeToggle": function( $elem ) { - return $elem.css("opacity"); - }, - "toggle": function( $elem ) { - return parseFloat( $elem.css("width") ); - } -}, -function( method, defProp ) { - test( method + "().stop()." + method + "()", function() { - expect( 8 ); +test( "interrupt toggle", function() { + expect( 24 ); + stop(); - var animTime = 2000; + var longDuration = 2000, + shortDuration = 500, + remaining = 0, + $elems = jQuery(".chain-test"), + finish = function() { + if ( !(--remaining) ) { + start(); + } + }; - jQuery.each([ "in", "out" ], function( i, type ) { - var $elem = jQuery( "#" + method.toLowerCase() + type ), - startVal = defProp( $elem ); + jQuery.each( { slideToggle: "height", fadeToggle: "opacity", toggle: "width" }, function( method, prop ) { + var $methodElems = $elems.filter( "[id^='" + method.toLowerCase() + "']" ).each(function() { + // Don't end test until we're done with this element + remaining++; - $elem[ method ]( animTime ); - stop(); + // Save original property value for comparison + jQuery.data( this, "startVal", jQuery( this ).css( prop ) ); - setTimeout( function() { - $elem.stop(); - - notEqual( defProp( $elem ), startVal, ".stop() is called about halfway through animation." ); - - $elem[ method ](animTime, function() { - equal( defProp( $elem ), startVal, "After doing .stop() halfway through hide, check that state has been saved for returning to original property value." ); - - // Start from hidden position to show this time - $elem.hide()[ method ]( animTime ); - setTimeout( function() { - $elem.stop(); - - notEqual( defProp( $elem ), startVal, ".stop() is called about halfway through animation." ); - - $elem[ method ](animTime, function() { - equal( defProp( $elem ), startVal, "After doing .stop() halfway through show, check that state has been saved for returning to original property value." ); - - // Tolerate olddisplay data from .hide() call - QUnit.expectJqData( this, "olddisplay" ); - start(); - }); - }, animTime / 2); - }); - }, animTime / 2); + // Expect olddisplay data from our .hide() call below + QUnit.expectJqData( this, "olddisplay" ); }); + + // Interrupt a hiding toggle + $methodElems[ method ]( longDuration ); + setTimeout(function() { + $methodElems.stop().each(function() { + notEqual( jQuery( this ).css( prop ), jQuery.data( this, "startVal" ), ".stop() before completion of hiding ." + method + "() - #" + this.id ); + }); + + // Restore + $methodElems[ method ]( shortDuration, function() { + var id = this.id, + $elem = jQuery( this ), + startVal = $elem.data("startVal"); + + $elem.removeData("startVal"); + + equal( $elem.css( prop ), startVal, "original value restored by ." + method + "() - #" + id ); + + // Interrupt a showing toggle + $elem.hide()[ method ]( longDuration ); + setTimeout(function() { + $elem.stop(); + notEqual( $elem.css( prop ), startVal, ".stop() before completion of showing ." + method + "() - #" + id ); + + // Restore + $elem[ method ]( shortDuration, function() { + equal( $elem.css( prop ), startVal, "original value restored by ." + method + "() - #" + id ); + finish(); + }); + }, shortDuration ); + }); + }, shortDuration ); }); }); @@ -1319,7 +1316,7 @@ test( "animate properties missing px w/ opacity as last (#9074)", 2, function() notEqual( l, 200, "Left is not 200 after partial animate" ); div.stop().remove(); start(); - }, 1000); + }, 500); }); test("callbacks should fire in correct order (#9100)", function() { @@ -1663,7 +1660,7 @@ asyncTest( "multiple unqueued and promise", 4, function() { }).animate({ top: 100 }, { - duration: 1500, + duration: 1000, queue: false, complete: function() { strictEqual( step++, 3, "Step 3" ); diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index ce0af136e..98e011232 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -281,85 +281,78 @@ test("unwrap()", function() { jQuery("body > span.unwrap").remove(); }); -var getWrappedElement = function() { - return jQuery("#sap"); -}; +var testAppendForObject = function(valueObj, isFragment) { + var $base, + type = isFragment ? " (DocumentFragment)" : " (Element)", + text = "This link has class=\"blog\": Simon Willison's Weblog", + el = document.getElementById("sap").cloneNode( true ), + first = document.getElementById("first"), + yahoo = document.getElementById("yahoo"); -var getWrappedDocumentFragment = function() { - var f = document.createDocumentFragment(); - - // copy contents of #sap into new fragment - var clone = jQuery("#sap")[0].cloneNode(true); - var childs = clone.childNodes; - while (clone.childNodes.length) { - f.appendChild(clone.childNodes[0]); + if ( isFragment ) { + $base = document.createDocumentFragment(); + jQuery( el ).contents().each(function() { + $base.appendChild( this ); + }); + $base = jQuery( $base ); + } else { + $base = jQuery( el ); } - clone = null; - return jQuery(f); -}; - -var testAppendForObject = function(valueObj, isFragment) { - var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:"; - var objType = " " + (isFragment ? "(DocumentFragment)" : "(Element)"); - var getObj = isFragment ? getWrappedDocumentFragment : getWrappedElement; - - var obj = getObj(); - obj.append(valueObj(document.getElementById("first"))); - equal( obj.text(), expected, "Check for appending of element" + objType); - - QUnit.reset(); - expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo"; - obj = getObj(); - obj.append(valueObj([document.getElementById("first"), document.getElementById("yahoo")])); - equal( obj.text(), expected, "Check for appending of array of elements" + objType ); - - QUnit.reset(); - expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:"; - obj = getObj(); - obj.append(valueObj(jQuery("#yahoo, #first"))); - equal( obj.text(), expected, "Check for appending of jQuery object" + objType ); - - QUnit.reset(); - obj = getObj(); - obj.append(valueObj( 5 )); - ok( obj.text().match( /5$/ ), "Check for appending a number" + objType ); - - QUnit.reset(); - expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:GoogleYahoo"; - obj = getObj(); - obj.append( valueObj( [ jQuery("#first"), jQuery("#yahoo, #google") ] ) ); - equal( obj.text(), expected, "Check for appending of array of jQuery objects" ); - - QUnit.reset(); - obj = getObj(); - obj.append(valueObj( " text with spaces " )); - ok( obj.text().match(/ text with spaces $/), "Check for appending text with spaces" + objType ); - - QUnit.reset(); - obj = getObj(); - ok( obj.append(valueObj( [] )), "Check for appending an empty array." + objType ); - ok( obj.append(valueObj( "" )), "Check for appending an empty string." + objType ); - ok( obj.append(valueObj( document.getElementsByTagName("foo") )), "Check for appending an empty nodelist." + objType ); - - QUnit.reset(); - obj = getObj(); - obj.append(valueObj( document.getElementById("form") )); - equal( obj.children("form").size(), 1, "Check for appending a form" + objType ); // Bug #910 - - QUnit.reset(); - obj = getObj(); - - var prev = obj.children().length; - - obj.append( - "", - "", - "" + equal( $base.clone().append( valueObj( first.cloneNode(true) ) ).text(), + text + "Try them out:", + "Check for appending of element" + type ); - equal( obj.children().length, prev + 3, "Make sure that multiple arguments works." + objType ); - QUnit.reset(); + equal( $base.clone().append( valueObj([ first.cloneNode(true), yahoo.cloneNode(true) ]) ).text(), + text + "Try them out:Yahoo", + "Check for appending of array of elements" + type + ); + + equal( $base.clone().append( valueObj( jQuery("#yahoo, #first").clone() ) ).text(), + text + "YahooTry them out:", + "Check for appending of jQuery object" + type + ); + + equal( $base.clone().append( valueObj(5) ).text(), + text + "5", + "Check for appending a number" + type + ); + + equal( $base.clone().append( valueObj( [ jQuery("#first").clone(), jQuery("#yahoo, #google").clone() ] ) ).text(), + text + "Try them out:GoogleYahoo", + "Check for appending of array of jQuery objects" + ); + + equal( $base.clone().append( valueObj(" text with spaces ") ).text(), + text + " text with spaces ", + "Check for appending text with spaces" + type + ); + + equal( $base.clone().append( valueObj([]) ).text(), + text, + "Check for appending an empty array" + type + ); + + equal( $base.clone().append( valueObj("") ).text(), + text, + "Check for appending an empty string" + type + ); + + equal( $base.clone().append( valueObj( document.getElementsByTagName("foo") ) ).text(), + text, + "Check for appending an empty nodelist" + type + ); + + equal( $base.clone().append( "", "", "").children().length, + $base.children().length + 3, + "Make sure that multiple arguments works." + type + ); + + equal( $base.clone().append( valueObj( document.getElementById("form").cloneNode(true) ) ).children("form").length, + 1, + "Check for appending a form (#910)" + type + ); }; var testAppend = function(valueObj) { @@ -372,65 +365,57 @@ var testAppend = function(valueObj) { equal( result.text(), defaultText + "buga", "Check if text appending works" ); equal( jQuery("#select3").append(valueObj("")).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element"); - QUnit.reset(); jQuery("form").append(valueObj("")); jQuery("form input[name=radiotest]").each(function(){ ok( jQuery(this).is(":checked"), "Append checked radio"); }).remove(); - QUnit.reset(); - jQuery("form").append(valueObj("")); - jQuery("form input[name=radiotest]").each(function(){ + jQuery("form").append(valueObj("")); + jQuery("form input[name=radiotest2]").each(function(){ ok( jQuery(this).is(":checked"), "Append alternately formated checked radio"); }).remove(); - QUnit.reset(); - jQuery("form").append(valueObj("")); - jQuery("form input[name=radiotest]").each(function(){ + jQuery("form").append(valueObj("")); + jQuery("form input[name=radiotest3]").each(function(){ ok( jQuery(this).is(":checked"), "Append HTML5-formated checked radio"); }).remove(); - QUnit.reset(); - jQuery("form").append(valueObj("")); - jQuery("form input[name=radiotest]").each(function(){ + jQuery("form").append(valueObj("")); + jQuery("form input[name=radiotest4]").each(function(){ ok( jQuery(this).is(":checked"), "Append with name attribute after checked attribute"); }).remove(); - QUnit.reset(); - var pass = true; + var message = "Test for appending a DOM node to the contents of an iframe", + iframe = jQuery("#iframe")[0], + iframeDoc = iframe.contentDocument || iframe.contentWindow && iframe.contentWindow.document; try { - var body = jQuery("#iframe")[0].contentWindow.document.body; + if ( iframeDoc && iframeDoc.body ) { + equal( jQuery( iframeDoc.body ).append( valueObj("
test
") )[0].lastChild.id, "success", message ); + } else { + ok( true, message + " - can't test" ); + } + } catch(e) { + strictEqual( e.message || e, undefined, message ); + } - pass = false; - jQuery( body ).append(valueObj( "
test
" )); - pass = true; - } catch(e) {} - - ok( pass, "Test for appending a DOM node to the contents of an IFrame" ); - - QUnit.reset(); - jQuery("
").appendTo("#form").append(valueObj( "test" )); + jQuery("
").appendTo("#form").append( valueObj("test") ); t( "Append legend", "#legend", ["legend"] ); - QUnit.reset(); - jQuery("#select1").append(valueObj( "" )); - equal( jQuery("#select1 option:last").text(), "Test", "Appending <OPTION> (all caps)" ); + jQuery("#select1").append( valueObj("") ); + equal( jQuery("#select1 option:last").text(), "Test", "Appending OPTION (all caps)" ); - jQuery("#table").append(valueObj( "" )); - ok( jQuery("#table colgroup").length, "Append colgroup" ); + jQuery("#table").append( valueObj("") ); + equal( jQuery("#table colgroup").length, 1, "Append colgroup" ); - jQuery("#table colgroup").append(valueObj( "" )); - ok( jQuery("#table colgroup col").length, "Append col" ); + jQuery("#table colgroup").append( valueObj("") ); + equal( jQuery("#table colgroup col").length, 1, "Append col" ); - QUnit.reset(); - jQuery("#table").append(valueObj( "" )); - ok( jQuery("#table caption").length, "Append caption" ); - - QUnit.reset(); - jQuery("form:last") - .append(valueObj( "" )) - .append(valueObj( "" )); + jQuery("#table").append( valueObj("") ); + equal( jQuery("#table caption").length, 1, "Append caption" ); + jQuery("#form") + .append( valueObj("") ) + .append( valueObj("") ); t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] ); equal( "Two nodes", jQuery("
").append("Two", " nodes").text(), "Appending two text nodes (#4011)" ); @@ -439,24 +424,22 @@ var testAppend = function(valueObj) { var j = jQuery("#nonnodes").contents(); var d = jQuery("
").appendTo("#nonnodes").append(j); equal( jQuery("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" ); - ok( d.contents().length >= 2, "Check node,textnode,comment append works" ); + equal( d.contents().length, 3, "Check node,textnode,comment append works" ); d.contents().appendTo("#nonnodes"); d.remove(); - ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" ); + equal( jQuery("#nonnodes").contents().length, 3, "Check node,textnode,comment append cleanup worked" ); - QUnit.reset(); var $input = jQuery("").attr({ "type": "checkbox", "checked": true }).appendTo("#testForm"); equal( $input[0].checked, true, "A checked checkbox that is appended stays checked" ); - QUnit.reset(); - var $radios = jQuery("input:radio[name='R1']"), - $radioNot = jQuery("").insertAfter( $radios ), - $radio = $radios.eq(1).click(); - $radioNot[0].checked = false; - $radios.parent().wrap("
"); - equal( $radio[0].checked, true, "Reappending radios uphold which radio is checked" ); - equal( $radioNot[0].checked, false, "Reappending radios uphold not being checked" ); - QUnit.reset(); + var $radioChecked = jQuery("input:radio[name='R1']").eq(1), + $radioParent = $radioChecked.parent(), + $radioUnchecked = jQuery("").appendTo( $radioParent ); + $radioChecked.click(); + $radioUnchecked[0].checked = false; + $radioParent.wrap("
"); + equal( $radioChecked[0].checked, true, "Reappending radios uphold which radio is checked" ); + equal( $radioUnchecked[0].checked, false, "Reappending radios uphold not being checked" ); }; test("append(String|Element|Array|jQuery)", function() { @@ -1272,49 +1255,36 @@ test("clone() (#8070)", function () { }); test("clone()", function() { - expect( 44 ); + expect( 45 ); + var div, clone; - equal( "This is a normal link: Yahoo", jQuery("#en").text(), "Assert text for #en" ); - var clone = jQuery("#yahoo").clone(); - equal( "Try them out:Yahoo", jQuery("#first").append(clone).text(), "Check for clone" ); - equal( "This is a normal link: Yahoo", jQuery("#en").text(), "Reassert text for #en" ); + equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Assert text for #en" ); + equal( jQuery("#first").append( jQuery("#yahoo").clone() ).text(), "Try them out:Yahoo", "Check for clone" ); + equal( jQuery("#en").text(), "This is a normal link: Yahoo", "Reassert text for #en" ); - var cloneTags = [ - "", "", "
", "
", - "