Tests: Avoid use of QUnit.reset() in tests by splitting them

Closes gh-1470
Ref #1457
This commit is contained in:
cjqed 2013-12-24 11:35:41 -05:00 committed by Oleg
parent 3140d3bdd3
commit 305eefe6ab
7 changed files with 564 additions and 226 deletions

View File

@ -13,7 +13,7 @@ var oldStart = window.start,
expectedDataKeys = {}, expectedDataKeys = {},
splice = [].splice, splice = [].splice,
reset = QUnit.reset, reset,
ajaxSettings = jQuery.ajaxSettings; ajaxSettings = jQuery.ajaxSettings;
@ -157,8 +157,7 @@ window.moduleTeardown = function() {
oldActive = jQuery.active; oldActive = jQuery.active;
} }
// Allow QUnit.reset to clean up any attached elements before checking for leaks reset();
QUnit.reset();
for ( i in jQuery.cache ) { for ( i in jQuery.cache ) {
++cacheLength; ++cacheLength;
@ -187,8 +186,8 @@ QUnit.done(function() {
supportjQuery("#qunit ~ *").remove(); supportjQuery("#qunit ~ *").remove();
}); });
// jQuery-specific QUnit.reset // jQuery-specific post-test cleanup
QUnit.reset = function() { reset = function() {
// Ensure jQuery events and data on the fixture are properly removed // Ensure jQuery events and data on the fixture are properly removed
jQuery("#qunit-fixture").empty(); jQuery("#qunit-fixture").empty();
@ -206,10 +205,11 @@ QUnit.reset = function() {
// Cleanup globals // Cleanup globals
Globals.cleanup(); Globals.cleanup();
// Let QUnit reset the fixture jQuery("#qunit-fixture")[0].innerHTML = QUnit.config.fixture;
reset.apply( this, arguments );
}; };
QUnit.testDone(reset);
// Register globals for cleanup and the cleanup code itself // Register globals for cleanup and the cleanup code itself
// Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs // Explanation at http://perfectionkills.com/understanding-delete/#ie_bugs
window.Globals = (function() { window.Globals = (function() {
@ -351,7 +351,7 @@ function testSubproject( label, subProjectURL, risTests, complete ) {
// WARNING: UNDOCUMENTED INTERFACE // WARNING: UNDOCUMENTED INTERFACE
QUnit.config.fixture = fixtureHTML; QUnit.config.fixture = fixtureHTML;
QUnit.reset(); reset();
if ( supportjQuery("#qunit-fixture").html() !== fixtureHTML ) { if ( supportjQuery("#qunit-fixture").html() !== fixtureHTML ) {
ok( false, "Copied subproject fixture" ); ok( false, "Copied subproject fixture" );
return; return;
@ -359,7 +359,6 @@ function testSubproject( label, subProjectURL, risTests, complete ) {
fixtureReplaced = true; fixtureReplaced = true;
} }
fn.apply( this, arguments ); fn.apply( this, arguments );
}; };
} }

View File

@ -639,7 +639,7 @@ test( "removeAttr(Multi String, variable space width)", function() {
}); });
test( "prop(String, Object)", 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"), "Test", "Check for value attribute" );
equal( jQuery("#text1").prop( "value", "Test2" ).prop("defaultValue"), "Test", "Check for defaultValue 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" ); equal( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" );
jQuery("#table").prop( "frameborder", 1 ); jQuery("#table").prop( "frameborder", 1 );
equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" ); 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, var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form,
body = document.body, body = document.body,
@ -799,16 +803,19 @@ test( "removeProp(String)", function() {
}); });
}); });
test( "val()", function() { test( "val() after modification", function() {
expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) ); expect( 1 );
var checks, $button;
document.getElementById("text1").value = "bla"; document.getElementById("text1").value = "bla";
equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" ); 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" ); equal( jQuery("#text1").val(), "Test", "Check for value of input element" );
// ticket #1714 this caused a JS error in IE // ticket #1714 this caused a JS error in IE
equal( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" ); 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 ) { var testVal = function( valueObj ) {
expect( 8 ); expect( 8 );
QUnit.reset();
jQuery("#text1").val( valueObj("test") ); jQuery("#text1").val( valueObj("test") );
equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" ); 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() { test( "val(Function) with incoming value", function() {
expect( 10 ); expect( 10 );
QUnit.reset();
var oldVal = jQuery("#text1").val(); var oldVal = jQuery("#text1").val();
jQuery("#text1").val(function( i, val ) { jQuery("#text1").val(function( i, val ) {

View File

@ -592,7 +592,6 @@ test("jQuery('html')", function() {
var s, div, j; var s, div, j;
QUnit.reset();
jQuery["foo"] = false; jQuery["foo"] = false;
s = jQuery("<script>jQuery.foo='test';</script>")[0]; s = jQuery("<script>jQuery.foo='test';</script>")[0];
ok( s, "Creating a script" ); ok( s, "Creating a script" );
@ -608,7 +607,6 @@ test("jQuery('html')", function() {
equal( div.childNodes[1].nodeType, 1, "Paragraph." ); equal( div.childNodes[1].nodeType, 1, "Paragraph." );
equal( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." ); equal( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
QUnit.reset();
ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" ); ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
ok( !jQuery("<script/>")[0].parentNode, "Create a script" ); ok( !jQuery("<script/>")[0].parentNode, "Create a script" );

View File

@ -431,9 +431,9 @@ test("css(Object) where values are Functions with incoming values", function() {
}); });
test("show(); hide()", function() { test("show(); hide()", function() {
expect(22); expect( 4 );
var hiddendiv, div, pass, old, test; var hiddendiv, div;
hiddendiv = jQuery("div.hidden"); hiddendiv = jQuery("div.hidden");
hiddendiv.hide(); hiddendiv.hide();
@ -446,8 +446,13 @@ test("show(); hide()", function() {
div.appendTo("#qunit-fixture").show(); div.appendTo("#qunit-fixture").show();
equal( div.css("display"), "block", "Pre-hidden div shown" ); equal( div.css("display"), "block", "Pre-hidden div shown" );
QUnit.reset(); });
test("show();", function() {
expect( 18 );
var hiddendiv, div, pass, old, test;
hiddendiv = jQuery("div.hidden"); hiddendiv = jQuery("div.hidden");
equal(jQuery.css( hiddendiv[0], "display"), "none", "hiddendiv is display: none"); equal(jQuery.css( hiddendiv[0], "display"), "none", "hiddendiv is display: none");
@ -666,7 +671,6 @@ test("toggle()", function() {
test("hide hidden elements (bug #7141)", function() { test("hide hidden elements (bug #7141)", function() {
expect(3); expect(3);
QUnit.reset();
var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture"); var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture");
equal( div.css("display"), "none", "Element is hidden by default" ); equal( div.css("display"), "none", "Element is hidden by default" );

View File

@ -124,7 +124,7 @@ test("show()", 27, function () {
test("show(Number) - other displays", function() { test("show(Number) - other displays", function() {
expect(15); expect(15);
QUnit.reset();
stop(); stop();
// #show-tests * is set display: none in CSS // #show-tests * is set display: none in CSS
@ -171,7 +171,7 @@ test("show(Number) - other displays", function() {
// Supports #7397 // Supports #7397
test("Persist correct display value", function() { test("Persist correct display value", function() {
expect(3); expect(3);
QUnit.reset();
stop(); stop();
// #show-tests * is set display: none in CSS // #show-tests * is set display: none in CSS
@ -1310,7 +1310,7 @@ test("animate with CSS shorthand properties", function(){
test("hide hidden elements, with animation (bug #7141)", function() { test("hide hidden elements, with animation (bug #7141)", function() {
expect(3); expect(3);
QUnit.reset();
stop(); stop();
var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture"); var div = jQuery("<div style='display:none'></div>").appendTo("#qunit-fixture");

View File

@ -331,11 +331,11 @@ test( "append(param) to object, see #11280", function() {
equal( object.children().eq(0).attr("name"), "bar", "param has name=bar" ); equal( object.children().eq(0).attr("name"), "bar", "param has name=bar" );
}); });
test( "append(Function) with incoming value", function() { test( "append(Function) returns String", function() {
expect( 12 ); expect( 4 );
var defaultText, result, select, old, expected; var defaultText, result, select, old;
defaultText = "Try them out:"; defaultText = "Try them out:";
old = jQuery("#first").html(); old = jQuery("#first").html();
@ -353,9 +353,12 @@ test( "append(Function) with incoming value", function() {
equal( val, old, "Make sure the incoming value is correct." ); equal( val, old, "Make sure the incoming value is correct." );
return "<option value='appendTest'>Append Test</option>"; return "<option value='appendTest'>Append Test</option>";
}).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" ); }).find("option:last-child").attr("value"), "appendTest", "Appending html options to select element" );
});
QUnit.reset(); test( "append(Function) returns Element", function() {
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:",
old = jQuery("#sap").html(); old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) { jQuery("#sap").append(function( i, val ) {
@ -363,9 +366,12 @@ test( "append(Function) with incoming value", function() {
return document.getElementById("first"); return document.getElementById("first");
}); });
equal( jQuery("#sap").text(), expected, "Check for appending of element" ); equal( jQuery("#sap").text(), expected, "Check for appending of element" );
});
QUnit.reset(); test( "append(Function) returns Array<Element>", function() {
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo",
old = jQuery("#sap").html(); old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) { jQuery("#sap").append(function( i, val ) {
@ -373,9 +379,12 @@ test( "append(Function) with incoming value", function() {
return [ document.getElementById("first"), document.getElementById("yahoo") ]; return [ document.getElementById("first"), document.getElementById("yahoo") ];
}); });
equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" ); equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
});
QUnit.reset(); test( "append(Function) returns jQuery", function() {
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:",
old = jQuery("#sap").html(); old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) { jQuery("#sap").append(function( i, val ) {
@ -383,17 +392,18 @@ test( "append(Function) with incoming value", function() {
return jQuery("#yahoo, #first"); return jQuery("#yahoo, #first");
}); });
equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" ); equal( jQuery("#sap").text(), expected, "Check for appending of jQuery object" );
});
QUnit.reset(); test( "append(Function) returns Number", function() {
old = jQuery("#sap").html();
expect( 2 );
var old = jQuery("#sap").html();
jQuery("#sap").append(function( i, val ) { jQuery("#sap").append(function( i, val ) {
equal( val, old, "Make sure the incoming value is correct." ); equal( val, old, "Make sure the incoming value is correct." );
return 5; return 5;
}); });
ok( jQuery("#sap")[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" ); ok( jQuery("#sap")[ 0 ].innerHTML.match( /5$/ ), "Check for appending a number" );
QUnit.reset();
}); });
test( "XML DOM manipulation (#9960)", function() { test( "XML DOM manipulation (#9960)", function() {
@ -562,11 +572,11 @@ test( "append(xml)", function() {
}); });
test( "appendTo(String|Element|Array<Element>|jQuery)", function() { test( "appendTo(String)", function() {
expect( 16 ); expect( 4 );
var defaultText, l, expected, num, div; var l, defaultText;
defaultText = "Try them out:"; defaultText = "Try them out:";
jQuery("<b>buga</b>").appendTo("#first"); jQuery("<b>buga</b>").appendTo("#first");
@ -580,9 +590,13 @@ test( "appendTo(String|Element|Array<Element>|jQuery)", function() {
.appendTo("#first"); .appendTo("#first");
equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." ); equal( jQuery("#first").children().length, l, "Make sure the elements were inserted." );
equal( jQuery("#first").children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." ); equal( jQuery("#first").children().last()[ 0 ].nodeName.toLowerCase(), "strong", "Verify the last element." );
});
QUnit.reset(); test( "appendTo(Element|Array<Element>)", function() {
expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
expect( 2 );
var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
jQuery( document.getElementById("first") ).appendTo("#sap"); jQuery( document.getElementById("first") ).appendTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for appending of element" ); equal( jQuery("#sap").text(), expected, "Check for appending of element" );
@ -590,7 +604,13 @@ test( "appendTo(String|Element|Array<Element>|jQuery)", function() {
jQuery([ document.getElementById("first"), document.getElementById("yahoo") ]).appendTo("#sap"); jQuery([ document.getElementById("first"), document.getElementById("yahoo") ]).appendTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" ); equal( jQuery("#sap").text(), expected, "Check for appending of array of elements" );
QUnit.reset(); });
test( "appendTo(jQuery)", function() {
expect( 10 );
var expected, num, div;
ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." ); ok( jQuery(document.createElement("script")).appendTo("body").length, "Make sure a disconnected script can be appended." );
expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:"; expected = "This link has class=\"blog\": Simon Willison's WeblogYahooTry them out:";
@ -630,52 +650,62 @@ test( "appendTo(String|Element|Array<Element>|jQuery)", function() {
equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." ); equal( jQuery("#qunit-fixture div").length, num, "Make sure all the removed divs were inserted." );
}); });
function testPrepend( val ) { test( "prepend(String)", function() {
expect( 6 ); expect( 2 );
var defaultText, result, expected; var result, expected;
expected = "Try them out:";
result = jQuery("#first").prepend( "<b>buga</b>" );
equal( result.text(), "buga" + expected, "Check if text prepending works" );
equal( jQuery("#select3").prepend( "<option value='prependTest'>Prepend Test</option>" ).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
});
defaultText = "Try them out:"; test( "prepend(Element)", function() {
result = jQuery("#first").prepend( val("<b>buga</b>") );
equal( result.text(), "buga" + defaultText, "Check if text prepending works" ); expect( 1 );
equal( jQuery("#select3").prepend( val("<option value='prependTest'>Prepend Test</option>" ) ).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
QUnit.reset(); var expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( val(document.getElementById("first")) ); jQuery("#sap").prepend( document.getElementById("first") );
equal( jQuery("#sap").text(), expected, "Check for prepending of element" ); equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
QUnit.reset(); test( "prepend(Array<Element>)", function() {
expect( 1 );
var expected;
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( val([ document.getElementById("first"), document.getElementById("yahoo") ]) ); jQuery("#sap").prepend( [ document.getElementById("first"), document.getElementById("yahoo") ] );
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" ); equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
QUnit.reset(); test( "prepend(jQuery)", function() {
expect( 1 );
var expected;
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( val(jQuery("#yahoo, #first")) ); jQuery("#sap").prepend( jQuery("#yahoo, #first") );
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" ); equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
});
QUnit.reset(); test( "prepend(Array<jQuery>)", function() {
expect( 1 );
var expected;
expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:GoogleYahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery("#sap").prepend( val([ jQuery("#first"), jQuery("#yahoo, #google") ]) ); jQuery("#sap").prepend( [ jQuery("#first"), jQuery("#yahoo, #google") ] );
equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" ); equal( jQuery("#sap").text(), expected, "Check for prepending of array of jQuery objects" );
}
test( "prepend(String|Element|Array<Element>|jQuery)", function() {
testPrepend( manipulationBareObj );
}); });
test( "prepend(Function)", function() { test( "prepend(Function) with incoming value -- String", function() {
testPrepend( manipulationFunctionReturningObj );
});
test( "prepend(Function) with incoming value", function() { expect( 4 );
expect( 10 ); var defaultText, old, result;
var defaultText, old, result, expected;
defaultText = "Try them out:"; defaultText = "Try them out:";
old = jQuery("#first").html(); old = jQuery("#first").html();
@ -692,8 +722,13 @@ test( "prepend(Function) with incoming value", function() {
equal( val, old, "Make sure the incoming value is correct." ); equal( val, old, "Make sure the incoming value is correct." );
return "<option value='prependTest'>Prepend Test</option>"; return "<option value='prependTest'>Prepend Test</option>";
}).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" ); }).find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
});
QUnit.reset(); test( "prepend(Function) with incoming value -- Element", function() {
expect( 2 );
var old, expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html(); old = jQuery("#sap").html();
@ -703,8 +738,13 @@ test( "prepend(Function) with incoming value", function() {
}); });
equal( jQuery("#sap").text(), expected, "Check for prepending of element" ); equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
QUnit.reset(); test( "prepend(Function) with incoming value -- Array<Element>", function() {
expect( 2 );
var old, expected;
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html(); old = jQuery("#sap").html();
@ -714,8 +754,13 @@ test( "prepend(Function) with incoming value", function() {
}); });
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" ); equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
QUnit.reset(); test( "prepend(Function) with incoming value -- jQuery", function() {
expect( 2 );
var old, expected;
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
old = jQuery("#sap").html(); old = jQuery("#sap").html();
@ -727,80 +772,174 @@ test( "prepend(Function) with incoming value", function() {
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" ); equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
}); });
test( "prependTo(String|Element|Array<Element>|jQuery)", function() { test( "prependTo(String)", function() {
expect( 6 ); expect( 2 );
var defaultText, expected; var defaultText;
defaultText = "Try them out:"; defaultText = "Try them out:";
jQuery("<b>buga</b>").prependTo("#first"); jQuery("<b>buga</b>").prependTo("#first");
equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" ); equal( jQuery("#first").text(), "buga" + defaultText, "Check if text prepending works" );
equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" ); equal( jQuery("<option value='prependTest'>Prepend Test</option>").prependTo("#select3").parent().find("option:first-child").attr("value"), "prependTest", "Prepending html options to select element" );
QUnit.reset(); });
test( "prependTo(Element)", function() {
expect( 1 );
var expected;
expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery( document.getElementById("first") ).prependTo("#sap"); jQuery( document.getElementById("first") ).prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of element" ); equal( jQuery("#sap").text(), expected, "Check for prepending of element" );
});
test( "prependTo(Array<Element>)", function() {
expect( 1 );
var expected;
QUnit.reset();
expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog"; expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
jQuery( [ document.getElementById("first"), document.getElementById("yahoo") ] ).prependTo("#sap"); jQuery( [ document.getElementById("first"), document.getElementById("yahoo") ] ).prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" ); equal( jQuery("#sap").text(), expected, "Check for prepending of array of elements" );
});
test( "prependTo(jQuery)", function() {
expect( 1 );
var expected;
QUnit.reset();
expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog"; expected = "YahooTry them out:This link has class=\"blog\": Simon Willison's Weblog";
jQuery("#yahoo, #first").prependTo("#sap"); jQuery("#yahoo, #first").prependTo("#sap");
equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" ); equal( jQuery("#sap").text(), expected, "Check for prepending of jQuery object" );
});
test( "prependTo(Array<jQuery>)", function() {
expect( 1 );
QUnit.reset();
jQuery("<select id='prependSelect1'></select>").prependTo("#form"); jQuery("<select id='prependSelect1'></select>").prependTo("#form");
jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("#form"); jQuery("<select id='prependSelect2'><option>Test</option></select>").prependTo("#form");
t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] ); t( "Prepend Select", "#prependSelect2, #prependSelect1", [ "prependSelect2", "prependSelect1" ] );
}); });
function testBefore( val ) { test( "before(String)", function() {
expect( 7 ); expect( 1 );
var expected, set; var expected;
expected = "This is a normal link: bugaYahoo"; expected = "This is a normal link: bugaYahoo";
jQuery("#yahoo").before( val("<b>buga</b>") ); jQuery("#yahoo").before( manipulationBareObj("<b>buga</b>") );
equal( jQuery("#en").text(), expected, "Insert String before" ); equal( jQuery("#en").text(), expected, "Insert String before" );
});
test( "before(Element)", function() {
expect( 1 );
var expected;
QUnit.reset();
expected = "This is a normal link: Try them out:Yahoo"; expected = "This is a normal link: Try them out:Yahoo";
jQuery("#yahoo").before( val(document.getElementById("first")) ); jQuery("#yahoo").before( manipulationBareObj(document.getElementById("first")) );
equal( jQuery("#en").text(), expected, "Insert element before" ); equal( jQuery("#en").text(), expected, "Insert element before" );
});
QUnit.reset(); test( "before(Array<Element>)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:diveintomarkYahoo"; expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery("#yahoo").before( val([ document.getElementById("first"), document.getElementById("mark") ]) ); jQuery("#yahoo").before( manipulationBareObj([ document.getElementById("first"), document.getElementById("mark") ]) );
equal( jQuery("#en").text(), expected, "Insert array of elements before" ); equal( jQuery("#en").text(), expected, "Insert array of elements before" );
});
QUnit.reset(); test( "before(jQuery)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: diveintomarkTry them out:Yahoo"; expected = "This is a normal link: diveintomarkTry them out:Yahoo";
jQuery("#yahoo").before( val(jQuery("#mark, #first")) ); jQuery("#yahoo").before( manipulationBareObj(jQuery("#mark, #first")) );
equal( jQuery("#en").text(), expected, "Insert jQuery before" ); equal( jQuery("#en").text(), expected, "Insert jQuery before" );
});
QUnit.reset(); test( "before(Array<jQuery>)", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:GooglediveintomarkYahoo"; expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
jQuery("#yahoo").before( val([ jQuery("#first"), jQuery("#mark, #google") ]) ); jQuery("#yahoo").before( manipulationBareObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" ); equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
});
test( "before(Function) -- Returns String", function() {
expect( 1 );
var expected;
expected = "This is a normal link: bugaYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj("<b>buga</b>") );
equal( jQuery("#en").text(), expected, "Insert String before" );
});
test( "before(Function) -- Returns Element", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:Yahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj(document.getElementById("first")) );
equal( jQuery("#en").text(), expected, "Insert element before" );
});
test( "before(Function) -- Returns Array<Element>", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj([ document.getElementById("first"), document.getElementById("mark") ]) );
equal( jQuery("#en").text(), expected, "Insert array of elements before" );
});
test( "before(Function) -- Returns jQuery", function() {
expect( 1 );
var expected;
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj(jQuery("#mark, #first")) );
equal( jQuery("#en").text(), expected, "Insert jQuery before" );
});
test( "before(Function) -- Returns Array<jQuery>", function() {
expect( 1 );
var expected;
expected = "This is a normal link: Try them out:GooglediveintomarkYahoo";
jQuery("#yahoo").before( manipulationFunctionReturningObj([ jQuery("#first"), jQuery("#mark, #google") ]) );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects before" );
});
test( "before(no-op)", function() {
expect( 2 );
var set;
set = jQuery("<div/>").before("<span>test</span>"); set = jQuery("<div/>").before("<span>test</span>");
equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" ); equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert before a disconnected node should be a no-op" );
equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" ); equal( set.length, 1, "Insert the element before the disconnected node. should be a no-op" );
}
test( "before(String|Element|Array<Element>|jQuery)", function() {
testBefore( manipulationBareObj );
});
test( "before(Function)", function() {
testBefore( manipulationFunctionReturningObj );
}); });
test( "before and after w/ empty object (#10812)", function() { test( "before and after w/ empty object (#10812)", function() {
@ -813,124 +952,207 @@ test( "before and after w/ empty object (#10812)", function() {
equal( res.length, 0, "didn't choke on empty object" ); equal( res.length, 0, "didn't choke on empty object" );
}); });
test( "before and after on disconnected node (#10517)", function() { test( ".before() and .after() disconnected node", function() {
expect( 6 ); expect(2);
var expectedBefore = "This is a normal link: bugaYahoo",
expectedAfter = "This is a normal link: Yahoobuga";
equal( jQuery("<input type='checkbox'/>").before("<div/>").length, 1, "before() on disconnected node is no-op" ); equal( jQuery("<input type='checkbox'/>").before("<div/>").length, 1, "before() on disconnected node is no-op" );
equal( jQuery("<input type='checkbox'/>").after("<div/>").length, 1, "after() on disconnected node is no-op" ); equal( jQuery("<input type='checkbox'/>").after("<div/>").length, 1, "after() on disconnected node is no-op" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedBefore = "This is a normal link: bugaYahoo";
QUnit.reset();
jQuery("#yahoo").add("<span/>").before("<b>buga</b>"); jQuery("#yahoo").add("<span/>").before("<b>buga</b>");
equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node last" ); equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node last" );
});
test( "insert with .before() on disconnected node first", function() {
expect(1);
var expectedBefore = "This is a normal link: bugaYahoo";
QUnit.reset();
jQuery("<span/>").add("#yahoo").before("<b>buga</b>"); jQuery("<span/>").add("#yahoo").before("<b>buga</b>");
equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node first" ); equal( jQuery("#en").text(), expectedBefore, "Insert String before with disconnected node first" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedAfter = "This is a normal link: Yahoobuga";
QUnit.reset();
jQuery("#yahoo").add("<span/>").after("<b>buga</b>"); jQuery("#yahoo").add("<span/>").after("<b>buga</b>");
equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node last" ); equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node last" );
});
test( "insert with .before() on disconnected node last", function() {
expect(1);
var expectedAfter = "This is a normal link: Yahoobuga";
QUnit.reset();
jQuery("<span/>").add("#yahoo").after("<b>buga</b>"); jQuery("<span/>").add("#yahoo").after("<b>buga</b>");
equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node first" ); equal( jQuery("#en").text(), expectedAfter, "Insert String after with disconnected node first" );
}); });
test( "insertBefore(String|Element|Array<Element>|jQuery)", function() { test( "insertBefore(String)", function() {
expect( 4 ); expect( 1 );
var expected; var expected = "This is a normal link: bugaYahoo";
expected = "This is a normal link: bugaYahoo";
jQuery("<b>buga</b>").insertBefore("#yahoo"); jQuery("<b>buga</b>").insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert String before" ); equal( jQuery("#en").text(), expected, "Insert String before" );
});
QUnit.reset(); test( "insertBefore(Element)", function() {
expected = "This is a normal link: Try them out:Yahoo";
expect( 1 );
var expected = "This is a normal link: Try them out:Yahoo";
jQuery( document.getElementById("first") ).insertBefore("#yahoo"); jQuery( document.getElementById("first") ).insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert element before" ); equal( jQuery("#en").text(), expected, "Insert element before" );
});
QUnit.reset(); test( "insertBefore(Array<Element>)", function() {
expected = "This is a normal link: Try them out:diveintomarkYahoo";
expect( 1 );
var expected = "This is a normal link: Try them out:diveintomarkYahoo";
jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertBefore("#yahoo"); jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert array of elements before" ); equal( jQuery("#en").text(), expected, "Insert array of elements before" );
});
QUnit.reset(); test( "insertBefore(jQuery)", function() {
expected = "This is a normal link: diveintomarkTry them out:Yahoo";
expect( 1 );
var expected = "This is a normal link: diveintomarkTry them out:Yahoo";
jQuery("#mark, #first").insertBefore("#yahoo"); jQuery("#mark, #first").insertBefore("#yahoo");
equal( jQuery("#en").text(), expected, "Insert jQuery before" ); equal( jQuery("#en").text(), expected, "Insert jQuery before" );
}); });
function testAfter( val ) { test( ".after(String)", function() {
expect( 7 ); expect( 1 );
var set, expected; var expected = "This is a normal link: Yahoobuga";
jQuery("#yahoo").after( "<b>buga</b>" );
equal( jQuery("#en").text(), expected, "Insert String after" );
});
expected = "This is a normal link: Yahoobuga"; test( ".after(Element)", function() {
expect( 1 );
var expected = "This is a normal link: YahooTry them out:";
jQuery("#yahoo").after( document.getElementById("first") );
equal( jQuery("#en").text(), expected, "Insert element after" );
});
test( ".after(Array<Element>)", function() {
expect( 1 );
var expected = "This is a normal link: YahooTry them out:diveintomark";
jQuery("#yahoo").after( [ document.getElementById("first"), document.getElementById("mark") ] );
equal( jQuery("#en").text(), expected, "Insert array of elements after" );
});
test( ".after(jQuery)", function() {
expect( 1 );
var expected = "This is a normal link: YahooTry them out:Googlediveintomark";
jQuery("#yahoo").after( [ jQuery("#first"), jQuery("#mark, #google") ] );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" );
});
test( ".after(Function) returns String", function() {
expect( 1 );
var expected = "This is a normal link: Yahoobuga",
val = manipulationFunctionReturningObj;
jQuery("#yahoo").after( val("<b>buga</b>") ); jQuery("#yahoo").after( val("<b>buga</b>") );
equal( jQuery("#en").text(), expected, "Insert String after" ); equal( jQuery("#en").text(), expected, "Insert String after" );
});
QUnit.reset(); test( ".after(Function) returns Element", function() {
expected = "This is a normal link: YahooTry them out:";
expect( 1 );
var expected = "This is a normal link: YahooTry them out:",
val = manipulationFunctionReturningObj;
jQuery("#yahoo").after( val(document.getElementById("first")) ); jQuery("#yahoo").after( val(document.getElementById("first")) );
equal( jQuery("#en").text(), expected, "Insert element after" ); equal( jQuery("#en").text(), expected, "Insert element after" );
});
QUnit.reset(); test( ".after(Function) returns Array<Element>", function() {
expected = "This is a normal link: YahooTry them out:diveintomark";
expect( 1 );
var expected = "This is a normal link: YahooTry them out:diveintomark",
val = manipulationFunctionReturningObj;
jQuery("#yahoo").after( val([ document.getElementById("first"), document.getElementById("mark") ]) ); jQuery("#yahoo").after( val([ document.getElementById("first"), document.getElementById("mark") ]) );
equal( jQuery("#en").text(), expected, "Insert array of elements after" ); equal( jQuery("#en").text(), expected, "Insert array of elements after" );
});
QUnit.reset(); test( ".after(Function) returns jQuery", function() {
expected = "This is a normal link: YahoodiveintomarkTry them out:";
jQuery("#yahoo").after(val( jQuery("#mark, #first") ));
equal( jQuery("#en").text(), expected, "Insert jQuery after" );
QUnit.reset(); expect( 1 );
expected = "This is a normal link: YahooTry them out:Googlediveintomark";
var expected = "This is a normal link: YahooTry them out:Googlediveintomark",
val = manipulationFunctionReturningObj;
jQuery("#yahoo").after( val([ jQuery("#first"), jQuery("#mark, #google") ]) ); jQuery("#yahoo").after( val([ jQuery("#first"), jQuery("#mark, #google") ]) );
equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" ); equal( jQuery("#en").text(), expected, "Insert array of jQuery objects after" );
});
set = jQuery("<div/>").before("<span>test</span>"); test( ".after(disconnected node)", function() {
expect( 2 );
var set = jQuery("<div/>").before("<span>test</span>");
equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" ); equal( set[ 0 ].nodeName.toLowerCase(), "div", "Insert after a disconnected node should be a no-op" );
equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" ); equal( set.length, 1, "Insert the element after the disconnected node should be a no-op" );
}
test( "after(String|Element|Array<Element>|jQuery)", function() {
testAfter( manipulationBareObj );
}); });
test( "after(Function)", function() { test( "insertAfter(String)", function() {
testAfter( manipulationFunctionReturningObj );
});
test( "insertAfter(String|Element|Array<Element>|jQuery)", function() { expect( 1 ) ;
expect( 4 ) ; var expected = "This is a normal link: Yahoobuga";
var expected;
expected = "This is a normal link: Yahoobuga";
jQuery("<b>buga</b>").insertAfter("#yahoo"); jQuery("<b>buga</b>").insertAfter("#yahoo");
equal( jQuery("#en").text(), expected, "Insert String after" ); equal( jQuery("#en").text(), expected, "Insert String after" );
});
QUnit.reset(); test( "insertAfter(Element)", function() {
expected = "This is a normal link: YahooTry them out:";
expect(1);
var expected = "This is a normal link: YahooTry them out:";
jQuery( document.getElementById("first") ).insertAfter("#yahoo"); jQuery( document.getElementById("first") ).insertAfter("#yahoo");
equal( jQuery("#en").text(), expected, "Insert element after" ); equal( jQuery("#en").text(), expected, "Insert element after" );
});
QUnit.reset(); test( "insertAfter(Array<Element>)", function() {
expected = "This is a normal link: YahooTry them out:diveintomark";
expect(1);
var expected = "This is a normal link: YahooTry them out:diveintomark";
jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertAfter("#yahoo"); jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).insertAfter("#yahoo");
equal( jQuery("#en").text(), expected, "Insert array of elements after" ); equal( jQuery("#en").text(), expected, "Insert array of elements after" );
});
QUnit.reset(); test( "insertAfter(jQuery)", function() {
expected = "This is a normal link: YahoodiveintomarkTry them out:";
expect(1);
var expected = "This is a normal link: YahoodiveintomarkTry them out:";
jQuery("#mark, #first").insertAfter("#yahoo"); jQuery("#mark, #first").insertAfter("#yahoo");
equal( jQuery("#en").text(), expected, "Insert jQuery after" ); equal( jQuery("#en").text(), expected, "Insert jQuery after" );
}); });
@ -1088,26 +1310,38 @@ test( "Empty replaceWith (#13401; #13596)", 8, function() {
}); });
}); });
test( "replaceAll(String|Element|Array<Element>|jQuery)", function() { test( "replaceAll(String)", function() {
expect( 10 ); expect( 2 );
jQuery("<b id='replace'>buga</b>").replaceAll("#yahoo"); jQuery("<b id='replace'>buga</b>").replaceAll("#yahoo");
ok( jQuery("#replace")[ 0 ], "Replace element with string" ); ok( jQuery("#replace")[ 0 ], "Replace element with string" );
ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" ); ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" );
});
test( "replaceAll(Element)", function() {
expect( 2 );
QUnit.reset();
jQuery( document.getElementById("first") ).replaceAll("#yahoo"); jQuery( document.getElementById("first") ).replaceAll("#yahoo");
ok( jQuery("#first")[ 0 ], "Replace element with element" ); ok( jQuery("#first")[ 0 ], "Replace element with element" );
ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after element" ); ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after element" );
});
test( "replaceAll(Array<Element>)", function() {
expect( 3 );
QUnit.reset();
jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).replaceAll("#yahoo"); jQuery( [ document.getElementById("first"), document.getElementById("mark") ] ).replaceAll("#yahoo");
ok( jQuery("#first")[ 0 ], "Replace element with array of elements" ); ok( jQuery("#first")[ 0 ], "Replace element with array of elements" );
ok( jQuery("#mark")[ 0 ], "Replace element with array of elements" ); ok( jQuery("#mark")[ 0 ], "Replace element with array of elements" );
ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after array of elements" ); ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after array of elements" );
});
test( "replaceAll(jQuery)", function() {
expect( 3 );
QUnit.reset();
jQuery("#mark, #first").replaceAll("#yahoo"); jQuery("#mark, #first").replaceAll("#yahoo");
ok( jQuery("#first")[ 0 ], "Replace element with set of elements" ); ok( jQuery("#first")[ 0 ], "Replace element with set of elements" );
ok( jQuery("#mark")[ 0 ], "Replace element with set of elements" ); ok( jQuery("#mark")[ 0 ], "Replace element with set of elements" );
@ -1375,7 +1609,7 @@ function childNodeNames( node ) {
} }
function testHtml( valueObj ) { function testHtml( valueObj ) {
expect( 40 ); expect( 39 );
var actual, expected, tmp, var actual, expected, tmp,
div = jQuery("<div></div>"), div = jQuery("<div></div>"),
@ -1477,10 +1711,6 @@ function testHtml( valueObj ) {
"<span id='scriptorder'><script>equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>", "<span id='scriptorder'><script>equal( jQuery.scriptorder++, 1, 'Script (nested) is executed in order');</script></span>",
"<script>equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>" "<script>equal( jQuery.scriptorder++, 2, 'Script (unnested) is executed in order' );</script>"
].join("")) ); ].join("")) );
QUnit.reset();
fixture.html( valueObj( fixture.text() ) );
ok( /^[^<]*[^<\s][^<]*$/.test( fixture.html() ), "Replace html with text" );
} }
test( "html(String|Number)", function() { test( "html(String|Number)", function() {
@ -1491,11 +1721,29 @@ test( "html(Function)", function() {
testHtml( manipulationFunctionReturningObj ); 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"); els = jQuery("#foo > p");
actualhtml = els.map(function() { actualhtml = els.map(function() {
@ -1514,9 +1762,14 @@ test( "html(Function) with incoming value", function() {
} }
}); });
ok( pass, "Set HTML" ); 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(); j = jQuery("#nonnodes").contents();
actualhtml = j.map(function() { actualhtml = j.map(function() {
return jQuery( this ).html(); 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()" ); ok( expected.test( $content.html() ), "html()" );
}); });
function testRemove( method ) { test( "remove() no filters", function() {
var markup, div,
first = jQuery("#ap").children().first(); expect( 3 );
var first = jQuery("#ap").children().first();
first.data("foo", "bar"); first.data("foo", "bar");
jQuery("#ap").children()[ method ](); jQuery("#ap").children().remove();
ok( jQuery("#ap").text().length > 10, "Check text is not removed" ); ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
equal( jQuery("#ap").children().length, 0, "Check remove" ); 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" ); ok( jQuery("#ap").text().length > 10, "Check text is not removed" );
equal( jQuery("#ap").children().length, 1, "Check filtered remove" ); 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" ); equal( jQuery("#ap").children().length, 0, "Check multi-filtered remove" );
// Positional and relative selectors // Positional and relative selectors
markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>"; markup = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
div = jQuery( markup ); div = jQuery( markup );
div.children().remove("span:nth-child(2n)"); 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 = jQuery( markup );
div.children().remove("span:first"); 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 = jQuery( markup );
div.children().remove("span:last"); 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 // using contents will get comments regular, text, and comment nodes
// Handle the case where no comment is in the document // Handle the case where no comment is in the document
ok( jQuery("#nonnodes").contents().length >= 2, "Check node,textnode,comment remove works" ); 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" ); 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() { test( "remove() event cleaning ", 1, function() {
@ -1671,8 +1923,52 @@ test( "remove() in document order #13779", 1, function() {
jQuery.cleanData = cleanData; jQuery.cleanData = cleanData;
}); });
test( "detach()", 11, function() { test("detach() no filters", function () {
testRemove("detach");
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 = "<div><span>1</span><span>2</span><span>3</span><span>4</span></div>";
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() { test( "detach() event cleaning ", 1, function() {

View File

@ -31,12 +31,10 @@ function testWrap( val ) {
equal( defaultText, result, "Check for wrapping of on-the-fly html" ); equal( defaultText, result, "Check for wrapping of on-the-fly html" );
ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" ); ok( jQuery("#first").parent().parent().is(".red"), "Check if wrapper has class 'red'" );
QUnit.reset();
result = jQuery("#first").wrap( val(document.getElementById("empty")) ).parent(); result = jQuery("#first").wrap( val(document.getElementById("empty")) ).parent();
ok( result.is("ol"), "Check for element wrapping" ); ok( result.is("ol"), "Check for element wrapping" );
equal( result.text(), defaultText, "Check for element wrapping" ); equal( result.text(), defaultText, "Check for element wrapping" );
QUnit.reset();
jQuery("#check1").on( "click", function() { jQuery("#check1").on( "click", function() {
var checkbox = this; var checkbox = this;
@ -97,8 +95,6 @@ function testWrap( val ) {
j.parent().trigger("click"); j.parent().trigger("click");
// clean up attached elements
QUnit.reset();
} }
test( "wrap(String|Element)", function() { 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; var prev, p, result;
prev = jQuery("#firstp")[ 0 ].previousSibling; prev = jQuery("#firstp")[ 0 ].previousSibling;
p = jQuery("#firstp,#first")[ 0 ].parentNode; p = jQuery("#firstp,#first")[ 0 ].parentNode;
result = jQuery("#firstp,#first").wrapAll( val("<div class='red'><div class='tmp'></div></div>") ); result = jQuery("#firstp,#first").wrapAll( "<div class='red'><div class='tmp'></div></div>" );
equal( result.parent().length, 1, "Check for wrapping of on-the-fly html" ); 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'" ); 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 ].previousSibling, prev, "Correct Previous Sibling" );
equal( jQuery("#first").parent().parent()[ 0 ].parentNode, p, "Correct Parent" ); 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; prev = jQuery("#firstp")[ 0 ].previousSibling;
p = jQuery("#first")[ 0 ].parentNode; 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 ], jQuery("#firstp").parent()[ 0 ], "Same Parent" );
equal( jQuery("#first").parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" ); equal( jQuery("#first").parent()[ 0 ].previousSibling, prev, "Correct Previous Sibling" );
equal( jQuery("#first").parent()[ 0 ].parentNode, p, "Correct Parent" ); 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( "<div class='red'><div id='tmp'></div></div>" );
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<div>test</div><div>test2</div>").children().length;
jQuery("#first").wrapInner( "<div class='red'><div id='tmp'></div></div>" );
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("<div/>");
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( "<span></span>" );
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; num = jQuery("#first").children().length;
jQuery("#first").wrapInner( val("<div class='red'><div id='tmp'></div></div>") ); jQuery("#first").wrapInner( val("<div class='red'><div id='tmp'></div></div>") );
@ -179,32 +217,30 @@ function testWrapInner( val ) {
ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" );
QUnit.reset();
num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length; num = jQuery("#first").html("foo<div>test</div><div>test2</div>").children().length;
jQuery("#first").wrapInner( val("<div class='red'><div id='tmp'></div></div>") ); jQuery("#first").wrapInner( val("<div class='red'><div id='tmp'></div></div>") );
equal( jQuery("#first").children().length, 1, "Only one child" ); equal( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is(".red"), "Verify Right Element" ); ok( jQuery("#first").children().is(".red"), "Verify Right Element" );
equal( jQuery("#first").children().children().children().length, num, "Verify Elements Intact" ); 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("<div/>");
QUnit.reset();
num = jQuery("#first").children().length; num = jQuery("#first").children().length;
jQuery("#first").wrapInner( val(document.getElementById("empty")) ); jQuery("#first").wrapInner( val(document.getElementById("empty")) );
equal( jQuery("#first").children().length, 1, "Only one child" ); equal( jQuery("#first").children().length, 1, "Only one child" );
ok( jQuery("#first").children().is("#empty"), "Verify Right Element" ); ok( jQuery("#first").children().is("#empty"), "Verify Right Element" );
equal( jQuery("#first").children().children().length, num, "Verify Elements Intact" ); equal( jQuery("#first").children().children().length, num, "Verify Elements Intact" );
div = jQuery("<div/>");
div.wrapInner( val("<span></span>") ); div.wrapInner( val("<span></span>") );
equal( div.children().length, 1, "The contents were wrapped." ); equal( div.children().length, 1, "The contents were wrapped." );
equal( div.children()[ 0 ].nodeName.toLowerCase(), "span", "A span was inserted." ); 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() { test( "unwrap()", function() {