X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=test%2Funit%2Fcore.js;h=2f775cd771dcc508a4bd8862528bc7e9e453a1a2;hb=ac2e731b2d219fd516a166a058831de1ff949d4a;hp=b2cf8f137e8a1cf97373e1359b92e635c06ade81;hpb=c39bd07cc9f408d5680ba565e2a2fe71bbdc64b9;p=jquery.git
diff --git a/test/unit/core.js b/test/unit/core.js
index b2cf8f1..88f7609 100644
--- a/test/unit/core.js
+++ b/test/unit/core.js
@@ -8,100 +8,316 @@ test("Basic requirements", function() {
ok( document.getElementsByTagName, "getElementsByTagName" );
ok( RegExp, "RegExp" );
ok( jQuery, "jQuery" );
- ok( $, "$()" );
+ ok( $, "$" );
});
-test("$()", function() {
- expect(4);
-
- var main = $("#main");
- isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
-
+test("jQuery()", function() {
+ expect(24);
+
+ // Basic constructor's behavior
+
+ 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");
+ 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( jQuery(window).length, 1, "Correct number of elements generated for jQuery(window)" );
+
+
+ var main = jQuery("#main");
+ same( 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
- var crlfContainer = $('
\r\n
');
+ 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 {
- $("
');
- ok( $('#table caption').length, "Append caption" );
-
- reset();
- $('form:last')
- .append('')
- .append('');
-
- t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
-
- // using contents will get comments regular, text, and comment nodes
- var j = $("#nonnodes").contents();
- var d = $("").appendTo("#nonnodes").append(j);
- equals( $("#nonnodes").length, 1, "Check node,textnode,comment append moved leaving just the div" );
- ok( d.contents().length >= 2, "Check node,textnode,comment append works" );
- d.contents().appendTo("#nonnodes");
- d.remove();
- ok( $("#nonnodes").contents().length >= 2, "Check node,textnode,comment append cleanup worked" );
-});
+ return;//these haven't been accepted yet
-test("appendTo(String|Element|Array<Element>|jQuery)", function() {
- expect(6);
- var defaultText = 'Try them out:'
- $('buga').appendTo('#first');
- ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
- ok( $('').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
-
- reset();
- var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
- $(document.getElementById('first')).appendTo('#sap');
- ok( expected == $('#sap').text(), "Check for appending of element" );
-
- reset();
- expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
- $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
- ok( expected == $('#sap').text(), "Check for appending of array of elements" );
-
- reset();
- expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
- $("#first, #yahoo").appendTo('#sap');
- ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
-
- reset();
- $('#select1').appendTo('#foo');
- t( 'Append select', '#foo select', ['select1'] );
-});
+ //for #2616
+ var keys = jQuery.map( {a:1,b:2}, function( v, k ){
+ return k;
+ }, [ ] );
-test("prepend(String|Element|Array<Element>|jQuery)", function() {
- expect(5);
- var defaultText = 'Try them out:'
- var result = $('#first').prepend('buga');
- ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
- ok( $('#select3').prepend('').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
-
- reset();
- var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
- $('#sap').prepend(document.getElementById('first'));
- ok( expected == $('#sap').text(), "Check for prepending of element" );
-
- reset();
- expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
- $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
- ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
-
- reset();
- expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
- $('#sap').prepend($("#first, #yahoo"));
- ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
-});
+ equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
-test("prependTo(String|Element|Array<Element>|jQuery)", function() {
- expect(6);
- var defaultText = 'Try them out:'
- $('buga').prependTo('#first');
- ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
- ok( $('').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
-
- reset();
- var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
- $(document.getElementById('first')).prependTo('#sap');
- ok( expected == $('#sap').text(), "Check for prepending of element" );
-
- reset();
- expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
- $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
- ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
-
- reset();
- expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
- $("#yahoo, #first").prependTo('#sap');
- ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
-
- reset();
- $('').prependTo('form:last');
- $('').prependTo('form:last');
-
- t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
-});
+ var values = jQuery.map( {a:1,b:2}, function( v, k ){
+ return v;
+ }, [ ] );
-test("before(String|Element|Array<Element>|jQuery)", function() {
- expect(4);
- var expected = 'This is a normal link: bugaYahoo';
- $('#yahoo').before('buga');
- ok( expected == $('#en').text(), 'Insert String before' );
-
- reset();
- expected = "This is a normal link: Try them out:Yahoo";
- $('#yahoo').before(document.getElementById('first'));
- ok( expected == $('#en').text(), "Insert element before" );
-
- reset();
- expected = "This is a normal link: Try them out:diveintomarkYahoo";
- $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
- ok( expected == $('#en').text(), "Insert array of elements before" );
-
- reset();
- expected = "This is a normal link: Try them out:diveintomarkYahoo";
- $('#yahoo').before($("#first, #mark"));
- ok( expected == $('#en').text(), "Insert jQuery before" );
-});
+ equals( values.join(""), "12", "Map the values from a hash to an array" );
-test("insertBefore(String|Element|Array<Element>|jQuery)", function() {
- expect(4);
- var expected = 'This is a normal link: bugaYahoo';
- $('buga').insertBefore('#yahoo');
- ok( expected == $('#en').text(), 'Insert String before' );
-
- reset();
- expected = "This is a normal link: Try them out:Yahoo";
- $(document.getElementById('first')).insertBefore('#yahoo');
- ok( expected == $('#en').text(), "Insert element before" );
-
- reset();
- expected = "This is a normal link: Try them out:diveintomarkYahoo";
- $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
- ok( expected == $('#en').text(), "Insert array of elements before" );
-
- reset();
- expected = "This is a normal link: Try them out:diveintomarkYahoo";
- $("#first, #mark").insertBefore('#yahoo');
- ok( expected == $('#en').text(), "Insert jQuery before" );
-});
+ var scripts = document.getElementsByTagName("script");
+ var mapped = jQuery.map( scripts, function( v, k ){
+ return v;
+ }, {length:0} );
-test("after(String|Element|Array<Element>|jQuery)", function() {
- expect(4);
- var expected = 'This is a normal link: Yahoobuga';
- $('#yahoo').after('buga');
- ok( expected == $('#en').text(), 'Insert String after' );
-
- reset();
- expected = "This is a normal link: YahooTry them out:";
- $('#yahoo').after(document.getElementById('first'));
- ok( expected == $('#en').text(), "Insert element after" );
-
- reset();
- expected = "This is a normal link: YahooTry them out:diveintomark";
- $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
- ok( expected == $('#en').text(), "Insert array of elements after" );
-
- reset();
- expected = "This is a normal link: YahooTry them out:diveintomark";
- $('#yahoo').after($("#first, #mark"));
- ok( expected == $('#en').text(), "Insert jQuery after" );
-});
+ equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
-test("insertAfter(String|Element|Array<Element>|jQuery)", function() {
- expect(4);
- var expected = 'This is a normal link: Yahoobuga';
- $('buga').insertAfter('#yahoo');
- ok( expected == $('#en').text(), 'Insert String after' );
-
- reset();
- expected = "This is a normal link: YahooTry them out:";
- $(document.getElementById('first')).insertAfter('#yahoo');
- ok( expected == $('#en').text(), "Insert element after" );
-
- reset();
- expected = "This is a normal link: YahooTry them out:diveintomark";
- $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
- ok( expected == $('#en').text(), "Insert array of elements after" );
-
- reset();
- expected = "This is a normal link: YahooTry them out:diveintomark";
- $("#mark, #first").insertAfter('#yahoo');
- ok( expected == $('#en').text(), "Insert jQuery after" );
-});
+ var flat = jQuery.map( Array(4), function( v, k ){
+ return k % 2 ? k : [k,k,k];//try mixing array and regular returns
+ });
-test("replaceWith(String|Element|Array<Element>|jQuery)", function() {
- expect(10);
- $('#yahoo').replaceWith('buga');
- ok( $("#replace")[0], 'Replace element with string' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
-
- reset();
- $('#yahoo').replaceWith(document.getElementById('first'));
- ok( $("#first")[0], 'Replace element with element' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
-
- reset();
- $('#yahoo').replaceWith([document.getElementById('first'), document.getElementById('mark')]);
- ok( $("#first")[0], 'Replace element with array of elements' );
- ok( $("#mark")[0], 'Replace element with array of elements' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
-
- reset();
- $('#yahoo').replaceWith($("#first, #mark"));
- ok( $("#first")[0], 'Replace element with set of elements' );
- ok( $("#mark")[0], 'Replace element with set of elements' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
+ equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
});
-test("replaceAll(String|Element|Array<Element>|jQuery)", function() {
- expect(10);
- $('buga').replaceAll("#yahoo");
- ok( $("#replace")[0], 'Replace element with string' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after string' );
-
- reset();
- $(document.getElementById('first')).replaceAll("#yahoo");
- ok( $("#first")[0], 'Replace element with element' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after element' );
-
- reset();
- $([document.getElementById('first'), document.getElementById('mark')]).replaceAll("#yahoo");
- ok( $("#first")[0], 'Replace element with array of elements' );
- ok( $("#mark")[0], 'Replace element with array of elements' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after array of elements' );
-
- reset();
- $("#first, #mark").replaceAll("#yahoo");
- ok( $("#first")[0], 'Replace element with set of elements' );
- ok( $("#mark")[0], 'Replace element with set of elements' );
- ok( !$("#yahoo")[0], 'Verify that original element is gone, after set of elements' );
-});
+test("jQuery.merge()", function() {
+ expect(8);
-test("end()", function() {
- expect(3);
- ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
- ok( $('#yahoo').end(), 'Check for end with nothing to end' );
-
- var x = $('#yahoo');
- x.parent();
- ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
-});
+ var parse = jQuery.merge;
-test("find(String)", function() {
- expect(2);
- ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
+ same( parse([],[]), [], "Empty arrays" );
- // using contents will get comments regular, text, and comment nodes
- var j = $("#nonnodes").contents();
- equals( j.find("div").length, 0, "Check node,textnode,comment to find zero divs" );
-});
+ same( parse([1],[2]), [1,2], "Basic" );
+ same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
-test("clone()", function() {
- expect(6);
- ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
- var clone = $('#yahoo').clone();
- ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
- ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
- // using contents will get comments regular, text, and comment nodes
- var cl = $("#nonnodes").contents().clone();
- ok( cl.length >= 2, "Check node,textnode,comment clone works (some browsers delete comments on clone)" );
+ same( parse([1,2],[]), [1,2], "Second empty" );
+ same( parse([],[1,2]), [1,2], "First empty" );
- stop();
- $.get("data/dashboard.xml", function (xml) {
- var root = $(xml.documentElement).clone();
- $("tab:first", xml).text("origval");
- $("tab:first", root).text("cloneval");
- equals($("tab:first", xml).text(), "origval", "Check original XML node was correctly set");
- equals($("tab:first", root).text(), "cloneval", "Check cloned XML node was correctly set");
- start();
- });
-});
+ // Fixed at [5998], #3641
+ same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
-test("is(String)", function() {
- expect(26);
- ok( $('#form').is('form'), 'Check for element: A form must be a form' );
- ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
- ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
- ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
- ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
- ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
- ok( $('#en').is('[lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
- ok( !$('#en').is('[lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
- ok( $('#text1').is('[type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
- ok( !$('#text1').is('[type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
- ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
- ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
- ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
- ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
- ok( $('#foo').is(':has(p)'), 'Check for child: Expected a child "p" element' );
- ok( !$('#foo').is(':has(ul)'), 'Check for child: Did not expect "ul" element' );
- ok( $('#foo').is(':has(p):has(a):has(code)'), 'Check for childs: Expected "p", "a" and "code" child elements' );
- ok( !$('#foo').is(':has(p):has(a):has(code):has(ol)'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
- ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
- ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
- ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
- ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
-
- // test is() with comma-seperated expressions
- ok( $('#en').is('[lang="en"],[lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
- ok( $('#en').is('[lang="de"],[lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
- ok( $('#en').is('[lang="en"] , [lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
- ok( $('#en').is('[lang="de"] , [lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
+ // After fixing #5527
+ same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
+ same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
});
-test("$.extend(Object, Object)", function() {
- expect(17);
+test("jQuery.extend(Object, Object)", function() {
+ expect(28);
var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
@@ -1013,28 +681,72 @@ test("$.extend(Object, Object)", function() {
deep1copy = { foo: { bar: true } },
deep2 = { foo: { baz: true }, foo2: document },
deep2copy = { foo: { baz: true }, foo2: document },
- deepmerged = { foo: { bar: true, baz: true }, foo2: document };
+ deepmerged = { foo: { bar: true, baz: true }, foo2: document },
+ arr = [1, 2, 3],
+ nestedarray = { arr: arr };
jQuery.extend(settings, options);
- isObj( settings, merged, "Check if extended: settings must be extended" );
- isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
+ same( settings, merged, "Check if extended: settings must be extended" );
+ same( options, optionsCopy, "Check if not modified: options must not be modified" );
jQuery.extend(settings, null, options);
- isObj( settings, merged, "Check if extended: settings must be extended" );
- isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
+ same( settings, merged, "Check if extended: settings must be extended" );
+ same( options, optionsCopy, "Check if not modified: options must not be modified" );
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" );
+ same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
+ same( 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" );
+ ok( jQuery.extend(true, {}, nestedarray).arr !== arr, "Deep extend of object must clone child array" );
+
+ // #5991
+ ok( jQuery.isArray( jQuery.extend(true, { arr: {} }, nestedarray).arr ), "Cloned array heve to be an Array" );
+ ok( jQuery.isPlainObject( jQuery.extend(true, { arr: arr }, { arr: {} }).arr ), "Cloned object heve to be an plain object" );
+
+ var empty = {};
+ var optionsWithLength = { foo: { length: -1 } };
+ jQuery.extend(true, empty, optionsWithLength);
+ same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
+
+ empty = {};
+ var optionsWithDate = { foo: { date: new Date } };
+ jQuery.extend(true, empty, optionsWithDate);
+ same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
+
+ var myKlass = function() {};
+ var customObject = new myKlass();
+ var optionsWithCustomObject = { foo: { date: customObject } };
+ empty = {};
+ jQuery.extend(true, empty, optionsWithCustomObject);
+ ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
+
+ // Makes the class a little more realistic
+ myKlass.prototype = { someMethod: function(){} };
+ empty = {};
+ jQuery.extend(true, empty, optionsWithCustomObject);
+ ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
+
+ var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
+ ok( ret.foo == 5, "Wrapped numbers copy correctly" );
+
+ var nullUndef;
+ nullUndef = jQuery.extend({}, options, { xnumber2: null });
+ ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
+
+ nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
+ ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
+
+ nullUndef = jQuery.extend({}, options, { xnumber0: null });
+ ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
+
var target = {};
var recursive = { foo:target, bar:5 };
jQuery.extend(true, target, recursive);
- isObj( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
+ same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
- ok( ret.foo.length == 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
+ equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
@@ -1059,416 +771,426 @@ test("$.extend(Object, Object)", function() {
merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
var settings = jQuery.extend({}, defaults, options1, options2);
- isObj( settings, merged2, "Check if extended: settings must be extended" );
- isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
- isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
- isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
+ same( settings, merged2, "Check if extended: settings must be extended" );
+ same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
+ same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
+ same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
});
-test("val()", function() {
- expect(4);
- ok( $("#text1").val() == "Test", "Check for value of input element" );
- ok( !$("#text1").val() == "", "Check for value of input element" );
- // ticket #1714 this caused a JS error in IE
- ok( $("#first").val() == "", "Check a paragraph element to see if it has a value" );
- ok( $([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
-});
+test("jQuery.each(Object,Function)", function() {
+ expect(13);
+ jQuery.each( [0,1,2], function(i, n){
+ equals( i, n, "Check array iteration" );
+ });
-test("val(String)", function() {
- expect(4);
- 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" );
-
- // using contents will get comments regular, text, and comment nodes
- var j = $("#nonnodes").contents();
- j.val("asdf");
- equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
- j.removeAttr("value");
+ jQuery.each( [5,6,7], function(i, n){
+ equals( i, n - 5, "Check array iteration" );
+ });
+
+ jQuery.each( { name: "name", lang: "lang" }, function(i, n){
+ equals( i, n, "Check object iteration" );
+ });
+
+ 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" );
+
+ var f = function(){};
+ f.foo = 'bar';
+ jQuery.each(f, function(i){
+ f[i] = 'baz';
+ });
+ equals( "baz", f.foo, "Loop over a function" );
});
-var scriptorder = 0;
+test("jQuery.makeArray", function(){
+ expect(17);
-test("html(String)", function() {
- expect(11);
- var div = $("#main > div");
- div.html("test");
- var pass = true;
- for ( var i = 0; i < div.size(); i++ ) {
- if ( div.get(i).childNodes.length != 1 ) pass = false;
- }
- ok( pass, "Set HTML" );
+ equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
- reset();
- // using contents will get comments regular, text, and comment nodes
- var j = $("#nonnodes").contents();
- j.html("bold");
- equals( j.html().toLowerCase(), "bold", "Check node,textnode,comment with html()" );
+ equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
- $("#main").html("");
- $("#main select").html("");
- equals( $("#main select").val(), "O2", "Selected option correct" );
+ equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
- stop();
+ equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
- $("#main").html('');
+ equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
- $("#main").html('foo ');
+ equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
- // it was decided that waiting to execute ALL scripts makes sense since nested ones have to wait anyway so this test case is changed, see #1959
- $("#main").html("