X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=63f2b6c854189448dd477287495ea0869af6ef4b;hb=71efbdd3b26f3a283f8d4bfdcc7b6343142027b9;hp=db47856095e3a2f2ce98af219b3b24c083e7f2bb;hpb=6728e3cf74d02794113deb0af70a5f9a7caa2241;p=jquery.git diff --git a/test/unit/core.js b/test/unit/core.js index db47856..63f2b6c 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -8,38 +8,173 @@ 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(8); + + var main = jQuery("#main"); + isSet( 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 = $(""); + var img = jQuery(""); equals( img.length, 1, "Correct number of elements generated for img" ); - var div = $("

"); + 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" ); +}); + +test("selector state", function() { + expect(30); + + var test; + + test = jQuery(); + 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" ); +}); + +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 + var version = v ? v[1] : null; + equals( version, browsers[i], "Checking UA string" ); + } +}); + +test("noConflict", function() { + expect(6); + + 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." ); + + jQuery = $$; }); test("isFunction", function() { - expect(21); + expect(19); // Make sure that false values return false ok( !jQuery.isFunction(), "No Value" ); @@ -50,10 +185,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 +212,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 +221,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 +230,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,669 +252,169 @@ test("isFunction", function() { ok( jQuery.isFunction(fn), "Recursive Function Call" ); - fn({ some: "data" }); + fn({ some: "data" }); }; callme(function(){ - callme(function(){}); + callme(function(){}); }); }); -test("$('html')", function() { - expect(2); - - reset(); - ok( $("")[0], "Creating a script" ); - - reset(); - ok( $("")[0], "Creating a link" ); - +test("jQuery('html')", function() { + expect(8); + reset(); -}); + jQuery.foo = false; + var s = jQuery("")[0]; + ok( s, "Creating a script" ); + 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("length", function() { - expect(1); - ok( $("p").length == 6, "Get Number of Elements Found" ); -}); + reset(); + ok( jQuery("")[0], "Creating a link" ); -test("size()", function() { - expect(1); - ok( $("p").size() == 6, "Get Number of Elements Found" ); -}); + ok( !jQuery("').evalScripts(); + var total = 0; + jQuery.each([1,2,3], function(i,v){ total += v; }); + equals( total, 6, "Looping over an array" ); + total = 0; + jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; }); + equals( total, 3, "Looping over an array, with break" ); + total = 0; + jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; }); + equals( total, 6, "Looping over an object" ); + 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" ); }); -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)" ); -}); +test("jQuery.makeArray", function(){ + expect(15); -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(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" ); -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(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" ); -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( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" ); -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([1,2,3]).join(""), "123", "Pass makeArray a real array" ); -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().length, 0, "Pass nothing to makeArray and expect an empty array" ); -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( 0 )[0], 0 , "Pass makeArray a number" ); -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( "foo" )[0], "foo", "Pass makeArray a string" ); -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" ); -}); + equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" ); -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" ); - -}); + equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" ); -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" ); -}); + equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" ); -test("removeAttr(String", function() { - expect(1); - ok( $('#mark').removeAttr("class")[0].className == "", "remove class" ); -}); - -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.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" ); -test("$.each(Object,Function)", function() { - expect(8); - $.each( [0,1,2], function(i, n){ - ok( i == n, "Check array iteration" ); - }); + // function, is tricky as it has length + equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" ); - $.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" ); - }); -}); - -test("$.prop", 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" ); -}); - -test("remove()", 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" ); -}); - -test("slice()", function() { - expect(4); - 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)" ); -}); + //window, also has length + equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" ); -test("map()", function() { - expect(2); + equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" ); - isSet( - $("#ap").map(function(){ - return $(this).find("a").get(); - }), - q("google", "groups", "anchor1", "mark"), - "Array Map" - ); - - isSet( - $("#ap > a").map(function(){ - return this.parentNode; - }), - q("ap","ap","ap"), - "Single Map" - ); -}); - -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" ); + ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" ); });