You can now overwrite values returned from .data() with .bind("getData") - returning...
[jquery.git] / test / unit / core.js
index 03f89d1..3cb90aa 100644 (file)
@@ -43,6 +43,42 @@ test("$()", function() {
        equals( div.length, 4, "Correct number of elements generated for div hr code b" );
 });
 
+test("browser", function() {
+       expect(13);
+       var browsers = {
+               //Internet Explorer
+               "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
+               "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727)": "7.0",
+               /** Failing #1876
+                * "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; .NET CLR 2.0.50727; .NET CLR 1.1.4322; .NET CLR 3.0.04506.30)": "7.0",
+                */
+               //Browsers with Gecko engine
+               //Mozilla
+               "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
+               //Firefox
+               "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3": "1.8.1.3",
+               //Netscape
+               "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
+               //Flock
+               "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.11) Gecko/20070321 Firefox/1.5.0.11 Flock/0.7.12" : "1.8.0.11",
+               //Opera browser
+               "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
+               "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
+               "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
+               //WebKit engine
+               "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
+               "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
+               "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
+               //Other user agent string
+               "Other browser's user agent 1.0":null
+       };
+       for (var i in browsers) {
+               var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
+               version = v ? v[1] : null;
+               equals( version, browsers[i], "Checking UA string" );
+       }
+});
+
 test("noConflict", function() {
        expect(6);
        
@@ -181,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();
@@ -192,6 +229,7 @@ test("$(selector, xml).text(str) - Loaded via XML document", function() {
                start();
        });
 });
