X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=f49b6766e03bd86d3bb9cde58a852cf2aba9991a;hb=23d455b4469f2b47363c21867aad271d6930f112;hp=eee1740d6361d99c959eccbe1d3db98169772cb1;hpb=971840f343dae04fdc6ff51ecb03ec8b3aaee067;p=jquery.git diff --git a/test/unit/core.js b/test/unit/core.js index eee1740..1967007 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -8,38 +8,313 @@ test("Basic requirements", function() { ok( document.getElementsByTagName, "getElementsByTagName" ); ok( RegExp, "RegExp" ); ok( jQuery, "jQuery" ); - ok( $, "$()" ); + ok( $, "$" ); }); -test("$()", function() { - expect(5); - - var main = $("#main"); - isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" ); - +test("jQuery()", function() { + expect(24); + + strictEqual( commonJSDefined, jQuery, "CommonJS registered (Bug #7102)" ); + + // Basic constructor's behavior + + equals( jQuery().length, 0, "jQuery() === jQuery([])" ); + 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( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" ); + + + var main = jQuery("#main"); + same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" ); + +/* + // disabled since this test was doing nothing. i tried to fix it but i'm not sure + // what the expected behavior should even be. FF returns "\n" for the text node // make sure this is handled - $('

\r\n

'); - ok( true, "Check for \\r and \\n in jQuery()" ); - + var crlfContainer = jQuery('

\r\n

'); + var x = crlfContainer.contents().get(0).nodeValue; + equals( x, what???, "Check for \\r and \\n in jQuery()" ); +*/ + /* // Disabled until we add this functionality in var pass = true; try { - $("
Testing
").appendTo(document.getElementById("iframe").contentDocument.body); + jQuery("
Testing
").appendTo(document.getElementById("iframe").contentDocument.body); } catch(e){ pass = false; } - ok( pass, "$('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/ + ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/ - var code = $(""); + var code = jQuery(""); equals( code.length, 1, "Correct number of elements generated for code" ); - var img = $(""); + equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." ); + var img = jQuery(""); equals( img.length, 1, "Correct number of elements generated for img" ); - var div = $("

"); + equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." ); + var div = jQuery("

"); equals( div.length, 4, "Correct number of elements generated for div hr code b" ); + equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." ); + + 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" ); + + var exec = false; + + var elem = jQuery("
", { + width: 10, + css: { paddingLeft:1, paddingRight:1 }, + click: function(){ ok(exec, "Click executed."); }, + text: "test", + "class": "test2", + id: "test3" + }); + + equals( elem[0].style.width, '10px', 'jQuery() quick setter width'); + equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css'); + equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css'); + equals( elem[0].childNodes.length, 1, 'jQuery quick setter text'); + equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text'); + equals( elem[0].className, "test2", 'jQuery() quick setter class'); + equals( elem[0].id, "test3", 'jQuery() quick setter id'); + + exec = true; + elem.click(); +}); + +test("selector state", function() { + expect(31); + + var test; + + 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" ); + + 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" ); + + // Test cloning + 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" ); + + 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" ); + + var d = "
"; + equals( + jQuery(d).appendTo(jQuery(d)).selector, + jQuery(d).appendTo(d).selector, + "manipulation methods make same selector for jQuery objects" + ); +}); + +if ( !isLocal ) { +test("browser", function() { + stop(); + + jQuery.get("data/ua.txt", function(data){ + var uas = data.split("\n"); + expect( (uas.length - 1) * 2 ); + + jQuery.each(uas, function(){ + var parts = this.split("\t"); + if ( parts[2] ) { + var ua = jQuery.uaMatch( parts[2] ); + equals( ua.browser, parts[0], "Checking browser for " + parts[2] ); + equals( ua.version, parts[1], "Checking version string for " + parts[2] ); + } + }); + + start(); + }); +}); +} + +test("noConflict", function() { + expect(7); + + var $$ = jQuery; + + equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" ); + equals( jQuery, $$, "Make sure jQuery wasn't touched." ); + equals( $, original$, "Make sure $ was reverted." ); + + jQuery = $ = $$; + + equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" ); + equals( jQuery, originaljQuery, "Make sure jQuery was reverted." ); + equals( $, original$, "Make sure $ was reverted." ); + ok( $$("#main").html("test"), "Make sure that jQuery still works." ); + + jQuery = $$; +}); + +test("trim", function() { + expect(9); + + var nbsp = String.fromCharCode(160); + + equals( jQuery.trim("hello "), "hello", "trailing space" ); + equals( jQuery.trim(" hello"), "hello", "leading space" ); + equals( jQuery.trim(" hello "), "hello", "space on both sides" ); + equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " ); + + equals( jQuery.trim(), "", "Nothing in." ); + equals( jQuery.trim( undefined ), "", "Undefined" ); + equals( jQuery.trim( null ), "", "Null" ); + equals( jQuery.trim( 5 ), "5", "Number" ); + equals( jQuery.trim( false ), "false", "Boolean" ); +}); + +test("type", function() { + expect(23); + + equals( jQuery.type(null), "null", "null" ); + equals( jQuery.type(undefined), "undefined", "undefined" ); + equals( jQuery.type(true), "boolean", "Boolean" ); + equals( jQuery.type(false), "boolean", "Boolean" ); + equals( jQuery.type(Boolean(true)), "boolean", "Boolean" ); + equals( jQuery.type(0), "number", "Number" ); + equals( jQuery.type(1), "number", "Number" ); + equals( jQuery.type(Number(1)), "number", "Number" ); + equals( jQuery.type(""), "string", "String" ); + equals( jQuery.type("a"), "string", "String" ); + equals( jQuery.type(String("a")), "string", "String" ); + equals( jQuery.type({}), "object", "Object" ); + equals( jQuery.type(/foo/), "regexp", "RegExp" ); + equals( jQuery.type(new RegExp("asdf")), "regexp", "RegExp" ); + equals( jQuery.type([1]), "array", "Array" ); + equals( jQuery.type(new Date()), "date", "Date" ); + equals( jQuery.type(new Function("return;")), "function", "Function" ); + equals( jQuery.type(function(){}), "function", "Function" ); + equals( jQuery.type(window), "object", "Window" ); + equals( jQuery.type(document), "object", "Document" ); + equals( jQuery.type(document.body), "object", "Element" ); + equals( jQuery.type(document.createTextNode("foo")), "object", "TextNode" ); + equals( jQuery.type(document.getElementsByTagName("*")), "object", "NodeList" ); +}); + +test("isPlainObject", function() { + expect(14); + + stop(); + + // The use case that we want to match + ok(jQuery.isPlainObject({}), "{}"); + + // Not objects shouldn't be matched + ok(!jQuery.isPlainObject(""), "string"); + ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number"); + ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean"); + ok(!jQuery.isPlainObject(null), "null"); + ok(!jQuery.isPlainObject(undefined), "undefined"); + + // Arrays shouldn't be matched + ok(!jQuery.isPlainObject([]), "array"); + + // Instantiated objects shouldn't be matched + ok(!jQuery.isPlainObject(new Date), "new Date"); + + var fn = function(){}; + + // Functions shouldn't be matched + ok(!jQuery.isPlainObject(fn), "fn"); + + // Again, instantiated objects shouldn't be matched + ok(!jQuery.isPlainObject(new fn), "new fn (no methods)"); + + // Makes the function a little more realistic + // (and harder to detect, incidentally) + fn.prototype = {someMethod: function(){}}; + + // Again, instantiated objects shouldn't be matched + ok(!jQuery.isPlainObject(new fn), "new fn"); + + // DOM Element + ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element"); + + // Window + ok(!jQuery.isPlainObject(window), "window"); + + try { + var iframe = document.createElement("iframe"); + document.body.appendChild(iframe); + + window.iframeDone = function(otherObject){ + // Objects from other windows should be matched + ok(jQuery.isPlainObject(new otherObject), "new otherObject"); + document.body.removeChild( iframe ); + start(); + }; + + var doc = iframe.contentDocument || iframe.contentWindow.document; + doc.open(); + doc.write(""); + doc.close(); + } catch(e) { + document.body.removeChild( iframe ); + + ok(true, "new otherObject - iframes not supported"); + start(); + } }); test("isFunction", function() { - expect(21); + expect(19); // Make sure that false values return false ok( !jQuery.isFunction(), "No Value" ); @@ -50,10 +325,10 @@ test("isFunction", function() { // Check built-ins // Safari uses "(Internal Function)" - ok( jQuery.isFunction(String), "String Function" ); - ok( jQuery.isFunction(Array), "Array Function" ); - ok( jQuery.isFunction(Object), "Object Function" ); - ok( jQuery.isFunction(Function), "Function Function" ); + ok( jQuery.isFunction(String), "String Function("+String+")" ); + ok( jQuery.isFunction(Array), "Array Function("+Array+")" ); + ok( jQuery.isFunction(Object), "Object Function("+Object+")" ); + ok( jQuery.isFunction(Function), "Function Function("+Function+")" ); // When stringified, this could be misinterpreted var mystr = "function"; @@ -77,7 +352,8 @@ test("isFunction", function() { ok( !jQuery.isFunction(obj), "Object Element" ); // IE says this is an object - ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" ); + // Since 1.3, this isn't supported (#2968) + //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" ); var nodes = document.body.childNodes; @@ -85,7 +361,7 @@ test("isFunction", function() { ok( !jQuery.isFunction(nodes), "childNodes Property" ); var first = document.body.firstChild; - + // Normal elements are reported ok everywhere ok( !jQuery.isFunction(first), "A normal DOM Element" ); @@ -94,7 +370,8 @@ test("isFunction", function() { document.body.appendChild( input ); // IE says this is an object - ok( jQuery.isFunction(input.focus), "A default function property" ); + // Since 1.3, this isn't supported (#2968) + //ok( jQuery.isFunction(input.focus), "A default function property" ); document.body.removeChild( input ); @@ -115,972 +392,698 @@ test("isFunction", function() { ok( jQuery.isFunction(fn), "Recursive Function Call" ); - fn({ some: "data" }); + fn({ some: "data" }); }; callme(function(){ - callme(function(){}); + callme(function(){}); }); }); -var foo = false; - -test("$('html')", function() { +test("isXMLDoc - HTML", function() { expect(4); - - reset(); - foo = false; - var s = $("")[0]; + + ok( !jQuery.isXMLDoc( document ), "HTML document" ); + ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" ); + ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" ); + + var iframe = document.createElement("iframe"); + document.body.appendChild( iframe ); + + try { + var body = jQuery(iframe).contents()[0]; + + try { + ok( !jQuery.isXMLDoc( body ), "Iframe body element" ); + } catch(e) { + ok( false, "Iframe body element exception" ); + } + + } catch(e) { + ok( true, "Iframe body element - iframe not working correctly" ); + } + + document.body.removeChild( iframe ); +}); + +if ( !isLocal ) { +test("isXMLDoc - XML", function() { + expect(3); + stop(); + jQuery.get('data/dashboard.xml', function(xml) { + ok( jQuery.isXMLDoc( xml ), "XML document" ); + ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" ); + ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" ); + start(); + }); +}); +} + +test("isWindow", function() { + expect( 12 ); + + ok( jQuery.isWindow(window), "window" ); + ok( !jQuery.isWindow(), "empty" ); + ok( !jQuery.isWindow(null), "null" ); + ok( !jQuery.isWindow(undefined), "undefined" ); + ok( !jQuery.isWindow(document), "document" ); + ok( !jQuery.isWindow(document.documentElement), "documentElement" ); + ok( !jQuery.isWindow(""), "string" ); + ok( !jQuery.isWindow(1), "number" ); + ok( !jQuery.isWindow(true), "boolean" ); + ok( !jQuery.isWindow({}), "object" ); + // HMMM + // ok( !jQuery.isWindow({ setInterval: function(){} }), "fake window" ); + ok( !jQuery.isWindow(/window/), "regexp" ); + ok( !jQuery.isWindow(function(){}), "function" ); +}); + +test("jQuery('html')", function() { + expect(15); + + QUnit.reset(); + jQuery.foo = false; + var s = jQuery("")[0]; ok( s, "Creating a script" ); - ok( !foo, "Make sure the script wasn't executed prematurely" ); - $("body").append(s); - ok( foo, "Executing a scripts contents in the right context" ); - - reset(); - ok( $("")[0], "Creating a link" ); - - reset(); + ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" ); + jQuery("body").append(""); + ok( jQuery.foo, "Executing a scripts contents in the right context" ); + + // Test multi-line HTML + var div = jQuery("
\r\nsome text\n

