Added tests for almost everything in jquery/jquery.js; fixed some docs and removed...
authorJörn Zaefferer <joern.zaefferer@gmail.com>
Wed, 10 Jan 2007 11:08:54 +0000 (11:08 +0000)
committerJörn Zaefferer <joern.zaefferer@gmail.com>
Wed, 10 Jan 2007 11:08:54 +0000 (11:08 +0000)
src/jquery/coreTest.js
src/jquery/jquery.js

index e587c3a..ed7b52b 100644 (file)
@@ -214,6 +214,29 @@ test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( expected == $('#sap').text(), "Check for appending of jQuery object" );\r
 });\r
 \r
+test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(5);\r
+       var defaultText = 'Try them out:'\r
+       $('<b>buga</b>').appendTo('#first');\r
+       ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );\r
+       ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');\r
+       \r
+       reset();\r
+       expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";\r
+       $(document.getElementById('first')).appendTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for appending of element" );\r
+       \r
+       reset();\r
+       expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
+       $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for appending of array of elements" );\r
+       \r
+       reset();\r
+       expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";\r
+       $("#first, #yahoo").appendTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for appending of jQuery object" );\r
+});\r
+\r
 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
        expect(5);\r
        var defaultText = 'Try them out:'\r
@@ -237,6 +260,29 @@ test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );\r
 });\r
 \r
+test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(5);\r
+       var defaultText = 'Try them out:'\r
+       $('<b>buga</b>').prependTo('#first');\r
+       ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );\r
+       ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');\r
+       \r
+       reset();\r
+       expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";\r
+       $(document.getElementById('first')).prependTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for prepending of element" );\r
+\r
+       reset();\r
+       expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
+       $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for prepending of array of elements" );\r
+       \r
+       reset();\r
+       expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";\r
+       $("#yahoo, #first").prependTo('#sap');\r
+       ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );\r
+});\r
+\r
 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
        expect(4);\r
        var expected = 'This is a normal link: bugaYahoo';\r
@@ -259,6 +305,28 @@ test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( expected == $('#en').text(), "Insert jQuery before" );\r
 });\r
 \r
+test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(4);\r
+       var expected = 'This is a normal link: bugaYahoo';\r
+       $('<b>buga</b>').insertBefore('#yahoo');\r
+       ok( expected == $('#en').text(), 'Insert String before' );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: Try them out:Yahoo";\r
+       $(document.getElementById('first')).insertBefore('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert element before" );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
+       $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert array of elements before" );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: Try them out:diveintomarkYahoo";\r
+       $("#first, #mark").insertBefore('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert jQuery before" );\r
+});\r
+\r
 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
        expect(4);\r
        var expected = 'This is a normal link: Yahoobuga';\r
@@ -281,10 +349,36 @@ test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
        ok( expected == $('#en').text(), "Insert jQuery after" );\r
 });\r
 \r
+test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {\r
+       expect(4);\r
+       var expected = 'This is a normal link: Yahoobuga';\r
+       $('<b>buga</b>').insertAfter('#yahoo');\r
+       ok( expected == $('#en').text(), 'Insert String after' );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: YahooTry them out:";\r
+       $(document.getElementById('first')).insertAfter('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert element after" );\r
+\r
+       reset();\r
+       expected = "This is a normal link: YahooTry them out:diveintomark";\r
+       $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert array of elements after" );\r
+       \r
+       reset();\r
+       expected = "This is a normal link: YahooTry them out:diveintomark";\r
+       $("#mark, #first").insertAfter('#yahoo');\r
+       ok( expected == $('#en').text(), "Insert jQuery after" );\r
+});\r
+\r
 test("end()", function() {\r
-       expect(2);\r
+       expect(3);\r
        ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );\r
        ok( $('#yahoo').end(), 'Check for end with nothing to end' );\r
+       \r
+       var x = $('#yahoo');\r
+       x.parent();\r
+       ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );\r
 });\r
 \r
 test("find(String)", function() {\r
@@ -492,3 +586,65 @@ test("text(String)", function() {
        expect(1);\r
        ok( $("#foo").text("<div><b>Hello</b> cruel world!</div>")[0].innerHTML == "&lt;div&gt;&lt;b&gt;Hello&lt;/b&gt; cruel world!&lt;/div&gt;", "Check escaped text" );\r
 });\r
+\r
+test("$.each(Object,Function)", function() {\r
+       expect(8);\r
+       $.each( [0,1,2], function(i, n){\r
+               ok( i == n, "Check array iteration" );\r
+       });\r
+       \r
+       $.each( [5,6,7], function(i, n){\r
+               ok( i == n - 5, "Check array iteration" );\r
+       });\r
+        \r
+       $.each( { name: "name", lang: "lang" }, function(i, n){\r
+               ok( i == n, "Check object iteration" );\r
+       });\r
+});\r
+\r
+test("$.prop", function() {\r
+       expect(2);\r
+       var handle = function() { return this.id };\r
+       ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );\r
+       ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );\r
+});\r
+\r
+test("$.className", function() {\r
+       expect(6);\r
+       var x = $("<p>Hi</p>")[0];\r
+       var c = $.className;\r
+       c.add(x, "hi");\r
+       ok( x.className == "hi", "Check single added class" );\r
+       c.add(x, "foo bar");\r
+       ok( x.className == "hi foo bar", "Check more added classes" );\r
+       c.remove(x);\r
+       ok( x.className == "", "Remove all classes" );\r
+       c.add(x, "hi foo bar");\r
+       c.remove(x, "foo");\r
+       ok( x.className == "hi bar", "Check removal of one class" );\r
+       ok( c.has(x, "hi"), "Check has1" );\r
+       ok( c.has(x, "bar"), "Check has2" );\r
+});\r
+\r
+test("remove()", function() {\r
+       $("#ap").children().remove();\r
+       ok( $("#ap").text().length > 10, "Check text is not removed" );\r
+       ok( $("#ap").children().length == 0, "Check remove" );\r
+       \r
+       reset();\r
+       $("#ap").children().remove("a");\r
+       ok( $("#ap").text().length > 10, "Check text is not removed" );\r
+       ok( $("#ap").children().length == 1, "Check filtered remove" );\r
+});\r
+\r
+test("empty()", function() {\r
+       ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );\r
+       ok( $("#ap").children().length == 4, "Check elements are not removed" );\r
+});\r
+\r
+test("eq(), gt(), lt(), contains()", function() {\r
+       ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );\r
+       ok( $("#ap a").gt(1).get(), q("groups", "anchor1", "mark"), "gt()" );\r
+       ok( $("#ap a").lt(2).get(), q("google", "groups", "anchor1"), "lt()" );\r
+       ok( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );\r
+});
\ No newline at end of file
index 0f712af..6e71268 100644 (file)
@@ -160,21 +160,6 @@ var $ = jQuery;
  * @type jQuery
  */
 