+}
 
 test("length", function() {
        expect(1);
@@ -218,6 +256,10 @@ test("add(String|Element|Array|undefined)", function() {
        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" );
+
+       // 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" );
@@ -420,7 +462,7 @@ test("css(String|Hash)", function() {
 });
 
 test("css(String, Object)", function() {
-       expect(20);
+       expect(21);
        ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
        $('#foo').css('display', 'none');
        ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
@@ -454,6 +496,10 @@ test("css(String, Object)", function() {
        var j = $("#nonnodes").contents();
        j.css("padding-left", "1px");
        equals( j.css("padding-left"), "1px", "Check node,textnode,comment css works" );
+
+       // opera sometimes doesn't update 'display' correctly, see #2037
+       $("#t2037")[0].innerHTML = $("#t2037")[0].innerHTML
+       equals( $("#t2037 .hidden").css("display"), "none", "Make sure browser thinks it is hidden" );
 });
 
 test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", function () {
@@ -469,12 +515,55 @@ test("jQuery.css(elem, 'height') doesn't clear radio buttons (bug #1095)", funct
 });
 
 test("width()", function() {
-       expect(2);
+       expect(9);
+
+       var $div = $("#nothiddendiv");
+       $div.width(30);
+       equals($div.width(), 30, "Test set to 30 correctly");
+       $div.width(-1); // handle negative numbers by ignoring #1599
+       equals($div.width(), 30, "Test negative width ignored");
+       $div.css("padding", "20px");
+       equals($div.width(), 30, "Test padding specified with pixels");
+       $div.css("border", "2px solid #fff");
+       equals($div.width(), 30, "Test border specified with pixels");
+       $div.css("padding", "2em");
+       equals($div.width(), 30, "Test padding specified with ems");
+       $div.css("border", "1em solid #fff");
+       equals($div.width(), 30, "Test border specified with ems");
+       $div.css("padding", "2%");
+       equals($div.width(), 30, "Test padding specified with percent");
+       $div.hide();
+       equals($div.width(), 30, "Test hidden div");
+       
+       $div.css({ display: "", border: "", padding: "" });
+       
+       $("#nothiddendivchild").css({ padding: "3px", border: "2px solid #fff" });
+       equals($("#nothiddendivchild").width(), 20, "Test child width with border and padding");
+       $("#nothiddendiv, #nothiddendivchild").css({ border: "", padding: "", width: "" });
+});
+
+test("height()", function() {
+       expect(8);
 
-       $("#nothiddendiv").width(30);
-       equals($("#nothiddendiv").width(), 30, "Test set to 30 correctly");
-       $("#nothiddendiv").width(-1); // handle negative numbers by ignoring #1599
-       equals($("#nothiddendiv").width(), 30, "Test negative width ignored");
+       var $div = $("#nothiddendiv");
+       $div.height(30);
+       equals($div.height(), 30, "Test set to 30 correctly");
+       $div.height(-1); // handle negative numbers by ignoring #1599
+       equals($div.height(), 30, "Test negative height ignored");
+       $div.css("padding", "20px");
+       equals($div.height(), 30, "Test padding specified with pixels");
+       $div.css("border", "2px solid #fff");
+       equals($div.height(), 30, "Test border specified with pixels");
+       $div.css("padding", "2em");
+       equals($div.height(), 30, "Test padding specified with ems");
+       $div.css("border", "1em solid #fff");
+       equals($div.height(), 30, "Test border specified with ems");
+       $div.css("padding", "2%");
+       equals($div.height(), 30, "Test padding specified with percent");
+       $div.hide();
+       equals($div.height(), 30, "Test hidden div");
+       
+       $div.css({ display: "", border: "", padding: "", height: "1px" });
 });
 
 test("text()", function() {
@@ -866,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();
@@ -885,6 +990,7 @@ test("clone()", function() {
                start();
        });
 });
+}
 
 test("is(String)", function() {
        expect(26);
@@ -982,11 +1088,12 @@ test("$.extend(Object, Object)", function() {
 });
 
 test("val()", function() {
-       expect(3);
+       expect(4);
        ok( $("#text1").val() == "Test", "Check for value of input element" );
        ok( !$("#text1").val() == "", "Check for value of input element" );
        // ticket #1714 this caused a JS error in IE
        ok( $("#first").val() == "", "Check a paragraph element to see if it has a value" );
+       ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
 });
 
 test("val(String)", function() {
@@ -1054,12 +1161,17 @@ test("filter()", function() {
 });
 
 test("not()", function() {
-       expect(5);
+       expect(8);
        ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
+       ok( $("#main > p#ap > a").not(document.getElementById("google")).length == 2, "not(DOMElement)" );
        isSet( $("p").not(".result").get(), q("firstp", "ap", "sndp", "en", "sap", "first"), "not('.class')" );
        isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
        isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
+       ok( $("p").not(document.getElementsByTagName("p")).length == 0, "not(Array-like DOM collection)" );
        isSet( $("#form option").not("option.emptyopt:contains('Nothing'),[selected],[value='1']").get(), q("option1c", "option1d", "option2c", "option3d" ), "not('complex selector')");
+       
+       var selects = $("#form select");
+       isSet( selects.not( selects[1] ), q("select1", "select3"), "filter out DOM element");
 });
 
 test("andSelf()", function() {
@@ -1224,7 +1336,7 @@ test("text(String)", function() {
 });
 
 test("$.each(Object,Function)", function() {
-       expect(8);
+       expect(12);
        $.each( [0,1,2], function(i, n){
                ok( i == n, "Check array iteration" );
        });
@@ -1236,6 +1348,19 @@ test("$.each(Object,Function)", function() {
        $.each( { name: "name", lang: "lang" }, function(i, n){
                ok( i == n, "Check object iteration" );
        });
+
+        var total = 0;
+        jQuery.each([1,2,3], function(i,v){ total += v; });
+        ok( total == 6, "Looping over an array" );
+        total = 0;
+        jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
+        ok( total == 3, "Looping over an array, with break" );
+        total = 0;
+        jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
+        ok( total == 6, "Looping over an object" );
+        total = 0;
+        jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
+        ok( total == 3, "Looping over an object, with break" );
 });
 
 test("$.prop", function() {
@@ -1262,6 +1387,91 @@ test("$.className", function() {
        ok( c.has(x, "bar"), "Check has2" );
 });
 
+test("$.data", function() {
+       expect(3);
+       var div = $("#foo")[0];
+       ok( jQuery.data(div, "test") == undefined, "Check for no data exists" );
+       jQuery.data(div, "test", "success");
+       ok( jQuery.data(div, "test") == "success", "Check for added data" );
+       jQuery.data(div, "test", "overwritten");
+       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];
+       jQuery.data(div, "test", "testing");
+       jQuery.removeData(div, "test");
+       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();