3 test("Basic requirements", function() {
5 ok( Array.prototype.push, "Array.push()" );
6 ok( Function.prototype.apply, "Function.apply()" );
7 ok( document.getElementById, "getElementById" );
8 ok( document.getElementsByTagName, "getElementsByTagName" );
9 ok( RegExp, "RegExp" );
10 ok( jQuery, "jQuery" );
14 test("jQuery()", function() {
17 // Basic constructor's behavior
19 equals( jQuery().length, 0, "jQuery() === jQuery([])" );
20 equals( jQuery(undefined).length, 0, "jQuery(undefined) === jQuery([])" );
21 equals( jQuery(null).length, 0, "jQuery(null) === jQuery([])" );
22 equals( jQuery("").length, 0, "jQuery('') === jQuery([])" );
24 var obj = jQuery("div")
25 equals( jQuery(obj).selector, "div", "jQuery(jQueryObj) == jQueryObj" );
27 // can actually yield more than one, when iframes are included, the window is an array as well
28 equals( 1, jQuery(window).length, "Correct number of elements generated for jQuery(window)" );
31 var main = jQuery("#main");
32 same( jQuery("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
35 // disabled since this test was doing nothing. i tried to fix it but i'm not sure
36 // what the expected behavior should even be. FF returns "\n" for the text node
37 // make sure this is handled
38 var crlfContainer = jQuery('<p>\r\n</p>');
39 var x = crlfContainer.contents().get(0).nodeValue;
40 equals( x, what???, "Check for \\r and \\n in jQuery()" );
43 /* // Disabled until we add this functionality in
46 jQuery("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
50 ok( pass, "jQuery('<tag>') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
52 var code = jQuery("<code/>");
53 equals( code.length, 1, "Correct number of elements generated for code" );
54 equals( code.parent().length, 0, "Make sure that the generated HTML has no parent." );
55 var img = jQuery("<img/>");
56 equals( img.length, 1, "Correct number of elements generated for img" );
57 equals( img.parent().length, 0, "Make sure that the generated HTML has no parent." );
58 var div = jQuery("<div/><hr/><code/><b/>");
59 equals( div.length, 4, "Correct number of elements generated for div hr code b" );
60 equals( div.parent().length, 0, "Make sure that the generated HTML has no parent." );
62 equals( jQuery([1,2,3]).get(1), 2, "Test passing an array to the factory" );
64 equals( jQuery(document.body).get(0), jQuery('body').get(0), "Test passing an html node to the factory" );
66 var elem = jQuery("<div/>", {
68 css: { paddingLeft:1, paddingRight:1 },
74 equals( elem[0].style.width, '10px', 'jQuery() quick setter width');
75 equals( elem[0].style.paddingLeft, '1px', 'jQuery quick setter css');
76 equals( elem[0].style.paddingRight, '1px', 'jQuery quick setter css');
77 equals( elem[0].childNodes.length, 1, 'jQuery quick setter text');
78 equals( elem[0].firstChild.nodeValue, "test", 'jQuery quick setter text');
79 equals( elem[0].className, "test2", 'jQuery() quick setter class');
80 equals( elem[0].id, "test3", 'jQuery() quick setter id');
83 test("selector state", function() {
88 test = jQuery(undefined);
89 equals( test.selector, "", "Empty jQuery Selector" );
90 equals( test.context, undefined, "Empty jQuery Context" );
92 test = jQuery(document);
93 equals( test.selector, "", "Document Selector" );
94 equals( test.context, document, "Document Context" );
96 test = jQuery(document.body);
97 equals( test.selector, "", "Body Selector" );
98 equals( test.context, document.body, "Body Context" );
100 test = jQuery("#main");
101 equals( test.selector, "#main", "#main Selector" );
102 equals( test.context, document, "#main Context" );
104 test = jQuery("#notfoundnono");
105 equals( test.selector, "#notfoundnono", "#notfoundnono Selector" );
106 equals( test.context, document, "#notfoundnono Context" );
108 test = jQuery("#main", document);
109 equals( test.selector, "#main", "#main Selector" );
110 equals( test.context, document, "#main Context" );
112 test = jQuery("#main", document.body);
113 equals( test.selector, "#main", "#main Selector" );
114 equals( test.context, document.body, "#main Context" );
118 equals( test.selector, "#main", "#main Selector" );
119 equals( test.context, document.body, "#main Context" );
121 test = jQuery(document.body).find("#main");
122 equals( test.selector, "#main", "#main find Selector" );
123 equals( test.context, document.body, "#main find Context" );
125 test = jQuery("#main").filter("div");
126 equals( test.selector, "#main.filter(div)", "#main filter Selector" );
127 equals( test.context, document, "#main filter Context" );
129 test = jQuery("#main").not("div");
130 equals( test.selector, "#main.not(div)", "#main not Selector" );
131 equals( test.context, document, "#main not Context" );
133 test = jQuery("#main").filter("div").not("div");
134 equals( test.selector, "#main.filter(div).not(div)", "#main filter, not Selector" );
135 equals( test.context, document, "#main filter, not Context" );
137 test = jQuery("#main").filter("div").not("div").end();
138 equals( test.selector, "#main.filter(div)", "#main filter, not, end Selector" );
139 equals( test.context, document, "#main filter, not, end Context" );
141 test = jQuery("#main").parent("body");
142 equals( test.selector, "#main.parent(body)", "#main parent Selector" );
143 equals( test.context, document, "#main parent Context" );
145 test = jQuery("#main").eq(0);
146 equals( test.selector, "#main.slice(0,1)", "#main eq Selector" );
147 equals( test.context, document, "#main eq Context" );
151 jQuery(d).appendTo(jQuery(d)).selector,
152 jQuery(d).appendTo(d).selector,
153 "manipulation methods make same selector for jQuery objects"
157 test("browser", function() {
161 "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)": "6.0",
162 "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",
164 * "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",
166 //Browsers with Gecko engine
168 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915" : "1.7.12",
170 "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",
172 "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20070321 Netscape/8.1.3" : "1.7.5",
174 "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",
176 "Opera/9.20 (X11; Linux x86_64; U; en)": "9.20",
177 "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; en) Opera 9.20" : "9.20",
178 "Mozilla/5.0 (Windows NT 5.1; U; pl; rv:1.8.0) Gecko/20060728 Firefox/1.5.0 Opera 9.20": "9.20",
180 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/418.9 (KHTML, like Gecko) Safari/419.3": "418.9",
181 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418.8 (KHTML, like Gecko) Safari/419.3" : "418.8",
182 "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; sv-se) AppleWebKit/312.8 (KHTML, like Gecko) Safari/312.5": "312.8",
183 //Other user agent string
184 "Other browser's user agent 1.0":null
186 for (var i in browsers) {
187 var v = i.toLowerCase().match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ); // RegEx from Core jQuery.browser.version check
188 var version = v ? v[1] : null;
189 equals( version, browsers[i], "Checking UA string" );
193 test("noConflict", function() {
198 equals( jQuery, jQuery.noConflict(), "noConflict returned the jQuery object" );
199 equals( jQuery, $$, "Make sure jQuery wasn't touched." );
200 equals( $, original$, "Make sure $ was reverted." );
204 equals( jQuery.noConflict(true), $$, "noConflict returned the jQuery object" );
205 equals( jQuery, originaljQuery, "Make sure jQuery was reverted." );
206 equals( $, original$, "Make sure $ was reverted." );
211 test("trim", function() {
214 var nbsp = String.fromCharCode(160);
216 equals( jQuery.trim("hello "), "hello", "trailing space" );
217 equals( jQuery.trim(" hello"), "hello", "leading space" );
218 equals( jQuery.trim(" hello "), "hello", "space on both sides" );
219 equals( jQuery.trim(" " + nbsp + "hello " + nbsp + " "), "hello", " " );
222 test("isPlainObject", function() {
227 // The use case that we want to match
228 ok(jQuery.isPlainObject({}), "{}");
230 // Not objects shouldn't be matched
231 ok(!jQuery.isPlainObject(""), "string");
232 ok(!jQuery.isPlainObject(0) && !jQuery.isPlainObject(1), "number");
233 ok(!jQuery.isPlainObject(true) && !jQuery.isPlainObject(false), "boolean");
234 ok(!jQuery.isPlainObject(null), "null");
235 ok(!jQuery.isPlainObject(undefined), "undefined");
237 // Arrays shouldn't be matched
238 ok(!jQuery.isPlainObject([]), "array");
240 // Instantiated objects shouldn't be matched
241 ok(!jQuery.isPlainObject(new Date), "new Date");
243 var fn = function(){};
245 // Functions shouldn't be matched
246 ok(!jQuery.isPlainObject(fn), "fn");
248 // Again, instantiated objects shouldn't be matched
249 ok(!jQuery.isPlainObject(new fn), "new fn (no methods)");
251 // Makes the function a little more realistic
252 // (and harder to detect, incidentally)
253 fn.prototype = {someMethod: function(){}};
255 // Again, instantiated objects shouldn't be matched
256 ok(!jQuery.isPlainObject(new fn), "new fn");
259 ok(!jQuery.isPlainObject(document.createElement("div")), "DOM Element");
262 ok(!jQuery.isPlainObject(window), "window");
264 var iframe = document.createElement("iframe");
265 document.body.appendChild(iframe);
267 window.iframeDone = function(otherObject){
268 // Objects from other windows should be matched
269 ok(jQuery.isPlainObject(new otherObject), "new otherObject");
270 document.body.removeChild( iframe );
274 var doc = iframe.contentDocument || iframe.contentWindow.document;
276 doc.write("<body onload='window.top.iframeDone(Object);'>");
280 test("isFunction", function() {
283 // Make sure that false values return false
284 ok( !jQuery.isFunction(), "No Value" );
285 ok( !jQuery.isFunction( null ), "null Value" );
286 ok( !jQuery.isFunction( undefined ), "undefined Value" );
287 ok( !jQuery.isFunction( "" ), "Empty String Value" );
288 ok( !jQuery.isFunction( 0 ), "0 Value" );
291 // Safari uses "(Internal Function)"
292 ok( jQuery.isFunction(String), "String Function("+String+")" );
293 ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
294 ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
295 ok( jQuery.isFunction(Function), "Function Function("+Function+")" );
297 // When stringified, this could be misinterpreted
298 var mystr = "function";
299 ok( !jQuery.isFunction(mystr), "Function String" );
301 // When stringified, this could be misinterpreted
302 var myarr = [ "function" ];
303 ok( !jQuery.isFunction(myarr), "Function Array" );
305 // When stringified, this could be misinterpreted
306 var myfunction = { "function": "test" };
307 ok( !jQuery.isFunction(myfunction), "Function Object" );
309 // Make sure normal functions still work
310 var fn = function(){};
311 ok( jQuery.isFunction(fn), "Normal Function" );
313 var obj = document.createElement("object");
315 // Firefox says this is a function
316 ok( !jQuery.isFunction(obj), "Object Element" );
318 // IE says this is an object
319 // Since 1.3, this isn't supported (#2968)
320 //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
322 var nodes = document.body.childNodes;
324 // Safari says this is a function
325 ok( !jQuery.isFunction(nodes), "childNodes Property" );
327 var first = document.body.firstChild;
329 // Normal elements are reported ok everywhere
330 ok( !jQuery.isFunction(first), "A normal DOM Element" );
332 var input = document.createElement("input");
334 document.body.appendChild( input );
336 // IE says this is an object
337 // Since 1.3, this isn't supported (#2968)
338 //ok( jQuery.isFunction(input.focus), "A default function property" );
340 document.body.removeChild( input );
342 var a = document.createElement("a");
343 a.href = "some-function";
344 document.body.appendChild( a );
346 // This serializes with the word 'function' in it
347 ok( !jQuery.isFunction(a), "Anchor Element" );
349 document.body.removeChild( a );
351 // Recursive function calls have lengths and array-like properties
352 function callme(callback){
353 function fn(response){
357 ok( jQuery.isFunction(fn), "Recursive Function Call" );
359 fn({ some: "data" });
363 callme(function(){});
367 test("isXMLDoc - HTML", function() {
370 ok( !jQuery.isXMLDoc( document ), "HTML document" );
371 ok( !jQuery.isXMLDoc( document.documentElement ), "HTML documentElement" );
372 ok( !jQuery.isXMLDoc( document.body ), "HTML Body Element" );
374 var iframe = document.createElement("iframe");
375 document.body.appendChild( iframe );
378 var body = jQuery(iframe).contents()[0];
379 ok( !jQuery.isXMLDoc( body ), "Iframe body element" );
381 ok( false, "Iframe body element exception" );
384 document.body.removeChild( iframe );
388 test("isXMLDoc - XML", function() {
391 jQuery.get('data/dashboard.xml', function(xml) {
392 ok( jQuery.isXMLDoc( xml ), "XML document" );
393 ok( jQuery.isXMLDoc( xml.documentElement ), "XML documentElement" );
394 ok( jQuery.isXMLDoc( jQuery("tab", xml)[0] ), "XML Tab Element" );
400 test("jQuery('html')", function() {
405 var s = jQuery("<script>jQuery.foo='test';</script>")[0];
406 ok( s, "Creating a script" );
407 ok( !jQuery.foo, "Make sure the script wasn't executed prematurely" );
408 jQuery("body").append("<script>jQuery.foo='test';</script>");
409 ok( jQuery.foo, "Executing a scripts contents in the right context" );
411 // Test multi-line HTML
412 var div = jQuery("<div>\r\nsome text\n<p>some p</p>\nmore text\r\n</div>")[0];
413 equals( div.nodeName.toUpperCase(), "DIV", "Make sure we're getting a div." );
414 equals( div.firstChild.nodeType, 3, "Text node." );
415 equals( div.lastChild.nodeType, 3, "Text node." );
416 equals( div.childNodes[1].nodeType, 1, "Paragraph." );
417 equals( div.childNodes[1].firstChild.nodeType, 3, "Paragraph text." );
420 ok( jQuery("<link rel='stylesheet'/>")[0], "Creating a link" );
422 ok( !jQuery("<script/>")[0].parentNode, "Create a script" );
424 ok( jQuery("<input/>").attr("type", "hidden"), "Create an input and set the type." );
426 var j = jQuery("<span>hi</span> there <!-- mon ami -->");
427 ok( j.length >= 2, "Check node,textnode,comment creation (some browsers delete comments)" );
429 ok( !jQuery("<option>test</option>")[0].selected, "Make sure that options are auto-selected #2050" );
431 ok( jQuery("<div></div>")[0], "Create a div with closing tag." );
432 ok( jQuery("<table></table>")[0], "Create a table with closing tag." );
435 test("jQuery('html', context)", function() {
438 var $div = jQuery("<div/>")[0];
439 var $span = jQuery("<span/>", $div);
440 equals($span.length, 1, "Verify a span created with a div context works, #1763");
444 test("jQuery(selector, xml).text(str) - Loaded via XML document", function() {
447 jQuery.get('data/dashboard.xml', function(xml) {
448 // tests for #1419 where IE was a problem
449 var tab = jQuery("tab", xml).eq(0);
450 equals( tab.text(), "blabla", "Verify initial text correct" );
452 equals( tab.text(), "newtext", "Verify new text correct" );
458 test("end()", function() {
460 equals( 'Yahoo', jQuery('#yahoo').parent().end().text(), 'Check for end' );
461 ok( jQuery('#yahoo').end(), 'Check for end with nothing to end' );
463 var x = jQuery('#yahoo');
465 equals( 'Yahoo', jQuery('#yahoo').text(), 'Check for non-destructive behaviour' );
468 test("length", function() {
470 equals( jQuery("p").length, 6, "Get Number of Elements Found" );
473 test("size()", function() {
475 equals( jQuery("p").size(), 6, "Get Number of Elements Found" );
478 test("get()", function() {
480 same( jQuery("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
483 test("toArray()", function() {
485 same( jQuery("p").toArray(),
486 q("firstp","ap","sndp","en","sap","first"),
487 "Convert jQuery object to an Array" )
490 test("get(Number)", function() {
492 equals( jQuery("p").get(0), document.getElementById("firstp"), "Get A Single Element" );
495 test("get(-Number)",function() {
497 equals( jQuery("p").get(-1),
498 document.getElementById("first"),
499 "Get a single element with negative index" )
502 test("add(String|Element|Array|undefined)", function() {
504 same( jQuery("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
505 same( jQuery("#sndp").add( jQuery("#en")[0] ).add( jQuery("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
506 ok( jQuery([]).add(jQuery("#form")[0].elements).length >= 13, "Check elements from array" );
508 // For the time being, we're discontinuing support for jQuery(form.elements) since it's ambiguous in IE
509 // use jQuery([]).add(form.elements) instead.
510 //equals( jQuery([]).add(jQuery("#form")[0].elements).length, jQuery(jQuery("#form")[0].elements).length, "Array in constructor must equals array in add()" );
512 var tmp = jQuery("<div/>");
514 var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp));
515 equals( x[0].id, "x1", "Check on-the-fly element1" );
516 equals( x[1].id, "x2", "Check on-the-fly element2" );
518 var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>").appendTo(tmp)[0]).add(jQuery("<p id='x2'>xxx</p>").appendTo(tmp)[0]);
519 equals( x[0].id, "x1", "Check on-the-fly element1" );
520 equals( x[1].id, "x2", "Check on-the-fly element2" );
522 var x = jQuery([]).add(jQuery("<p id='x1'>xxx</p>")).add(jQuery("<p id='x2'>xxx</p>"));
523 equals( x[0].id, "x1", "Check on-the-fly element1" );
524 equals( x[1].id, "x2", "Check on-the-fly element2" );
526 var x = jQuery([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
527 equals( x[0].id, "x1", "Check on-the-fly element1" );
528 equals( x[1].id, "x2", "Check on-the-fly element2" );
531 equals( jQuery([]).add(notDefined).length, 0, "Check that undefined adds nothing" );
534 equals( jQuery([]).add([window,document,document.body,document]).length, 3, "Pass an array" );
535 equals( jQuery(document).add(document).length, 1, "Check duplicated elements" );
536 equals( jQuery(window).add(window).length, 1, "Check duplicated elements using the window" );
537 ok( jQuery([]).add( document.getElementById('form') ).length >= 13, "Add a form (adds the elements)" );
540 test("add(String, Context)", function() {
543 equals( jQuery(document).add("#form").length, 2, "Make sure that using regular context document still works." );
544 equals( jQuery(document.body).add("#form").length, 2, "Using a body context." );
545 equals( jQuery(document.body).add("#html").length, 1, "Using a body context." );
547 equals( jQuery(document).add("#form", document).length, 2, "Use a passed in document context." );
548 equals( jQuery(document).add("#form", document.body).length, 2, "Use a passed in body context." );
549 equals( jQuery(document).add("#html", document.body).length, 1, "Use a passed in body context." );
552 test("each(Function)", function() {
554 var div = jQuery("div");
555 div.each(function(){this.foo = 'zoo';});
557 for ( var i = 0; i < div.size(); i++ ) {
558 if ( div.get(i).foo != "zoo" ) pass = false;
560 ok( pass, "Execute a function, Relative" );
563 test("slice()", function() {
566 var $links = jQuery("#ap a");
568 same( $links.slice(1,2).get(), q("groups"), "slice(1,2)" );
569 same( $links.slice(1).get(), q("groups", "anchor1", "mark"), "slice(1)" );
570 same( $links.slice(0,3).get(), q("google", "groups", "anchor1"), "slice(0,3)" );
571 same( $links.slice(-1).get(), q("mark"), "slice(-1)" );
573 same( $links.eq(1).get(), q("groups"), "eq(1)" );
574 same( $links.eq('2').get(), q("anchor1"), "eq('2')" );
575 same( $links.eq(-1).get(), q("mark"), "eq(-1)" );
578 test("first()/last()", function() {
581 var $links = jQuery("#ap a"), $none = jQuery("asdf");
583 same( $links.first().get(), q("google"), "first()" );
584 same( $links.last().get(), q("mark"), "last()" );
586 same( $none.first().get(), [], "first() none" );
587 same( $none.last().get(), [], "last() none" );
590 test("map()", function() {
591 expect(2);//expect(6);
594 jQuery("#ap").map(function(){
595 return jQuery(this).find("a").get();
597 q("google", "groups", "anchor1", "mark"),
602 jQuery("#ap > a").map(function(){
603 return this.parentNode;
609 return;//these haven't been accepted yet
612 var keys = jQuery.map( {a:1,b:2}, function( v, k ){
616 equals( keys.join(""), "ab", "Map the keys from a hash to an array" );
618 var values = jQuery.map( {a:1,b:2}, function( v, k ){
622 equals( values.join(""), "12", "Map the values from a hash to an array" );
624 var scripts = document.getElementsByTagName("script");
625 var mapped = jQuery.map( scripts, function( v, k ){
629 equals( mapped.length, scripts.length, "Map an array(-like) to a hash" );
631 var flat = jQuery.map( Array(4), function( v, k ){
632 return k % 2 ? k : [k,k,k];//try mixing array and regular returns
635 equals( flat.join(""), "00012223", "try the new flatten technique(#2616)" );
638 test("jQuery.merge()", function() {
641 var parse = jQuery.merge;
643 same( parse([],[]), [], "Empty arrays" );
645 same( parse([1],[2]), [1,2], "Basic" );
646 same( parse([1,2],[3,4]), [1,2,3,4], "Basic" );
648 same( parse([1,2],[]), [1,2], "Second empty" );
649 same( parse([],[1,2]), [1,2], "First empty" );
651 // Fixed at [5998], #3641
652 same( parse([-2,-1], [0,1,2]), [-2,-1,0,1,2], "Second array including a zero (falsy)");
654 // After fixing #5527
655 same( parse([], [null, undefined]), [null, undefined], "Second array including null and undefined values");
656 same( parse({length:0}, [1,2]), {length:2, 0:1, 1:2}, "First array like");
659 test("jQuery.extend(Object, Object)", function() {
662 var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
663 options = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
664 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
665 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
666 deep1 = { foo: { bar: true } },
667 deep1copy = { foo: { bar: true } },
668 deep2 = { foo: { baz: true }, foo2: document },
669 deep2copy = { foo: { baz: true }, foo2: document },
670 deepmerged = { foo: { bar: true, baz: true }, foo2: document };
672 jQuery.extend(settings, options);
673 same( settings, merged, "Check if extended: settings must be extended" );
674 same( options, optionsCopy, "Check if not modified: options must not be modified" );
676 jQuery.extend(settings, null, options);
677 same( settings, merged, "Check if extended: settings must be extended" );
678 same( options, optionsCopy, "Check if not modified: options must not be modified" );
680 jQuery.extend(true, deep1, deep2);
681 same( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
682 same( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
683 equals( deep1.foo2, document, "Make sure that a deep clone was not attempted on the document" );
686 var optionsWithLength = { foo: { length: -1 } };
687 jQuery.extend(true, empty, optionsWithLength);
688 same( empty.foo, optionsWithLength.foo, "The length property must copy correctly" );
691 var optionsWithDate = { foo: { date: new Date } };
692 jQuery.extend(true, empty, optionsWithDate);
693 same( empty.foo, optionsWithDate.foo, "Dates copy correctly" );
695 var myKlass = function() {};
696 var customObject = new myKlass();
697 var optionsWithCustomObject = { foo: { date: customObject } };
699 jQuery.extend(true, empty, optionsWithCustomObject);
700 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly (no methods)" );
702 // Makes the class a little more realistic
703 myKlass.prototype = { someMethod: function(){} };
705 jQuery.extend(true, empty, optionsWithCustomObject);
706 ok( empty.foo && empty.foo.date === customObject, "Custom objects copy correctly" );
708 var ret = jQuery.extend(true, { foo: 4 }, { foo: new Number(5) } );
709 ok( ret.foo == 5, "Wrapped numbers copy correctly" );
712 nullUndef = jQuery.extend({}, options, { xnumber2: null });
713 ok( nullUndef.xnumber2 === null, "Check to make sure null values are copied");
715 nullUndef = jQuery.extend({}, options, { xnumber2: undefined });
716 ok( nullUndef.xnumber2 === options.xnumber2, "Check to make sure undefined values are not copied");
718 nullUndef = jQuery.extend({}, options, { xnumber0: null });
719 ok( nullUndef.xnumber0 === null, "Check to make sure null values are inserted");
722 var recursive = { foo:target, bar:5 };
723 jQuery.extend(true, target, recursive);
724 same( target, { bar:5 }, "Check to make sure a recursive obj doesn't go never-ending loop by not copying it over" );
726 var ret = jQuery.extend(true, { foo: [] }, { foo: [0] } ); // 1907
727 equals( ret.foo.length, 1, "Check to make sure a value with coersion 'false' copies over when necessary to fix #1907" );
729 var ret = jQuery.extend(true, { foo: "1,2,3" }, { foo: [1, 2, 3] } );
730 ok( typeof ret.foo != "string", "Check to make sure values equal with coersion (but not actually equal) overwrite correctly" );
732 var ret = jQuery.extend(true, { foo:"bar" }, { foo:null } );
733 ok( typeof ret.foo !== 'undefined', "Make sure a null value doesn't crash with deep extend, for #1908" );
735 var obj = { foo:null };
736 jQuery.extend(true, obj, { foo:"notnull" } );
737 equals( obj.foo, "notnull", "Make sure a null value can be overwritten" );
740 jQuery.extend(func, { key: "value" } );
741 equals( func.key, "value", "Verify a function can be extended" );
743 var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
744 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
745 options1 = { xnumber2: 1, xstring2: "x" },
746 options1Copy = { xnumber2: 1, xstring2: "x" },
747 options2 = { xstring2: "xx", xxx: "newstringx" },
748 options2Copy = { xstring2: "xx", xxx: "newstringx" },
749 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
751 var settings = jQuery.extend({}, defaults, options1, options2);
752 same( settings, merged2, "Check if extended: settings must be extended" );
753 same( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
754 same( options1, options1Copy, "Check if not modified: options1 must not be modified" );
755 same( options2, options2Copy, "Check if not modified: options2 must not be modified" );
758 test("jQuery.each(Object,Function)", function() {
760 jQuery.each( [0,1,2], function(i, n){
761 equals( i, n, "Check array iteration" );
764 jQuery.each( [5,6,7], function(i, n){
765 equals( i, n - 5, "Check array iteration" );
768 jQuery.each( { name: "name", lang: "lang" }, function(i, n){
769 equals( i, n, "Check object iteration" );
773 jQuery.each([1,2,3], function(i,v){ total += v; });
774 equals( total, 6, "Looping over an array" );
776 jQuery.each([1,2,3], function(i,v){ total += v; if ( i == 1 ) return false; });
777 equals( total, 3, "Looping over an array, with break" );
779 jQuery.each({"a":1,"b":2,"c":3}, function(i,v){ total += v; });
780 equals( total, 6, "Looping over an object" );
782 jQuery.each({"a":3,"b":3,"c":3}, function(i,v){ total += v; return false; });
783 equals( total, 3, "Looping over an object, with break" );
785 var f = function(){};
787 jQuery.each(f, function(i){
790 equals( "baz", f.foo, "Loop over a function" );
793 test("jQuery.makeArray", function(){
796 equals( jQuery.makeArray(jQuery('html>*'))[0].nodeName.toUpperCase(), "HEAD", "Pass makeArray a jQuery object" );
798 equals( jQuery.makeArray(document.getElementsByName("PWD")).slice(0,1)[0].name, "PWD", "Pass makeArray a nodelist" );
800 equals( (function(){ return jQuery.makeArray(arguments); })(1,2).join(""), "12", "Pass makeArray an arguments array" );
802 equals( jQuery.makeArray([1,2,3]).join(""), "123", "Pass makeArray a real array" );
804 equals( jQuery.makeArray().length, 0, "Pass nothing to makeArray and expect an empty array" );
806 equals( jQuery.makeArray( 0 )[0], 0 , "Pass makeArray a number" );
808 equals( jQuery.makeArray( "foo" )[0], "foo", "Pass makeArray a string" );
810 equals( jQuery.makeArray( true )[0].constructor, Boolean, "Pass makeArray a boolean" );
812 equals( jQuery.makeArray( document.createElement("div") )[0].nodeName.toUpperCase(), "DIV", "Pass makeArray a single node" );
814 equals( jQuery.makeArray( {length:2, 0:"a", 1:"b"} ).join(""), "ab", "Pass makeArray an array like map (with length)" );
816 ok( !!jQuery.makeArray( document.documentElement.childNodes ).slice(0,1)[0].nodeName, "Pass makeArray a childNodes array" );
818 // function, is tricky as it has length
819 equals( jQuery.makeArray( function(){ return 1;} )[0](), 1, "Pass makeArray a function" );
821 //window, also has length
822 equals( jQuery.makeArray(window)[0], window, "Pass makeArray the window" );
824 equals( jQuery.makeArray(/a/)[0].constructor, RegExp, "Pass makeArray a regex" );
826 ok( jQuery.makeArray(document.getElementById('form')).length >= 13, "Pass makeArray a form (treat as elements)" );
829 same( jQuery.makeArray({'length': '0'}), [], "Make sure object is coerced properly.");
830 same( jQuery.makeArray({'length': '5'}), [], "Make sure object is coerced properly.");
833 test("jQuery.isEmptyObject", function(){
836 equals(true, jQuery.isEmptyObject({}), "isEmptyObject on empty object literal" );
837 equals(false, jQuery.isEmptyObject({a:1}), "isEmptyObject on non-empty object literal" );
840 // equals(true, jQuery.isEmptyObject(null), "isEmptyObject on null" );