X-Git-Url: http://git.asbjorn.it/?p=jquery.git;a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=24de5c9a8ed7a1a9091b75e6b7f1e9cebc811e8d;hp=9b43c4889439c027c03d0b23d4a5c1315ebaccc1;hb=e00f74c43bf58132be01c6417c6126a5bc085899;hpb=93be758de94b2d4a76fca217465c9e09b613cedd diff --git a/test/unit/core.js b/test/unit/core.js index 9b43c48..24de5c9 100644 --- a/test/unit/core.js +++ b/test/unit/core.js @@ -1,4 +1,4 @@ -module("core"); +module("core", { teardown: moduleTeardown }); test("Basic requirements", function() { expect(7); @@ -12,20 +12,20 @@ test("Basic requirements", function() { }); test("jQuery()", function() { - expect(12); + expect(24); // Basic constructor's behavior - equals( jQuery().length, 1, "jQuery() === jQuery(document)" ); + 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") + 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)" ); + equals( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" ); var main = jQuery("#main"); @@ -51,14 +51,50 @@ test("jQuery()", function() { var code = jQuery(""); equals( code.length, 1, "Correct number of elements generated for code" ); + 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" ); + 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(); + + // manually clean up detached elements + elem.remove(); + + for ( var i = 0; i < 3; ++i ) { + elem = jQuery(""); + } + equals( elem[0].defaultValue, "TEST", "Ensure cached nodes are cloned properly (Bug #6655)" ); + + // manually clean up detached elements + elem.remove(); }); test("selector state", function() { @@ -126,7 +162,7 @@ test("selector state", function() { 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, @@ -135,44 +171,30 @@ test("selector state", function() { ); }); +if ( !isLocal ) { 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" ); - } + 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(6); + expect(7); var $$ = jQuery; @@ -185,10 +207,121 @@ test("noConflict", function() { 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(19); @@ -288,9 +421,15 @@ test("isXMLDoc - HTML", function() { try { var body = jQuery(iframe).contents()[0]; - ok( !jQuery.isXMLDoc( body ), "Iframe body element" ); - } catch(e){ - ok( false, "Iframe body element exception" ); + + 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 ); @@ -309,10 +448,29 @@ test("isXMLDoc - XML", function() { }); } +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); - reset(); + QUnit.reset(); jQuery.foo = false; var s = jQuery("")[0]; ok( s, "Creating a script" ); @@ -328,7 +486,7 @@ test("jQuery('html')", function() { equals( div.childNodes[1].nodeType, 1, "Paragraph." ); equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." ); - reset(); + QUnit.reset(); ok( jQuery("")[0], "Creating a link" ); ok( !jQuery("