-/**
- * A means of creating a cloned copy of a jQuery object. This function
- * copies the set of matched elements from one jQuery object and creates
- * another, new, jQuery object containing the same elements.
- *
- * @example var div = $("div");
- * $( div ).find("p");
- * @desc Locates all p elements with all div elements, without disrupting the original jQuery object contained in 'div' (as would normally be the case if a simple div.find("p") was done).
- *
- * @name $
- * @param jQuery obj The jQuery object to be cloned.
- * @cat Core
- * @type jQuery
- */
-
 jQuery.fn = jQuery.prototype = {
        /**
         * The current version of jQuery.
@@ -447,7 +432,7 @@ jQuery.fn = jQuery.prototype = {
                        for ( var prop in obj )
                                jQuery.attr(
                                        type ? this.style : this,
-                                       prop, jQuery.prop(this, prop, obj[prop], type)
+                                       prop, jQuery.prop(this, obj[prop])
                                );
                });
        },
@@ -1233,7 +1218,7 @@ jQuery.extend({
                return obj;
        },
        
-       prop: function(elem, key, value){
+       prop: function(elem, value){
                // Handle executable functions
                return value.constructor == Function &&
                        value.call( elem ) || value;
@@ -1859,8 +1844,9 @@ jQuery.each({
  *
  * @name appendTo
  * @type jQuery
- * @param String expr A jQuery expression of elements to match.
+ * @param <Content> content Content to append to the selected element to.
  * @cat DOM/Manipulation
+ * @see append(<Content>)
  */
 
 /**
@@ -1876,8 +1862,9 @@ jQuery.each({
  *
  * @name prependTo
  * @type jQuery
- * @param String expr A jQuery expression of elements to match.
+ * @param <Content> content Content to prepend to the selected element to.
  * @cat DOM/Manipulation
+ * @see prepend(<Content>)
  */
 
 /**
@@ -1893,8 +1880,9 @@ jQuery.each({
  *
  * @name insertBefore
  * @type jQuery
- * @param String expr A jQuery expression of elements to match.
+ * @param <Content> content Content to insert the selected element before.
  * @cat DOM/Manipulation
+ * @see before(<Content>)
  */
 
 /**
@@ -1910,8 +1898,9 @@ jQuery.each({
  *
  * @name insertAfter
  * @type jQuery
- * @param String expr A jQuery expression of elements to match.
+ * @param <Content> content Content to insert the selected element after.
  * @cat DOM/Manipulation
+ * @see after(<Content>)
  */
 
 jQuery.each({
@@ -1943,21 +1932,25 @@ jQuery.each({
  */
 
 /**
- * Adds the specified class to each of the set of matched elements.
+ * Adds the specified class(es) to each of the set of matched elements.
  *
  * @example $("p").addClass("selected")
  * @before <p>Hello</p>
  * @result [ <p class="selected">Hello</p> ]
  *
+ * @example $("p").addClass("selected highlight")
+ * @before <p>Hello</p>
+ * @result [ <p class="selected highlight">Hello</p> ]
+ *
  * @name addClass
  * @type jQuery
- * @param String class A CSS class to add to the elements
+ * @param String class One or more CSS classes to add to the elements
  * @cat DOM/Attributes
  * @see removeClass(String)
  */
 
 /**
- * Removes all or the specified class from the set of matched elements.
+ * Removes all or the specified class(es) from the set of matched elements.
  *
  * @example $("p").removeClass()
  * @before <p class="selected">Hello</p>
@@ -1967,9 +1960,13 @@ jQuery.each({
  * @before <p class="selected first">Hello</p>
  * @result [ <p class="first">Hello</p> ]
  *
+ * @example $("p").removeClass("selected highlight")
+ * @before <p class="highlight selected first">Hello</p>
+ * @result [ <p class="first">Hello</p> ]
+ *
  * @name removeClass
  * @type jQuery
- * @param String class (optional) A CSS class to remove from the elements
+ * @param String class (optional) One or more CSS classes to remove from the elements
  * @cat DOM/Attributes
  * @see addClass(String)
  */