From 306b698813c7d999b6f572d8d00b2691952fdea6 Mon Sep 17 00:00:00 2001 From: Richard Gibson Date: Wed, 16 Jan 2013 12:30:47 -0500 Subject: [PATCH] No ticket: improve replaceWith testing (cherry picked from commit 13468abaeb5488d547bc75d99bc3425ee891859e) --- test/unit/manipulation.js | 90 +++++++++++++++------------------------ 1 file changed, 35 insertions(+), 55 deletions(-) diff --git a/test/unit/manipulation.js b/test/unit/manipulation.js index e933a7aff..f3040c4fb 100644 --- a/test/unit/manipulation.js +++ b/test/unit/manipulation.js @@ -1106,104 +1106,88 @@ test( "insertAfter(String|Element|Array|jQuery)", function() { var testReplaceWith = function( val ) { - expect( 22 ); + var tmp, y, child, child2, set, non_existent, $div, + expected = 22; - var tmp, y, child, child2, set, non_existant, $div; + expect( expected ); - jQuery("#yahoo").replaceWith(val( "buga" )); - ok( jQuery("#replace")[ 0 ], "Replace element with string" ); + jQuery("#yahoo").replaceWith( val("buga") ); + ok( jQuery("#replace")[ 0 ], "Replace element with element from string" ); ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after string" ); - QUnit.reset(); - jQuery("#yahoo").replaceWith(val( document.getElementById("first") )); + jQuery("#anchor2").replaceWith( val(document.getElementById("first")) ); ok( jQuery("#first")[ 0 ], "Replace element with element" ); - ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after element" ); + ok( !jQuery("#anchor2")[ 0 ], "Verify that original element is gone, after element" ); - QUnit.reset(); - jQuery("#qunit-fixture").append("
"); - jQuery("#baz").replaceWith("Baz"); + jQuery("#qunit-fixture").append("
"); + jQuery("#baz").replaceWith( val("Baz") ); equal( jQuery("#bar").text(),"Baz", "Replace element with text" ); ok( !jQuery("#baz")[ 0 ], "Verify that original element is gone, after element" ); - QUnit.reset(); - jQuery("#yahoo").replaceWith( val([ document.getElementById("first"), document.getElementById("mark") ]) ); + jQuery("#google").replaceWith( val([ document.getElementById("first"), document.getElementById("mark") ]) ); ok( jQuery("#first")[ 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("#google")[ 0 ], "Verify that original element is gone, after array of elements" ); - QUnit.reset(); - jQuery("#yahoo").replaceWith( val(jQuery("#mark, #first")) ); + jQuery("#groups").replaceWith( val(jQuery("#mark, #first")) ); ok( jQuery("#first")[ 0 ], "Replace element with set of elements" ); ok( jQuery("#mark")[ 0 ], "Replace element with set of elements" ); - ok( !jQuery("#yahoo")[ 0 ], "Verify that original element is gone, after set of elements" ); + ok( !jQuery("#groups")[ 0 ], "Verify that original element is gone, after set of elements" ); - QUnit.reset(); - tmp = jQuery("
").appendTo("body").click(function() { + + tmp = jQuery("
").appendTo("#qunit-fixture").click(function() { ok( true, "Newly bound click run." ); }); - y = jQuery("
").appendTo("body").click(function() { - ok( true, "Previously bound click run." ); + y = jQuery("
").appendTo("#qunit-fixture").click(function() { + ok( false, "Previously bound click run." ); }); child = y.append("test").find("b").click(function() { ok( true, "Child bound click run." ); return false; }); - y.replaceWith( tmp ); + y.replaceWith( val(tmp) ); tmp.click(); y.click(); // Shouldn't be run child.click(); // Shouldn't be run - tmp.remove(); - y.remove(); - child.remove(); - - QUnit.reset(); - y = jQuery("
").appendTo("body").click(function() { - ok( true, "Previously bound click run." ); + y = jQuery("
").appendTo("#qunit-fixture").click(function() { + ok( false, "Previously bound click run." ); }); child2 = y.append("test").find("u").click(function() { ok( true, "Child 2 bound click run." ); return false; }); - y.replaceWith( child2 ); + y.replaceWith( val(child2) ); child2.click(); - y.remove(); - child2.remove(); - QUnit.reset(); - - set = jQuery("
").replaceWith(val("test")); + set = jQuery("
").replaceWith( val("test") ); equal( set[0].nodeName.toLowerCase(), "div", "No effect on a disconnected node." ); equal( set.length, 1, "No effect on a disconnected node." ); equal( set[0].childNodes.length, 0, "No effect on a disconnected node." ); - non_existant = jQuery("#does-not-exist").replaceWith( val("should not throw an error") ); - equal( non_existant.length, 0, "Length of non existant element." ); - $div = jQuery("
").appendTo("body"); - // TODO: Work on jQuery(...) inline script execution - //$div.replaceWith("
"); - equal(jQuery(".replacewith").length, 1, "Check number of elements in page."); - jQuery(".replacewith").remove(); + non_existent = jQuery("#does-not-exist").replaceWith( val("should not throw an error") ); + equal( non_existent.length, 0, "Length of non existent element." ); - QUnit.reset(); + $div = jQuery("
").appendTo("#qunit-fixture"); + $div.replaceWith( val("
") ); jQuery("#qunit-fixture").append("
"); equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." ); - jQuery("#replaceWith").replaceWith( val("
") ); - equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." ); - + equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after replacement." ); jQuery("#replaceWith").replaceWith( val("
") ); - equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists." ); + equal( jQuery("#qunit-fixture").find("div[id=replaceWith]").length, 1, "Make sure only one div exists after subsequent replacement." ); + + return expected; }; test( "replaceWith(String|Element|Array|jQuery)", function() { @@ -1211,17 +1195,13 @@ test( "replaceWith(String|Element|Array|jQuery)", function() { }); test( "replaceWith(Function)", function() { - testReplaceWith( manipulationFunctionReturningObj ); - - expect( 23 ); + expect( testReplaceWith(manipulationFunctionReturningObj) + 1 ); - var y = jQuery("#yahoo")[ 0 ]; + var y = jQuery("#foo")[ 0 ]; - jQuery(y).replaceWith(function() { + jQuery( y ).replaceWith(function() { equal( this, y, "Make sure the context is coming in correctly." ); }); - - QUnit.reset(); }); test( "replaceWith(string) for more than one element", function() { -- 2.39.5