X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=fe2e992a8aa7591b397fe44091840ba76fb96ca0;hb=8d52c27808a77f51d1cf42c7e738b0b356466c1a;hp=939e1b83785f37987866c7f519e2dfc845f5f5bb;hpb=d415e0adb8f89a00768091795860daa5f2c29835;p=jquery.git diff --git a/test/unit/core.js b/test/unit/core.js index 939e1b8..fe2e992 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -12,7 +12,18 @@ test("Basic requirements", function() { }); test("jQuery()", function() { - expect(8); + 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" ); @@ -42,11 +53,6 @@ test("jQuery()", function() { var div = jQuery("

"); 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" ); @@ -57,7 +63,7 @@ test("selector state", function() { var test; - test = jQuery(); + test = jQuery(undefined); equals( test.selector, "", "Empty jQuery Selector" ); equals( test.context, undefined, "Empty jQuery Context" ); @@ -322,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" ); @@ -367,7 +387,7 @@ test("each(Function)", function() { }); test("index(Object|String|undefined)", function() { - expect(15); + expect(16); var elements = jQuery([window, document]), inputElements = jQuery('#radio1,#radio2,#check1,#check2'); @@ -393,6 +413,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" ); }); @@ -414,7 +435,7 @@ test("jQuery.merge()", function() { }); test("jQuery.extend(Object, Object)", function() { - expect(20); + expect(21); var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" }, options = { xnumber2: 1, xstring2: "x", xxx: "newstring" }, @@ -439,6 +460,12 @@ 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" ); + var nullUndef; nullUndef = jQuery.extend({}, options, { xnumber2: null }); ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied"); @@ -487,7 +514,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" ); }); @@ -512,6 +539,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(){