some p

\nmore text\r\n
")[0]; + equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." ); + equals( div.firstChild.nodeType, 3, "Text node." ); + equals( div.lastChild.nodeType, 3, "Text node." ); + equals( div.childNodes[1].nodeType, 1, "Paragraph." ); + equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." ); + + QUnit.reset(); + ok( jQuery("")[0], "Creating a link" ); + + ok( !jQuery("'); +test("jQuery.makeArray", function(){ + expect(17); - $("#main").html('foo
'); + equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" ); - setTimeout( start, 100 ); -}); + equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" ); -test("filter()", function() { - expect(4); - isSet( $("#form input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" ); - isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" ); - isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" ); - isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" ); -}); + equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" ); -test("not()", function() { - expect(3); - ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" ); - 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)" ); -}); + equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" ); -test("andSelf()", function() { - expect(4); - isSet( $("#en").siblings().andSelf().get(), q("sndp", "sap","en"), "Check for siblings and self" ); - isSet( $("#foo").children().andSelf().get(), q("sndp", "en", "sap", "foo"), "Check for children and self" ); - isSet( $("#en, #sndp").parent().andSelf().get(), q("foo","en","sndp"), "Check for parent and self" ); - isSet( $("#groups").parents("p, div").andSelf().get(), q("ap", "main", "groups"), "Check for parents and self" ); -}); + equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" ); -test("siblings([String])", function() { - expect(5); - isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" ); - isSet( $("#sndp").siblings(":has(code)").get(), q("sap"), "Check for filtered siblings (has code child element)" ); - isSet( $("#sndp").siblings(":has(a)").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" ); - isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "testForm", "floatTest"), "Check for multiple filters" ); - isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" ); -}); + equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" ); -test("children([String])", function() { - expect(3); - isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" ); - isSet( $("#foo").children(":has(code)").get(), q("sndp", "sap"), "Check for filtered children" ); - isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" ); -}); + equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" ); -test("parent([String])", function() { - expect(5); - ok( $("#groups").parent()[0].id == "ap", "Simple parent check" ); - ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" ); - ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" ); - ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" ); - isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" ); -}); - -test("parents([String])", function() { - expect(5); - ok( $("#groups").parents()[0].id == "ap", "Simple parents check" ); - ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" ); - ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" ); - isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" ); - isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" ); -}); + equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" ); -test("next([String])", function() { - expect(4); - ok( $("#ap").next()[0].id == "foo", "Simple next check" ); - ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" ); - ok( $("#ap").next("p").length == 0, "Filtered next check, no match" ); - ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" ); -}); - -test("prev([String])", function() { - expect(4); - ok( $("#foo").prev()[0].id == "ap", "Simple prev check" ); - ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" ); - ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" ); - ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" ); -}); + equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" ); -test("show()", function() { - expect(1); - var pass = true, div = $("div"); - div.show().each(function(){ - if ( this.style.display == "none" ) pass = false; - }); - ok( pass, "Show" ); -}); + equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" ); -test("addClass(String)", function() { - expect(1); - var div = $("div"); - div.addClass("test"); - var pass = true; - for ( var i = 0; i < div.size(); i++ ) { - if ( div.get(i).className.indexOf("test") == -1 ) pass = false; - } - ok( pass, "Add Class" ); -}); + ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" ); -test("removeClass(String) - simple", function() { - expect(3); - var div = $("div").addClass("test").removeClass("test"), - pass = true; - for ( var i = 0; i < div.size(); i++ ) { - if ( div.get(i).className.indexOf("test") != -1 ) pass = false; - } - ok( pass, "Remove Class" ); - - reset(); - var div = $("div").addClass("test").addClass("foo").addClass("bar"); - div.removeClass("test").removeClass("bar").removeClass("foo"); - var pass = true; - for ( var i = 0; i < div.size(); i++ ) { - if ( div.get(i).className.match(/test|bar|foo/) ) pass = false; - } - ok( pass, "Remove multiple classes" ); - - reset(); - var div = $("div:eq(0)").addClass("test").removeClass(""); - ok( div.is('.test'), "Empty string passed to removeClass" ); - -}); + // function, is tricky as it has length + equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" ); -test("toggleClass(String)", function() { - expect(3); - var e = $("#firstp"); - ok( !e.is(".test"), "Assert class not present" ); - e.toggleClass("test"); - ok( e.is(".test"), "Assert class present" ); - e.toggleClass("test"); - ok( !e.is(".test"), "Assert class not present" ); -}); + //window, also has length + equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" ); -test("removeAttr(String", function() { - expect(1); - ok( $('#mark').removeAttr("class")[0].className == "", "remove class" ); -}); + equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" ); -test("text(String)", function() { - expect(1); - ok( $("#foo").text("
Hello cruel world!
")[0].innerHTML == "<div><b>Hello</b> cruel world!</div>", "Check escaped text" ); -}); + ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" ); -test("$.each(Object,Function)", function() { - expect(8); - $.each( [0,1,2], function(i, n){ - ok( i == n, "Check array iteration" ); - }); - - $.each( [5,6,7], function(i, n){ - ok( i == n - 5, "Check array iteration" ); - }); - - $.each( { name: "name", lang: "lang" }, function(i, n){ - ok( i == n, "Check object iteration" ); - }); + // For #5610 + same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly."); + same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly."); }); -test("$.prop", function() { +test("jQuery.isEmptyObject", function(){ expect(2); - var handle = function() { return this.id }; - ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" ); - ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" ); -}); -test("$.className", function() { - expect(6); - var x = $("

Hi

")[0]; - var c = $.className; - c.add(x, "hi"); - ok( x.className == "hi", "Check single added class" ); - c.add(x, "foo bar"); - ok( x.className == "hi foo bar", "Check more added classes" ); - c.remove(x); - ok( x.className == "", "Remove all classes" ); - c.add(x, "hi foo bar"); - c.remove(x, "foo"); - ok( x.className == "hi bar", "Check removal of one class" ); - ok( c.has(x, "hi"), "Check has1" ); - ok( c.has(x, "bar"), "Check has2" ); + 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" ); }); -test("remove()", function() { +test("jQuery.proxy", function(){ expect(4); - $("#ap").children().remove(); - ok( $("#ap").text().length > 10, "Check text is not removed" ); - ok( $("#ap").children().length == 0, "Check remove" ); - - reset(); - $("#ap").children().remove("a"); - ok( $("#ap").text().length > 10, "Check text is not removed" ); - ok( $("#ap").children().length == 1, "Check filtered remove" ); -}); -test("empty()", function() { - expect(2); - ok( $("#ap").children().empty().text().length == 0, "Check text is removed" ); - ok( $("#ap").children().length == 4, "Check elements are not removed" ); + var test = function(){ equals( this, thisObject, "Make sure that scope is set properly." ); }; + var thisObject = { foo: "bar", method: test }; + + // Make sure normal works + test.call( thisObject ); + + // Basic scoping + jQuery.proxy( test, thisObject )(); + + // Make sure it doesn't freak out + equals( jQuery.proxy( null, thisObject ), undefined, "Make sure no function was returned." ); + + // Use the string shortcut + jQuery.proxy( thisObject, "method" )(); }); -test("slice()", function() { - expect(5); - isSet( $("#ap a").slice(1,2), q("groups"), "slice(1,2)" ); - isSet( $("#ap a").slice(1), q("groups", "anchor1", "mark"), "slice(1)" ); - isSet( $("#ap a").slice(0,3), q("google", "groups", "anchor1"), "slice(0,3)" ); - isSet( $("#ap a").slice(-1), q("mark"), "slice(-1)" ); +test("jQuery.parseJSON", function(){ + expect(8); + + equals( jQuery.parseJSON(), null, "Nothing in, null out." ); + equals( jQuery.parseJSON( null ), null, "Nothing in, null out." ); + equals( jQuery.parseJSON( "" ), null, "Nothing in, null out." ); + + same( jQuery.parseJSON("{}"), {}, "Plain object parsing." ); + same( jQuery.parseJSON('{"test":1}'), {"test":1}, "Plain object parsing." ); - isSet( $("#ap a").eq(1), q("groups"), "eq(1)" ); + same( jQuery.parseJSON('\n{"test":1}'), {"test":1}, "Make sure leading whitespaces are handled." ); + + try { + jQuery.parseJSON("{a:1}"); + ok( false, "Test malformed JSON string." ); + } catch( e ) { + ok( true, "Test malformed JSON string." ); + } + + try { + jQuery.parseJSON("{'a':1}"); + ok( false, "Test malformed JSON string." ); + } catch( e ) { + ok( true, "Test malformed JSON string." ); + } }); -test("map()", function() { - expect(2); +test("jQuery._Deferred()", function() { + + expect( 10 ); + + var deferred, + object, + test; + + deferred = jQuery._Deferred(); + + test = false; + + deferred.then( function( value ) { + equals( value , "value" , "Test pre-resolve callback" ); + test = true; + } ); + + deferred.resolve( "value" ); + + ok( test , "Test pre-resolve callbacks called right away" ); - isSet( - $("#ap").map(function(){ - return $(this).find("a").get(); - }), - q("google", "groups", "anchor1", "mark"), - "Array Map" - ); + test = false; + + deferred.then( function( value ) { + equals( value , "value" , "Test post-resolve callback" ); + test = true; + } ); + + ok( test , "Test post-resolve callbacks called right away" ); + + deferred.cancel(); + + test = true; + + deferred.then( function() { + ok( false , "Cancel was ignored" ); + test = false; + } ); + + ok( test , "Test cancel" ); + + deferred = jQuery._Deferred().resolve(); + + try { + deferred.then( function() { + throw "Error"; + } , function() { + ok( true , "Test deferred do not cancel on exception" ); + } ); + } catch( e ) { + strictEqual( e , "Error" , "Test deferred propagates exceptions"); + deferred.then(); + } + + test = ""; + deferred = jQuery._Deferred().then( function() { + + test += "A"; + + }, function() { + + test += "B"; + + } ).resolve(); + + strictEqual( test , "AB" , "Test multiple then parameters" ); + + test = ""; + + deferred.then( function() { + + deferred.then( function() { + + test += "C"; + + } ); + + test += "A"; + + }, function() { + + test += "B"; + } ); + + strictEqual( test , "ABC" , "Test then callbacks order" ); + + deferred = jQuery._Deferred(); + + deferred.fire( jQuery , [ document ] ).then( function( doc ) { + ok( this === jQuery && arguments.length === 1 && doc === document , "Test fire context & args" ); + }); +}); - isSet( - $("#ap > a").map(function(){ - return this.parentNode; - }), - q("ap","ap","ap"), - "Single Map" - ); +test("jQuery.Deferred()", function() { + + expect( 4 ); + + jQuery.Deferred( function( defer ) { + strictEqual( this , defer , "Defer passed as this & first argument" ); + this.resolve( "done" ); + }).then( function( value ) { + strictEqual( value , "done" , "Passed function executed" ); + }); + + jQuery.Deferred().resolve().then( function() { + ok( true , "Success on resolve" ); + }).fail( function() { + ok( false , "Error on resolve" ); + }); + + jQuery.Deferred().reject().then( function() { + ok( false , "Success on reject" ); + }).fail( function() { + ok( true , "Error on reject" ); + }); +}); + +test("jQuery.isDeferred()", function() { + + expect( 11 ); + + var object1 = { then: function() { return this; } }, + object2 = { then: function() { return this; } }; + + object2.then._ = []; + + // The use case that we want to match + ok(jQuery.isDeferred(jQuery._Deferred()), "Simple deferred"); + ok(jQuery.isDeferred(jQuery.Deferred()), "Failable deferred"); + + // Some other objects + ok(!jQuery.isDeferred(object1), "Object with then & no marker"); + ok(!jQuery.isDeferred(object2), "Object with then & marker"); + + // Not objects shouldn't be matched + ok(!jQuery.isDeferred(""), "string"); + ok(!jQuery.isDeferred(0) && !jQuery.isDeferred(1), "number"); + ok(!jQuery.isDeferred(true) && !jQuery.isDeferred(false), "boolean"); + ok(!jQuery.isDeferred(null), "null"); + ok(!jQuery.isDeferred(undefined), "undefined"); + + object1 = {custom: jQuery._Deferred().then}; + + ok(!jQuery.isDeferred(object1) , "custom method name not found automagically"); + ok(jQuery.isDeferred(object1,"custom") , "custom method name"); }); -test("contents()", function() { - expect(2); - equals( $("#ap").contents().length, 9, "Check element contents" ); - ok( $("#iframe").contents()[0], "Check existance of IFrame document" ); - // Disabled, randomly fails - //ok( $("#iframe").contents()[0].body, "Check existance of IFrame body" ); +test("jQuery.when()", function() { + + expect( 5 ); + + var cache, i, deferred = { done: jQuery.Deferred().resolve( 1 ).then }; + + for( i = 1 ; i < 3 ; i++ ) { + jQuery.when( cache || jQuery.Deferred( function() { + this.resolve( i ); + }) ).then( function( value ) { + strictEqual( value , 1 , "Function executed" + ( i > 1 ? " only once" : "" ) ); + cache = value; + }).fail( function() { + ok( false , "Fail called" ); + }); + } + + cache = 0; + + for( i = 1 ; i < 3 ; i++ ) { + jQuery.when( cache || deferred , "done" ).done( function( value ) { + strictEqual( value , 1 , "Custom method: resolved" + ( i > 1 ? " only once" : "" ) ); + cache = value; + }).fail( function() { + ok( false , "Custom method: fail called" ); + }); + } + + stop(); + + jQuery.when( jQuery( document ) , "ready" ).then( function( test ) { + strictEqual( test , jQuery , "jQuery.fn.ready recognized as a deferred" ); + start(); + }); });