Added a fix for .noConflict(true) not reverting properly. Also added unit tests for...
[jquery.git] / test / unit / core.js
index dce86d8..5101f81 100644 (file)
@@ -38,6 +38,27 @@ test("$()", function() {
        equals( div.length, 4, "Correct number of elements generated for div hr code b" );
 });
 
+test("noConflict", function() {
+       expect(6);
+       
+       var old = jQuery;
+       var newjQuery = jQuery.noConflict();
+
+       ok( newjQuery == old, "noConflict returned the jQuery object" );
+       ok( jQuery == old, "Make sure jQuery wasn't touched." );
+       ok( $ == "$", "Make sure $ was reverted." );
+
+       jQuery = $ = old;
+
+       newjQuery = jQuery.noConflict(true);
+
+       ok( newjQuery == old, "noConflict returned the jQuery object" );
+       ok( jQuery == "jQuery", "Make sure jQuery was reverted." );
+       ok( $ == "$", "Make sure $ was reverted." );
+
+       jQuery = $ = old;
+});
+
 test("isFunction", function() {
        expect(21);
 
@@ -123,11 +144,18 @@ test("isFunction", function() {
        });
 });
 
+var foo = false;
+
 test("$('html')", function() {
-       expect(2);
+       expect(4);
        
        reset();
-       ok( $("<script>var foo='test';</script>")[0], "Creating a script" );
+       foo = false;
+       var s = $("<script>var foo='test';</script>")[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( $("<link rel='stylesheet'/>")[0], "Creating a link" );
@@ -213,15 +241,17 @@ test("attr(String)", function() {
        ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
 });
 
-test("attr(String) in XML Files", function() {
-       expect(2);
-       stop();
-       $.get("data/dashboard.xml", function(xml) {
-               ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
-               ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
-               start();
-       });
-});
+if ( !isLocal ) {
+    test("attr(String) in XML Files", function() {
+        expect(2);
+        stop();
+        $.get("data/dashboard.xml", function(xml) {
+            ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
+            ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
+            start();
+        });
+    });
+}
 
 test("attr(String, Function)", function() {
        expect(2);
@@ -286,19 +316,21 @@ test("attr(String, Object)", function() {
        equals( "checkbox", $(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
 });
 
-test("attr(String, Object) - Loaded via XML document", function() {
-       expect(2);
-       stop();
-       $.get('data/dashboard.xml', function(xml) { 
-               var titles = [];
-               $('tab', xml).each(function() {
-                       titles.push($(this).attr('title'));
-               });
-               ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
-               ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
-               start();
-       });
-});
+if ( !isLocal ) {
+    test("attr(String, Object) - Loaded via XML document", function() {
+        expect(2);
+        stop();
+        $.get('data/dashboard.xml', function(xml) { 
+              var titles = [];
+              $('tab', xml).each(function() {
+                    titles.push($(this).attr('title'));
+              });
+              ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
+              ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
+              start();
+        });
+    });
+}
 
 test("css(String|Hash)", function() {
        expect(19);
@@ -767,7 +799,7 @@ test("is(String)", function() {
 });
 
 test("$.extend(Object, Object)", function() {
-       expect(10);
+       expect(11);
 
        var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
                options =     { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@@ -775,9 +807,9 @@ test("$.extend(Object, Object)", function() {
                merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
                deep1 = { foo: { bar: true } },
                deep1copy = { foo: { bar: true } },
-               deep2 = { foo: { baz: true } },
-               deep2copy = { foo: { baz: true } },
-               deepmerged = { foo: { bar: true, baz: true } };
+               deep2 = { foo: { baz: true }, foo2: document },
+               deep2copy = { foo: { baz: true }, foo2: document },
+               deepmerged = { foo: { bar: true, baz: true }, foo2: document };
 
        jQuery.extend(settings, options);
        isObj( settings, merged, "Check if extended: settings must be extended" );
@@ -790,6 +822,7 @@ test("$.extend(Object, Object)", function() {
        jQuery.extend(true, deep1, deep2);
        isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
        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 defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
                defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
@@ -813,15 +846,20 @@ test("val()", function() {
 });
 
 test("val(String)", function() {
-       expect(2);
+       expect(3);
        document.getElementById('text1').value = "bla";
        ok( $("#text1").val() == "bla", "Check for modified value of input element" );
        $("#text1").val('test');
        ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
+       
+       $("#select1").val("3");
+       ok( $("#select1").val() == "3", "Check for modified (via val(String)) value of select element" );
 });
 
+var scriptorder = 0;
+
 test("html(String)", function() {
-       expect(3);
+       expect(9);
        var div = $("div");
        div.html("<b>test</b>");
        var pass = true;
@@ -836,6 +874,8 @@ test("html(String)", function() {
 
        $("#main").html('foo <form><script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script></form>');
 
+       $("#main").html("<script>ok(scriptorder++ == 0, 'Script is executed in order');ok($('#scriptorder').length == 0,'Execute before html')<\/script><span id='scriptorder'><script>ok(scriptorder++ == 1, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script></span><script>ok(scriptorder++ == 2, 'Script is executed in order');ok($('#scriptorder').length == 1,'Execute after html')<\/script>");
+
        setTimeout( start, 100 );
 });
 
@@ -1034,11 +1074,13 @@ test("empty()", function() {
 });
 
 test("slice()", function() {
-       expect(4);
+       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)" );
+
+       isSet( $("#ap a").eq(1), q("groups"), "eq(1)" );
 });
 
 test("map()", function() {