X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=8aa883aed0c4143bba00426f438c2a1524e787b4;hb=a38a5cd531a328319f8b7f3f33a84044b54591ce;hp=abd4f679dfb1de4c44b31b6f235e21c677e592af;hpb=c6b59263b5fb13f84ecdae4e51e4dd15198a6bdf;p=jquery.git diff --git a/test/unit/core.js b/test/unit/core.js index abd4f67..8aa883a 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -15,16 +15,16 @@ test("jQuery()", function() { expect(11); // 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([])" ); - + // 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" ); @@ -328,11 +328,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" ); @@ -372,8 +386,14 @@ test("each(Function)", function() { ok( pass, "Execute a function, Relative" ); }); +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(15); + expect(16); var elements = jQuery([window, document]), inputElements = jQuery('#radio1,#radio2,#check1,#check2'); @@ -399,6 +419,7 @@ test("index(Object|String|undefined)", function() { 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" ); }); @@ -420,7 +441,7 @@ test("jQuery.merge()", function() { }); 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" }, @@ -445,6 +466,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"); @@ -493,7 +530,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" ); }); @@ -518,6 +555,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(){ @@ -555,3 +599,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