Add test for jQuery(jQueryObj) cloning and simplify new get() code
[jquery.git] / test / unit / core.js
index 63f2b6c..28be653 100644 (file)
@@ -12,7 +12,21 @@ test("Basic requirements", function() {
 });
 
 test("jQuery()", function() {
-       expect(8);
+       expect(12);
+
+       // Basic constructor's behavior
+
+       equals( jQuery().length, 1, "jQuery() === jQuery(document)" );
+       equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
+       equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
+       equals( jQuery("").length, 0, "jQuery('') === jQuery([])" );
+
+       var obj = jQuery("div")
+       equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
+
+               // can actually yield more than one, when iframes are included, the window is an array as well
+       equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" );
+
 
        var main = jQuery("#main");
        isSet( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
@@ -42,11 +56,6 @@ test("jQuery()", function() {
        var div = jQuery("<div/><hr/><code/><b/>");
        equals( div.length, 4, "Correct number of elements generated for div hr code b" );
 
-       // can actually yield more than one, when iframes are included, the window is an array as well
-       equals( jQuery(window).length, 1, "Correct number of elements generated for window" );
-
-       equals( jQuery(document).length, 1, "Correct number of elements generated for document" );
-
        equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
 
        equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
@@ -56,19 +65,19 @@ test("selector state", function() {
        expect(30);
 
        var test;
-       
-       test = jQuery();
+
+       test = jQuery(undefined);
        equals( test.selector, "", "Empty jQuery Selector" );
        equals( test.context, undefined, "Empty jQuery Context" );
-       
+
        test = jQuery(document);
        equals( test.selector, "", "Document Selector" );
        equals( test.context, document, "Document Context" );
-       
+
        test = jQuery(document.body);
        equals( test.selector, "", "Body Selector" );
        equals( test.context, document.body, "Body Context" );
-       
+
        test = jQuery("#main");
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document, "#main Context" );
@@ -76,11 +85,11 @@ test("selector state", function() {
        test = jQuery("#notfoundnono");
        equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
        equals( test.context, document, "#notfoundnono Context" );
-       
+
        test = jQuery("#main", document);
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document, "#main Context" );
-       
+
        test = jQuery("#main", document.body);
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document.body, "#main Context" );
@@ -89,7 +98,7 @@ test("selector state", function() {
        test = jQuery(test);
        equals( test.selector, "#main", "#main Selector" );
        equals( test.context, document.body, "#main Context" );
-       
+
        test = jQuery(document.body).find("#main");
        equals( test.selector, "#main", "#main find Selector" );
        equals( test.context, document.body, "#main find Context" );
@@ -97,23 +106,23 @@ test("selector state", function() {
        test = jQuery("#main").filter("div");
        equals( test.selector, "#main.filter(div)", "#main filter Selector" );
        equals( test.context, document, "#main filter Context" );
-       
+
        test = jQuery("#main").not("div");
        equals( test.selector, "#main.not(div)", "#main not Selector" );
        equals( test.context, document, "#main not Context" );
-       
+
        test = jQuery("#main").filter("div").not("div");
        equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
        equals( test.context, document, "#main filter, not Context" );
-       
+
        test = jQuery("#main").filter("div").not("div").end();
        equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
        equals( test.context, document, "#main filter, not, end Context" );
-       
+
        test = jQuery("#main").parent("body");
        equals( test.selector, "#main.parent(body)", "#main parent Selector" );
        equals( test.context, document, "#main parent Context" );
-       
+
        test = jQuery("#main").eq(0);
        equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
        equals( test.context, document, "#main eq Context" );
@@ -322,11 +331,25 @@ test("get()", function() {
        isSet( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
 });
 
+test("toArray()", function() {
+       expect(1);
+       isSet ( jQuery("p").toArray(),
+               q("firstp","ap","sndp","en","sap","first"),
+               "Convert jQuery object to an Array" )
+})
+
 test("get(Number)", function() {
        expect(1);
        equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
 });
 
+test("get(-Number)",function() {
+       expect(1);
+       equals( jQuery("p").get(-1),
+               document.getElementById("first"),
+               "Get a single element with negative index" )
+})
+
 test("add(String|Element|Array|undefined)", function() {
        expect(12);
        isSet( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
@@ -366,12 +389,19 @@ test("each(Function)", function() {
        ok( pass, "Execute a function, Relative" );
 });
 
-test("index(Object)", function() {
-       expect(10);
+test("index()", function() {
+       expect(1);
+
+       equals( jQuery("#text2").index(), 2, "Returns the index of a child amongst its siblings" )
+});
+
+test("index(Object|String|undefined)", function() {
+       expect(16);
 
        var elements = jQuery([window, document]),
                inputElements = jQuery('#radio1,#radio2,#check1,#check2');
 
+       // Passing a node
        equals( elements.index(window), 0, "Check for index of elements" );
        equals( elements.index(document), 1, "Check for index of elements" );
        equals( inputElements.index(document.getElementById('radio1')), 0, "Check for index of elements" );
@@ -381,30 +411,40 @@ test("index(Object)", function() {
        equals( inputElements.index(window), -1, "Check for not found index" );
        equals( inputElements.index(document), -1, "Check for not found index" );
 
+       // Passing a jQuery object
        // enabled since [5500]
        equals( elements.index( elements ), 0, "Pass in a jQuery object" );
        equals( elements.index( elements.eq(1) ), 1, "Pass in a jQuery object" );
+       equals( jQuery("#form :radio").index( jQuery("#radio2") ), 1, "Pass in a jQuery object" );
+
+       // Passing a selector or nothing
+       // enabled since [6330]
+       equals( jQuery('#text2').index(), 2, "Check for index amongst siblings" );
+       equals( jQuery('#form').children().eq(4).index(), 4, "Check for index amongst siblings" );
+       equals( jQuery('#radio2').index('#form :radio') , 1, "Check for index within a selector" );
+       equals( jQuery('#form :radio').index( jQuery('#radio2') ), 1, "Check for index within a selector" );
+       equals( jQuery('#radio2').index('#form :text') , -1, "Check for index not found within a selector" );
 });
 
 test("jQuery.merge()", function() {
        expect(6);
-               
+
        var parse = jQuery.merge;
-       
+
        same( parse([],[]), [], "Empty arrays" );
-       
+
        same( parse([1],[2]), [1,2], "Basic" );
        same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
-       
+
        same( parse([1,2],[]), [1,2], "Second empty" );
-       same( parse([],[1,2]), [1,2], "First empty" );  
-       
+       same( parse([],[1,2]), [1,2], "First empty" );
+
        // Fixed at [5998], #3641
        same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
 });
 
 test("jQuery.extend(Object, Object)", function() {
-       expect(20);
+       expect(23);
 
        var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
                options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@@ -429,6 +469,22 @@ test("jQuery.extend(Object, Object)", function() {
        isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
        equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
 
+       var empty = {};
+       var optionsWithLength = { foo: { length: -1 } };
+       jQuery.extend(true, empty, optionsWithLength);
+       isObj( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
+
+       empty = {};
+       var optionsWithDate = { foo: { date: new Date } };
+       jQuery.extend(true, empty, optionsWithDate);
+       isObj( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
+
+       var myKlass = function() {};
+       empty = {};
+       var optionsWithCustomObject = { foo: { date: new myKlass } };
+       jQuery.extend(true, empty, optionsWithCustomObject);
+       isObj( empty.foo, optionsWithCustomObject.foo, "Custom objects copy correctly" );
+
        var nullUndef;
        nullUndef = jQuery.extend({}, options, { xnumber2: null });
        ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
@@ -477,7 +533,7 @@ test("jQuery.extend(Object, Object)", function() {
 });
 
 test("jQuery.each(Object,Function)", function() {
-       expect(12);
+       expect(13);
        jQuery.each( [0,1,2], function(i, n){
                equals( i, n, "Check array iteration" );
        });
@@ -502,6 +558,13 @@ test("jQuery.each(Object,Function)", function() {
        total = 0;
        jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
        equals( total, 3, "Looping over an object, with break" );
+
+       var f = function(){};
+       f.foo = 'bar';
+       jQuery.each(f, function(i){
+               f[i] = 'baz';
+       });
+       equals( "baz", f.foo, "Loop over a function" );
 });
 
 test("jQuery.makeArray", function(){
@@ -531,7 +594,7 @@ test("jQuery.makeArray", function(){
 
        // function, is tricky as it has length
        equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
-       
+
        //window, also has length
        equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
 
@@ -539,3 +602,13 @@ test("jQuery.makeArray", function(){
 
        ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
 });
+
+test("jQuery.isEmptyObject", function(){
+       expect(2);
+       
+       equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
+       equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
+       
+       // What about this ?
+       // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );
+});
\ No newline at end of file