You can now overwrite values returned from .data() with .bind("getData") - returning...
[jquery.git] / test / unit / core.js
index b2cf8f1..3cb90aa 100644 (file)
@@ -217,6 +217,7 @@ test("$('html', context)", function() {
        equals($span.length, 1, "Verify a span created with a div context works, #1763");
 });
 
+if ( !isLocal ) {
 test("$(selector, xml).text(str) - Loaded via XML document", function() {
        expect(2);
        stop();
@@ -228,6 +229,7 @@ test("$(selector, xml).text(str) - Loaded via XML document", function() {
                start();
        });
 });
+}
 
 test("length", function() {
        expect(1);
@@ -250,11 +252,14 @@ test("get(Number)", function() {
 });
 
 test("add(String|Element|Array|undefined)", function() {
-       expect(9);
+       expect(8);
        isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
        isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
        ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
-       equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" );
+
+       // For the time being, we're discontinuing support for $(form.elements) since it's ambiguous in IE
+       // use $([]).add(form.elements) instead.
+       //equals( $([]).add($("#form")[0].elements).length, $($("#form")[0].elements).length, "Array in constructor must equals array in add()" );
        
        var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
        ok( x[0].id == "x1", "Check on-the-fly element1" );
@@ -950,15 +955,31 @@ test("find(String)", function() {
 });
 
 test("clone()", function() {
-       expect(6);
+       expect(20);
        ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
        var clone = $('#yahoo').clone();
        ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
        ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
+
+       var cloneTags = [ 
+               "<table/>", "<tr/>", "<td/>", "<div/>", 
+               "<button/>", "<ul/>", "<ol/>", "<li/>",
+               "<input type='checkbox' />", "<select/>", "<option/>", "<textarea/>",
+               "<tbody/>", "<thead/>", "<tfoot/>", "<iframe/>"
+       ];
+       for (var i = 0; i < cloneTags.length; i++) {
+               var j = $(cloneTags[i]);
+               equals( j[0].tagName, j.clone()[0].tagName, 'Clone a &lt;' + cloneTags[i].substring(1));
+       }
+
        // using contents will get comments regular, text, and comment nodes
        var cl = $("#nonnodes").contents().clone();
        ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
+});
 
+if (!isLocal) {
+test("clone() on XML nodes", function() {
+       expect(2);
        stop();
        $.get("data/dashboard.xml", function (xml) {
                var root = $(xml.documentElement).clone();
@@ -969,6 +990,7 @@ test("clone()", function() {
                start();
        });
 });
+}
 
 test("is(String)", function() {
        expect(26);
@@ -1375,6 +1397,52 @@ test("$.data", function() {
        ok( jQuery.data(div, "test") == "overwritten", "Check for overwritten data" );
 });
 
+test(".data()", function() {
+       expect(16);
+       var div = $("#foo");
+       ok( div.data("test") == undefined, "Check for no data exists" );
+       div.data("test", "success");
+       ok( div.data("test") == "success", "Check for added data" );
+       div.data("test", "overwritten");
+       ok( div.data("test") == "overwritten", "Check for overwritten data" );
+
+       var hits = {test:0}, gets = {test:0};
+
+       div
+               .bind("setData",function(e,key,value){ hits[key] += value; })
+               .bind("setData.foo",function(e,key,value){ hits[key] += value; })
+               .bind("getData",function(e,key){ gets[key] += 1; })
+               .bind("getData.foo",function(e,key){ gets[key] += 3; });
+
+       div.data("test.foo", 2);
+       ok( div.data("test") == "overwritten", "Check for original data" );
+       ok( div.data("test.foo") == 2, "Check for namespaced data" );
+       ok( div.data("test.bar") == "overwritten", "Check for unmatched namespace" );
+       equals( hits.test, 2, "Check triggered setter functions" );
+       equals( gets.test, 5, "Check triggered getter functions" );
+
+       hits.test = 0;
+       gets.test = 0;
+
+       div.data("test", 1);
+       ok( div.data("test") == 1, "Check for original data" );
+       ok( div.data("test.foo") == 2, "Check for namespaced data" );
+       ok( div.data("test.bar") == 1, "Check for unmatched namespace" );
+       equals( hits.test, 1, "Check triggered setter functions" );
+       equals( gets.test, 5, "Check triggered getter functions" );
+
+       hits.test = 0;
+       gets.test = 0;
+
+       div
+               .bind("getData",function(e,key){ return key + "root"; })
+               .bind("getData.foo",function(e,key){ return key + "foo"; });
+
+       ok( div.data("test") == "testroot", "Check for original data" );
+       ok( div.data("test.foo") == "testfoo", "Check for namespaced data" );
+       ok( div.data("test.bar") == "testroot", "Check for unmatched namespace" );
+});
+
 test("$.removeData", function() {
        expect(1);
        var div = $("#foo")[0];
@@ -1383,6 +1451,27 @@ test("$.removeData", function() {
        ok( jQuery.data(div, "test") == undefined, "Check removal of data" );
 });
 
+test(".removeData()", function() {
+       expect(6);
+       var div = $("#foo");
+       div.data("test", "testing");
+       div.removeData("test");
+       ok( div.data("test") == undefined, "Check removal of data" );
+
+       div.data("test", "testing");
+       div.data("test.foo", "testing2");
+       div.removeData("test.bar");
+       ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
+       ok( div.data("test") == "testing", "Make sure data is intact" );
+
+       div.removeData("test");
+       ok( div.data("test.foo") == "testing2", "Make sure data is intact" );
+       ok( div.data("test") == undefined, "Make sure data is intact" );
+
+       div.removeData("test.foo");
+       ok( div.data("test.foo") == undefined, "Make sure data is intact" );
+});
+
 test("remove()", function() {
        expect(6);
        $("#ap").children().remove();