X-Git-Url: http://git.asbjorn.it/?p=jquery.git;a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=24de5c9a8ed7a1a9091b75e6b7f1e9cebc811e8d;hp=54ad982d24f5d44d9bf244c2776d73eec33519c9;hb=e00f74c43bf58132be01c6417c6126a5bc085899;hpb=781fe8b80d08b287e4c6e4ca408f773c6a1f3b2d diff --git a/test/unit/core.js b/test/unit/core.js index 54ad982..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,7 +12,7 @@ test("Basic requirements", function() { }); test("jQuery()", function() { - expect(23); + expect(24); // Basic constructor's behavior @@ -21,11 +21,11 @@ test("jQuery()", function() { 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"); @@ -84,6 +84,17 @@ test("jQuery()", function() { 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() { @@ -151,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, @@ -183,7 +194,7 @@ test("browser", function() { } test("noConflict", function() { - expect(6); + expect(7); var $$ = jQuery; @@ -196,19 +207,54 @@ 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(4); + 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", " " ); - var nbsp = String.fromCharCode(160); + 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.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.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() { @@ -218,55 +264,62 @@ test("isPlainObject", function() { // 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"); - - 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"); + 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(); - }; - - var doc = iframe.contentDocument || iframe.contentWindow.document; - doc.open(); - doc.write(""); - doc.close(); + } }); test("isFunction", function() { @@ -368,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 ); @@ -389,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" ); @@ -408,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("