Complete overhaul of the Ajax test suite, it's now passing in all browsers. In order...
[jquery.git] / src / jquery / coreTest.js
1 module("core");
2
3 test("Basic requirements", function() {
4         expect(7);
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" );
11         ok( $, "$()" );
12 });
13
14 test("$()", function() {
15         expect(2);
16         
17         var main = $("#main");
18         isSet( $("div p", main).get(), q("sndp", "en", "sap"), "Basic selector with jQuery object as context" );
19         
20         // make sure this is handled
21         $('<p>\r\n</p>');
22         ok( true, "Check for \\r and \\n in jQuery()" );
23         
24         /* // Disabled until we add this functionality in
25         var pass = true;
26         try {
27                 $("<div>Testing</div>").appendTo(document.getElementById("iframe").contentDocument.body);
28         } catch(e){
29                 pass = false;
30         }
31         ok( pass, "$('&lt;tag&gt;') needs optional document parameter to ease cross-frame DOM wrangling, see #968" );*/
32 });
33
34 test("isFunction", function() {
35         expect(21);
36
37         // Make sure that false values return false
38         ok( !jQuery.isFunction(), "No Value" );
39         ok( !jQuery.isFunction( null ), "null Value" );
40         ok( !jQuery.isFunction( undefined ), "undefined Value" );
41         ok( !jQuery.isFunction( "" ), "Empty String Value" );
42         ok( !jQuery.isFunction( 0 ), "0 Value" );
43
44         // Check built-ins
45         // Safari uses "(Internal Function)"
46         ok( jQuery.isFunction(String), "String Function" );
47         ok( jQuery.isFunction(Array), "Array Function" );
48         ok( jQuery.isFunction(Object), "Object Function" );
49         ok( jQuery.isFunction(Function), "Function Function" );
50
51         // When stringified, this could be misinterpreted
52         var mystr = "function";
53         ok( !jQuery.isFunction(mystr), "Function String" );
54
55         // When stringified, this could be misinterpreted
56         var myarr = [ "function" ];
57         ok( !jQuery.isFunction(myarr), "Function Array" );
58
59         // When stringified, this could be misinterpreted
60         var myfunction = { "function": "test" };
61         ok( !jQuery.isFunction(myfunction), "Function Object" );
62
63         // Make sure normal functions still work
64         var fn = function(){};
65         ok( jQuery.isFunction(fn), "Normal Function" );
66
67         var obj = document.createElement("object");
68
69         // Firefox says this is a function
70         ok( !jQuery.isFunction(obj), "Object Element" );
71
72         // IE says this is an object
73         ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );
74
75         var nodes = document.body.childNodes;
76
77         // Safari says this is a function
78         ok( !jQuery.isFunction(nodes), "childNodes Property" );
79
80         var first = document.body.firstChild;
81         
82         // Normal elements are reported ok everywhere
83         ok( !jQuery.isFunction(first), "A normal DOM Element" );
84
85         var input = document.createElement("input");
86         input.type = "text";
87         document.body.appendChild( input );
88
89         // IE says this is an object
90         ok( jQuery.isFunction(input.focus), "A default function property" );
91
92         document.body.removeChild( input );
93
94         var a = document.createElement("a");
95         a.href = "some-function";
96         document.body.appendChild( a );
97
98         // This serializes with the word 'function' in it
99         ok( !jQuery.isFunction(a), "Anchor Element" );
100
101         document.body.removeChild( a );
102
103         // Recursive function calls have lengths and array-like properties
104         function callme(callback){
105                 function fn(response){
106                         callback(response);
107                 }
108
109                 ok( jQuery.isFunction(fn), "Recursive Function Call" );
110
111         fn({ some: "data" });
112         };
113
114         callme(function(){
115         callme(function(){});
116         });
117 });
118
119 test("$('html')", function() {
120         expect(2);
121         
122         reset();
123         ok( $("<script>var foo='test';</script>")[0], "Creating a script" );
124         
125         reset();
126         ok( $("<link rel='stylesheet'/>")[0], "Creating a link" );
127         
128         reset();
129 });
130
131 test("length", function() {
132         expect(1);
133         ok( $("p").length == 6, "Get Number of Elements Found" );
134 });
135
136 test("size()", function() {
137         expect(1);
138         ok( $("p").size() == 6, "Get Number of Elements Found" );
139 });
140
141 test("get()", function() {
142         expect(1);
143         isSet( $("p").get(), q("firstp","ap","sndp","en","sap","first"), "Get All Elements" );
144 });
145
146 test("get(Number)", function() {
147         expect(1);
148         ok( $("p").get(0) == document.getElementById("firstp"), "Get A Single Element" );
149 });
150
151 test("add(String|Element|Array)", function() {
152         expect(7);
153         isSet( $("#sndp").add("#en").add("#sap").get(), q("sndp", "en", "sap"), "Check elements from document" );
154         isSet( $("#sndp").add( $("#en")[0] ).add( $("#sap") ).get(), q("sndp", "en", "sap"), "Check elements from document" );
155         ok( $([]).add($("#form")[0].elements).length >= 13, "Check elements from array" );
156         
157         var x = $([]).add($("<p id='x1'>xxx</p>")).add($("<p id='x2'>xxx</p>"));
158         ok( x[0].id == "x1", "Check on-the-fly element1" );
159         ok( x[1].id == "x2", "Check on-the-fly element2" );
160         
161         var x = $([]).add("<p id='x1'>xxx</p>").add("<p id='x2'>xxx</p>");
162         ok( x[0].id == "x1", "Check on-the-fly element1" );
163         ok( x[1].id == "x2", "Check on-the-fly element2" );
164 });
165
166 test("each(Function)", function() {
167         expect(1);
168         var div = $("div");
169         div.each(function(){this.foo = 'zoo';});
170         var pass = true;
171         for ( var i = 0; i < div.size(); i++ ) {
172           if ( div.get(i).foo != "zoo" ) pass = false;
173         }
174         ok( pass, "Execute a function, Relative" );
175 });
176
177 test("index(Object)", function() {
178         expect(8);
179         ok( $([window, document]).index(window) == 0, "Check for index of elements" );
180         ok( $([window, document]).index(document) == 1, "Check for index of elements" );
181         var inputElements = $('#radio1,#radio2,#check1,#check2');
182         ok( inputElements.index(document.getElementById('radio1')) == 0, "Check for index of elements" );
183         ok( inputElements.index(document.getElementById('radio2')) == 1, "Check for index of elements" );
184         ok( inputElements.index(document.getElementById('check1')) == 2, "Check for index of elements" );
185         ok( inputElements.index(document.getElementById('check2')) == 3, "Check for index of elements" );
186         ok( inputElements.index(window) == -1, "Check for not found index" );
187         ok( inputElements.index(document) == -1, "Check for not found index" );
188 });
189
190 test("attr(String)", function() {
191         expect(13);
192         ok( $('#text1').attr('value') == "Test", 'Check for value attribute' );
193         ok( $('#text1').attr('type') == "text", 'Check for type attribute' );
194         ok( $('#radio1').attr('type') == "radio", 'Check for type attribute' );
195         ok( $('#check1').attr('type') == "checkbox", 'Check for type attribute' );
196         ok( $('#simon1').attr('rel') == "bookmark", 'Check for rel attribute' );
197         ok( $('#google').attr('title') == "Google!", 'Check for title attribute' );
198         ok( $('#mark').attr('hreflang') == "en", 'Check for hreflang attribute' );
199         ok( $('#en').attr('lang') == "en", 'Check for lang attribute' );
200         ok( $('#simon').attr('class') == "blog link", 'Check for class attribute' );
201         ok( $('#name').attr('name') == "name", 'Check for name attribute' );
202         ok( $('#text1').attr('name') == "action", 'Check for name attribute' );
203         ok( $('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
204         
205         $('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
206         ok( $('#tAnchor5').attr('href') == "#5", 'Check for non-absolute href (an anchor)' );
207 });
208
209 test("attr(String) in XML Files", function() {
210         expect(2);
211         stop();
212         $.get("data/dashboard.xml", function(xml) {
213                 ok( $("locations", xml).attr("class") == "foo", "Check class attribute in XML document" );
214                 ok( $("location", xml).attr("for") == "bar", "Check for attribute in XML document" );
215                 start();
216         });
217 });
218
219 test("attr(String, Function)", function() {
220         expect(2);
221         ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
222         ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
223 });
224
225 test("attr(Hash)", function() {
226         expect(1);
227         var pass = true;
228         $("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
229           if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
230         });
231         ok( pass, "Set Multiple Attributes" );
232 });
233
234 test("attr(String, Object)", function() {
235         expect(8);
236         var div = $("div");
237         div.attr("foo", "bar");
238         var pass = true;
239         for ( var i = 0; i < div.size(); i++ ) {
240           if ( div.get(i).getAttribute('foo') != "bar" ) pass = false;
241         }
242         ok( pass, "Set Attribute" );
243
244         ok( $("#foo").attr({"width": null}), "Try to set an attribute to nothing" );    
245         
246         $("#name").attr('name', 'something');
247         ok( $("#name").attr('name') == 'something', 'Set name attribute' );
248         $("#check2").attr('checked', true);
249         ok( document.getElementById('check2').checked == true, 'Set checked attribute' );
250         $("#check2").attr('checked', false);
251         ok( document.getElementById('check2').checked == false, 'Set checked attribute' );
252         $("#text1").attr('readonly', true);
253         ok( document.getElementById('text1').readOnly == true, 'Set readonly attribute' );
254         $("#text1").attr('readonly', false);
255         ok( document.getElementById('text1').readOnly == false, 'Set readonly attribute' );
256         $("#name").attr('maxlength', '5');
257         ok( document.getElementById('name').maxLength == '5', 'Set maxlength attribute' );
258 });
259
260 test("attr(String, Object) - Loaded via XML document", function() {
261         expect(2);
262         stop();
263         $.get('data/dashboard.xml', function(xml) { 
264                 var titles = [];
265                 $('tab', xml).each(function() {
266                         titles.push($(this).attr('title'));
267                 });
268                 ok( titles[0] == 'Location', 'attr() in XML context: Check first title' );
269                 ok( titles[1] == 'Users', 'attr() in XML context: Check second title' );
270                 start();
271         });
272 });
273
274 test("css(String|Hash)", function() {
275         expect(19);
276         
277         ok( $('#main').css("display") == 'none', 'Check for css property "display"');
278         
279         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
280         $('#foo').css({display: 'none'});
281         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
282         $('#foo').css({display: 'block'});
283         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
284         
285         $('#floatTest').css({styleFloat: 'right'});
286         ok( $('#floatTest').css('styleFloat') == 'right', 'Modified CSS float using "styleFloat": Assert float is right');
287         $('#floatTest').css({cssFloat: 'left'});
288         ok( $('#floatTest').css('cssFloat') == 'left', 'Modified CSS float using "cssFloat": Assert float is left');
289         $('#floatTest').css({'float': 'right'});
290         ok( $('#floatTest').css('float') == 'right', 'Modified CSS float using "float": Assert float is right');
291         $('#floatTest').css({'font-size': '30px'});
292         ok( $('#floatTest').css('font-size') == '30px', 'Modified CSS font-size: Assert font-size is 30px');
293         
294         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
295                 $('#foo').css({opacity: n});
296                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
297                 $('#foo').css({opacity: parseFloat(n)});
298                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
299         });     
300         $('#foo').css({opacity: ''});
301         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
302 });
303
304 test("css(String, Object)", function() {
305         expect(18);
306         ok( $('#foo').is(':visible'), 'Modifying CSS display: Assert element is visible');
307         $('#foo').css('display', 'none');
308         ok( !$('#foo').is(':visible'), 'Modified CSS display: Assert element is hidden');
309         $('#foo').css('display', 'block');
310         ok( $('#foo').is(':visible'), 'Modified CSS display: Assert element is visible');
311         
312         $('#floatTest').css('styleFloat', 'left');
313         ok( $('#floatTest').css('styleFloat') == 'left', 'Modified CSS float using "styleFloat": Assert float is left');
314         $('#floatTest').css('cssFloat', 'right');
315         ok( $('#floatTest').css('cssFloat') == 'right', 'Modified CSS float using "cssFloat": Assert float is right');
316         $('#floatTest').css('float', 'left');
317         ok( $('#floatTest').css('float') == 'left', 'Modified CSS float using "float": Assert float is left');
318         $('#floatTest').css('font-size', '20px');
319         ok( $('#floatTest').css('font-size') == '20px', 'Modified CSS font-size: Assert font-size is 20px');
320         
321         $.each("0,0.25,0.5,0.75,1".split(','), function(i, n) {
322                 $('#foo').css('opacity', n);
323                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a String" );
324                 $('#foo').css('opacity', parseFloat(n));
325                 ok( $('#foo').css('opacity') == parseFloat(n), "Assert opacity is " + parseFloat(n) + " as a Number" );
326         });
327         $('#foo').css('opacity', '');
328         ok( $('#foo').css('opacity') == '1', "Assert opacity is 1 when set to an empty String" );
329 });
330
331 test("text()", function() {
332         expect(1);
333         var expected = "This link has class=\"blog\": Simon Willison's Weblog";
334         ok( $('#sap').text() == expected, 'Check for merged text of more then one element.' );
335 });
336
337 test("wrap(String|Element)", function() {
338         expect(6);
339         var defaultText = 'Try them out:'
340         var result = $('#first').wrap('<div class="red"><span></span></div>').text();
341         ok( defaultText == result, 'Check for wrapping of on-the-fly html' );
342         ok( $('#first').parent().parent().is('.red'), 'Check if wrapper has class "red"' );
343
344         reset();
345         var defaultText = 'Try them out:'
346         var result = $('#first').wrap(document.getElementById('empty')).parent();
347         ok( result.is('ol'), 'Check for element wrapping' );
348         ok( result.text() == defaultText, 'Check for element wrapping' );
349         
350         reset();
351         //stop();
352         $('#check1').click(function() {         
353                 var checkbox = this;            
354                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
355                 $(checkbox).wrap( '<div id="c1" style="display:none;"></div>' );
356                 ok( checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
357                 // use a fade in to check state after this event handler has finished
358                 /*setTimeout(function() {
359                         ok( !checkbox.checked, "Checkbox's state is erased after wrap() action, see #769" );
360                         start();
361                 }, 100);*/
362         }).click();
363 });
364
365 test("append(String|Element|Array&lt;Element&gt;|jQuery)", function() {
366         expect(18);
367         var defaultText = 'Try them out:'
368         var result = $('#first').append('<b>buga</b>');
369         ok( result.text() == defaultText + 'buga', 'Check if text appending works' );
370         ok( $('#select3').append('<option value="appendTest">Append Test</option>').find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
371         
372         reset();
373         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
374         $('#sap').append(document.getElementById('first'));
375         ok( expected == $('#sap').text(), "Check for appending of element" );
376         
377         reset();
378         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
379         $('#sap').append([document.getElementById('first'), document.getElementById('yahoo')]);
380         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
381         
382         reset();
383         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
384         $('#sap').append($("#first, #yahoo"));
385         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
386
387         reset();
388         $("#sap").append( 5 );
389         ok( $("#sap")[0].innerHTML.match( /5$/ ), "Check for appending a number" );
390
391         reset();
392         $("#sap").append( " text with spaces " );
393         ok( $("#sap")[0].innerHTML.match(/ text with spaces $/), "Check for appending text with spaces" );
394
395         reset();
396         ok( $("#sap").append([]), "Check for appending an empty array." );
397         ok( $("#sap").append(""), "Check for appending an empty string." );
398         ok( $("#sap").append(document.getElementsByTagName("foo")), "Check for appending an empty nodelist." );
399         
400         reset();
401         $("#sap").append(document.getElementById('form'));
402         ok( $("#sap>form").size() == 1, "Check for appending a form" );  // Bug #910
403
404         reset();
405         var pass = true;
406         try {
407                 $( $("iframe")[0].contentWindow.document.body ).append("<div>test</div>");
408         } catch(e) {
409                 pass = false;
410         }
411
412         ok( pass, "Test for appending a DOM node to the contents of an IFrame" );
413         
414         reset();
415         $('<fieldset/>').appendTo('#form').append('<legend id="legend">test</legend>');
416         t( 'Append legend', '#legend', ['legend'] );
417         
418         reset();
419         $('#select1').append('<OPTION>Test</OPTION>');
420         ok( $('#select1 option:last').text() == "Test", "Appending &lt;OPTION&gt; (all caps)" );
421         
422         $('#table').append('<colgroup></colgroup>');
423         ok( $('#table colgroup').length, "Append colgroup" );
424         
425         $('#table colgroup').append('<col/>');
426         ok( $('#table colgroup col').length, "Append col" );
427         
428         reset();
429         $('#table').append('<caption></caption>');
430         ok( $('#table caption').length, "Append caption" );
431
432         reset();
433         $('form:last')
434                 .append('<select id="appendSelect1"></select>')
435                 .append('<select id="appendSelect2"><option>Test</option></select>');
436         
437         t( "Append Select", "#appendSelect1, #appendSelect2", ["appendSelect1", "appendSelect2"] );
438 });
439
440 test("appendTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
441         expect(6);
442         var defaultText = 'Try them out:'
443         $('<b>buga</b>').appendTo('#first');
444         ok( $("#first").text() == defaultText + 'buga', 'Check if text appending works' );
445         ok( $('<option value="appendTest">Append Test</option>').appendTo('#select3').parent().find('option:last-child').attr('value') == 'appendTest', 'Appending html options to select element');
446         
447         reset();
448         var expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:";
449         $(document.getElementById('first')).appendTo('#sap');
450         ok( expected == $('#sap').text(), "Check for appending of element" );
451         
452         reset();
453         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
454         $([document.getElementById('first'), document.getElementById('yahoo')]).appendTo('#sap');
455         ok( expected == $('#sap').text(), "Check for appending of array of elements" );
456         
457         reset();
458         expected = "This link has class=\"blog\": Simon Willison's WeblogTry them out:Yahoo";
459         $("#first, #yahoo").appendTo('#sap');
460         ok( expected == $('#sap').text(), "Check for appending of jQuery object" );
461         
462         reset();
463         $('#select1').appendTo('#foo');
464         t( 'Append select', '#foo select', ['select1'] );
465 });
466
467 test("prepend(String|Element|Array&lt;Element&gt;|jQuery)", function() {
468         expect(5);
469         var defaultText = 'Try them out:'
470         var result = $('#first').prepend('<b>buga</b>');
471         ok( result.text() == 'buga' + defaultText, 'Check if text prepending works' );
472         ok( $('#select3').prepend('<option value="prependTest">Prepend Test</option>').find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
473         
474         reset();
475         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
476         $('#sap').prepend(document.getElementById('first'));
477         ok( expected == $('#sap').text(), "Check for prepending of element" );
478
479         reset();
480         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
481         $('#sap').prepend([document.getElementById('first'), document.getElementById('yahoo')]);
482         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
483         
484         reset();
485         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
486         $('#sap').prepend($("#first, #yahoo"));
487         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
488 });
489
490 test("prependTo(String|Element|Array&lt;Element&gt;|jQuery)", function() {
491         expect(6);
492         var defaultText = 'Try them out:'
493         $('<b>buga</b>').prependTo('#first');
494         ok( $('#first').text() == 'buga' + defaultText, 'Check if text prepending works' );
495         ok( $('<option value="prependTest">Prepend Test</option>').prependTo('#select3').parent().find('option:first-child').attr('value') == 'prependTest', 'Prepending html options to select element');
496         
497         reset();
498         var expected = "Try them out:This link has class=\"blog\": Simon Willison's Weblog";
499         $(document.getElementById('first')).prependTo('#sap');
500         ok( expected == $('#sap').text(), "Check for prepending of element" );
501
502         reset();
503         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
504         $([document.getElementById('yahoo'), document.getElementById('first')]).prependTo('#sap');
505         ok( expected == $('#sap').text(), "Check for prepending of array of elements" );
506         
507         reset();
508         expected = "Try them out:YahooThis link has class=\"blog\": Simon Willison's Weblog";
509         $("#yahoo, #first").prependTo('#sap');
510         ok( expected == $('#sap').text(), "Check for prepending of jQuery object" );
511         
512         reset();
513         $('<select id="prependSelect1"></select>').prependTo('form:last');
514         $('<select id="prependSelect2"><option>Test</option></select>').prependTo('form:last');
515         
516         t( "Prepend Select", "#prependSelect1, #prependSelect2", ["prependSelect1", "prependSelect2"] );
517 });
518
519 test("before(String|Element|Array&lt;Element&gt;|jQuery)", function() {
520         expect(4);
521         var expected = 'This is a normal link: bugaYahoo';
522         $('#yahoo').before('<b>buga</b>');
523         ok( expected == $('#en').text(), 'Insert String before' );
524         
525         reset();
526         expected = "This is a normal link: Try them out:Yahoo";
527         $('#yahoo').before(document.getElementById('first'));
528         ok( expected == $('#en').text(), "Insert element before" );
529         
530         reset();
531         expected = "This is a normal link: Try them out:diveintomarkYahoo";
532         $('#yahoo').before([document.getElementById('first'), document.getElementById('mark')]);
533         ok( expected == $('#en').text(), "Insert array of elements before" );
534         
535         reset();
536         expected = "This is a normal link: Try them out:diveintomarkYahoo";
537         $('#yahoo').before($("#first, #mark"));
538         ok( expected == $('#en').text(), "Insert jQuery before" );
539 });
540
541 test("insertBefore(String|Element|Array&lt;Element&gt;|jQuery)", function() {
542         expect(4);
543         var expected = 'This is a normal link: bugaYahoo';
544         $('<b>buga</b>').insertBefore('#yahoo');
545         ok( expected == $('#en').text(), 'Insert String before' );
546         
547         reset();
548         expected = "This is a normal link: Try them out:Yahoo";
549         $(document.getElementById('first')).insertBefore('#yahoo');
550         ok( expected == $('#en').text(), "Insert element before" );
551         
552         reset();
553         expected = "This is a normal link: Try them out:diveintomarkYahoo";
554         $([document.getElementById('first'), document.getElementById('mark')]).insertBefore('#yahoo');
555         ok( expected == $('#en').text(), "Insert array of elements before" );
556         
557         reset();
558         expected = "This is a normal link: Try them out:diveintomarkYahoo";
559         $("#first, #mark").insertBefore('#yahoo');
560         ok( expected == $('#en').text(), "Insert jQuery before" );
561 });
562
563 test("after(String|Element|Array&lt;Element&gt;|jQuery)", function() {
564         expect(4);
565         var expected = 'This is a normal link: Yahoobuga';
566         $('#yahoo').after('<b>buga</b>');
567         ok( expected == $('#en').text(), 'Insert String after' );
568         
569         reset();
570         expected = "This is a normal link: YahooTry them out:";
571         $('#yahoo').after(document.getElementById('first'));
572         ok( expected == $('#en').text(), "Insert element after" );
573
574         reset();
575         expected = "This is a normal link: YahooTry them out:diveintomark";
576         $('#yahoo').after([document.getElementById('first'), document.getElementById('mark')]);
577         ok( expected == $('#en').text(), "Insert array of elements after" );
578         
579         reset();
580         expected = "This is a normal link: YahooTry them out:diveintomark";
581         $('#yahoo').after($("#first, #mark"));
582         ok( expected == $('#en').text(), "Insert jQuery after" );
583 });
584
585 test("insertAfter(String|Element|Array&lt;Element&gt;|jQuery)", function() {
586         expect(4);
587         var expected = 'This is a normal link: Yahoobuga';
588         $('<b>buga</b>').insertAfter('#yahoo');
589         ok( expected == $('#en').text(), 'Insert String after' );
590         
591         reset();
592         expected = "This is a normal link: YahooTry them out:";
593         $(document.getElementById('first')).insertAfter('#yahoo');
594         ok( expected == $('#en').text(), "Insert element after" );
595
596         reset();
597         expected = "This is a normal link: YahooTry them out:diveintomark";
598         $([document.getElementById('mark'), document.getElementById('first')]).insertAfter('#yahoo');
599         ok( expected == $('#en').text(), "Insert array of elements after" );
600         
601         reset();
602         expected = "This is a normal link: YahooTry them out:diveintomark";
603         $("#mark, #first").insertAfter('#yahoo');
604         ok( expected == $('#en').text(), "Insert jQuery after" );
605 });
606
607 test("end()", function() {
608         expect(3);
609         ok( 'Yahoo' == $('#yahoo').parent().end().text(), 'Check for end' );
610         ok( $('#yahoo').end(), 'Check for end with nothing to end' );
611         
612         var x = $('#yahoo');
613         x.parent();
614         ok( 'Yahoo' == $('#yahoo').text(), 'Check for non-destructive behaviour' );
615 });
616
617 test("find(String)", function() {
618         expect(1);
619         ok( 'Yahoo' == $('#foo').find('.blogTest').text(), 'Check for find' );
620 });
621
622 test("clone()", function() {
623         expect(3);
624         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Assert text for #en' );
625         var clone = $('#yahoo').clone();
626         ok( 'Try them out:Yahoo' == $('#first').append(clone).text(), 'Check for clone' );
627         ok( 'This is a normal link: Yahoo' == $('#en').text(), 'Reassert text for #en' );
628 });
629
630 test("is(String)", function() {
631         expect(26);
632         ok( $('#form').is('form'), 'Check for element: A form must be a form' );
633         ok( !$('#form').is('div'), 'Check for element: A form is not a div' );
634         ok( $('#mark').is('.blog'), 'Check for class: Expected class "blog"' );
635         ok( !$('#mark').is('.link'), 'Check for class: Did not expect class "link"' );
636         ok( $('#simon').is('.blog.link'), 'Check for multiple classes: Expected classes "blog" and "link"' );
637         ok( !$('#simon').is('.blogTest'), 'Check for multiple classes: Expected classes "blog" and "link", but not "blogTest"' );
638         ok( $('#en').is('[@lang="en"]'), 'Check for attribute: Expected attribute lang to be "en"' );
639         ok( !$('#en').is('[@lang="de"]'), 'Check for attribute: Expected attribute lang to be "en", not "de"' );
640         ok( $('#text1').is('[@type="text"]'), 'Check for attribute: Expected attribute type to be "text"' );
641         ok( !$('#text1').is('[@type="radio"]'), 'Check for attribute: Expected attribute type to be "text", not "radio"' );
642         ok( $('#text2').is(':disabled'), 'Check for pseudoclass: Expected to be disabled' );
643         ok( !$('#text1').is(':disabled'), 'Check for pseudoclass: Expected not disabled' );
644         ok( $('#radio2').is(':checked'), 'Check for pseudoclass: Expected to be checked' );
645         ok( !$('#radio1').is(':checked'), 'Check for pseudoclass: Expected not checked' );
646         ok( $('#foo').is('[p]'), 'Check for child: Expected a child "p" element' );
647         ok( !$('#foo').is('[ul]'), 'Check for child: Did not expect "ul" element' );
648         ok( $('#foo').is('[p][a][code]'), 'Check for childs: Expected "p", "a" and "code" child elements' );
649         ok( !$('#foo').is('[p][a][code][ol]'), 'Check for childs: Expected "p", "a" and "code" child elements, but no "ol"' );
650         ok( !$('#foo').is(0), 'Expected false for an invalid expression - 0' );
651         ok( !$('#foo').is(null), 'Expected false for an invalid expression - null' );
652         ok( !$('#foo').is(''), 'Expected false for an invalid expression - ""' );
653         ok( !$('#foo').is(undefined), 'Expected false for an invalid expression - undefined' );
654         
655         // test is() with comma-seperated expressions
656         ok( $('#en').is('[@lang="en"],[@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
657         ok( $('#en').is('[@lang="de"],[@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
658         ok( $('#en').is('[@lang="en"] , [@lang="de"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
659         ok( $('#en').is('[@lang="de"] , [@lang="en"]'), 'Comma-seperated; Check for lang attribute: Expect en or de' );
660 });
661
662 test("$.extend(Object, Object)", function() {
663         expect(10);
664
665         var settings = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
666                 options =     { xnumber2: 1, xstring2: "x", xxx: "newstring" },
667                 optionsCopy = { xnumber2: 1, xstring2: "x", xxx: "newstring" },
668                 merged = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "x", xxx: "newstring" },
669                 deep1 = { foo: { bar: true } },
670                 deep1copy = { foo: { bar: true } },
671                 deep2 = { foo: { baz: true } },
672                 deep2copy = { foo: { baz: true } },
673                 deepmerged = { foo: { bar: true, baz: true } };
674
675         jQuery.extend(settings, options);
676         isObj( settings, merged, "Check if extended: settings must be extended" );
677         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
678
679         jQuery.extend(settings, null, options);
680         isObj( settings, merged, "Check if extended: settings must be extended" );
681         isObj( options, optionsCopy, "Check if not modified: options must not be modified" );
682
683         jQuery.extend(deep1, deep2);
684         isObj( deep1.foo, deepmerged.foo, "Check if foo: settings must be extended" );
685         isObj( deep2.foo, deep2copy.foo, "Check if not deep2: options must not be modified" );
686
687         var defaults = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
688                 defaultsCopy = { xnumber1: 5, xnumber2: 7, xstring1: "peter", xstring2: "pan" },
689                 options1 =     { xnumber2: 1, xstring2: "x" },
690                 options1Copy = { xnumber2: 1, xstring2: "x" },
691                 options2 =     { xstring2: "xx", xxx: "newstringx" },
692                 options2Copy = { xstring2: "xx", xxx: "newstringx" },
693                 merged2 = { xnumber1: 5, xnumber2: 1, xstring1: "peter", xstring2: "xx", xxx: "newstringx" };
694
695         var settings = jQuery.extend({}, defaults, options1, options2);
696         isObj( settings, merged2, "Check if extended: settings must be extended" );
697         isObj( defaults, defaultsCopy, "Check if not modified: options1 must not be modified" );
698         isObj( options1, options1Copy, "Check if not modified: options1 must not be modified" );
699         isObj( options2, options2Copy, "Check if not modified: options2 must not be modified" );
700 });
701
702 test("val()", function() {
703         expect(2);
704         ok( $("#text1").val() == "Test", "Check for value of input element" );
705         ok( !$("#text1").val() == "", "Check for value of input element" );
706 });
707
708 test("val(String)", function() {
709         expect(2);
710         document.getElementById('text1').value = "bla";
711         ok( $("#text1").val() == "bla", "Check for modified value of input element" );
712         $("#text1").val('test');
713         ok ( document.getElementById('text1').value == "test", "Check for modified (via val(String)) value of input element" );
714 });
715
716 test("html(String)", function() {
717         expect(1);
718         var div = $("div");
719         div.html("<b>test</b>");
720         var pass = true;
721         for ( var i = 0; i < div.size(); i++ ) {
722           if ( div.get(i).childNodes.length == 0 ) pass = false;
723         }
724         ok( pass, "Set HTML" );
725
726         // Ccommented out until we can resolve it       
727         // $("#main").html('<script type="text/javascript">ok( true, "$().html().evalScripts() Evals Scripts Twice in Firefox, see #975" );</script>').evalScripts();
728 });
729
730 test("filter()", function() {
731         expect(4);
732         isSet( $("input").filter(":checked").get(), q("radio2", "check1"), "filter(String)" );
733         isSet( $("p").filter("#ap, #sndp").get(), q("ap", "sndp"), "filter('String, String')" );
734         isSet( $("p").filter("#ap,#sndp").get(), q("ap", "sndp"), "filter('String,String')" );
735         isSet( $("p").filter(function() { return !$("a", this).length }).get(), q("sndp", "first"), "filter(Function)" );
736 });
737
738 test("not()", function() {
739         expect(3);
740         ok( $("#main > p#ap > a").not("#google").length == 2, "not('selector')" );
741         isSet( $("p").not("#ap, #sndp, .result").get(), q("firstp", "en", "sap", "first"), "not('selector, selector')" );
742         isSet( $("p").not($("#ap, #sndp, .result")).get(), q("firstp", "en", "sap", "first"), "not(jQuery)" );
743 });
744
745 test("siblings([String])", function() {
746         expect(5);
747         isSet( $("#en").siblings().get(), q("sndp", "sap"), "Check for siblings" );
748         isSet( $("#sndp").siblings("[code]").get(), q("sap"), "Check for filtered siblings (has code child element)" ); 
749         isSet( $("#sndp").siblings("[a]").get(), q("en", "sap"), "Check for filtered siblings (has anchor child element)" );
750         isSet( $("#foo").siblings("form, b").get(), q("form", "lengthtest", "floatTest"), "Check for multiple filters" );
751         isSet( $("#en, #sndp").siblings().get(), q("sndp", "sap", "en"), "Check for unique results from siblings" );
752 });
753
754 test("children([String])", function() {
755         expect(3);
756         isSet( $("#foo").children().get(), q("sndp", "en", "sap"), "Check for children" );
757         isSet( $("#foo").children("[code]").get(), q("sndp", "sap"), "Check for filtered children" );
758         isSet( $("#foo").children("#en, #sap").get(), q("en", "sap"), "Check for multiple filters" );
759 });
760
761 test("parent([String])", function() {
762         expect(5);
763         ok( $("#groups").parent()[0].id == "ap", "Simple parent check" );
764         ok( $("#groups").parent("p")[0].id == "ap", "Filtered parent check" );
765         ok( $("#groups").parent("div").length == 0, "Filtered parent check, no match" );
766         ok( $("#groups").parent("div, p")[0].id == "ap", "Check for multiple filters" );
767         isSet( $("#en, #sndp").parent().get(), q("foo"), "Check for unique results from parent" );
768 });
769         
770 test("parents([String])", function() {
771         expect(5);
772         ok( $("#groups").parents()[0].id == "ap", "Simple parents check" );
773         ok( $("#groups").parents("p")[0].id == "ap", "Filtered parents check" );
774         ok( $("#groups").parents("div")[0].id == "main", "Filtered parents check2" );
775         isSet( $("#groups").parents("p, div").get(), q("ap", "main"), "Check for multiple filters" );
776         isSet( $("#en, #sndp").parents().get(), q("foo", "main", "dl", "body", "html"), "Check for unique results from parents" );
777 });
778
779 test("next([String])", function() {
780         expect(4);
781         ok( $("#ap").next()[0].id == "foo", "Simple next check" );
782         ok( $("#ap").next("div")[0].id == "foo", "Filtered next check" );
783         ok( $("#ap").next("p").length == 0, "Filtered next check, no match" );
784         ok( $("#ap").next("div, p")[0].id == "foo", "Multiple filters" );
785 });
786         
787 test("prev([String])", function() {
788         expect(4);
789         ok( $("#foo").prev()[0].id == "ap", "Simple prev check" );
790         ok( $("#foo").prev("p")[0].id == "ap", "Filtered prev check" );
791         ok( $("#foo").prev("div").length == 0, "Filtered prev check, no match" );
792         ok( $("#foo").prev("p, div")[0].id == "ap", "Multiple filters" );
793 });
794
795 test("show()", function() {
796         expect(1);
797         var pass = true, div = $("div");
798         div.show().each(function(){
799           if ( this.style.display == "none" ) pass = false;
800         });
801         ok( pass, "Show" );
802 });
803
804 test("addClass(String)", function() {
805         expect(1);
806         var div = $("div");
807         div.addClass("test");
808         var pass = true;
809         for ( var i = 0; i < div.size(); i++ ) {
810          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
811         }
812         ok( pass, "Add Class" );
813 });
814
815 test("removeClass(String) - simple", function() {
816         expect(3);
817         var div = $("div").addClass("test").removeClass("test"),
818                 pass = true;
819         for ( var i = 0; i < div.size(); i++ ) {
820                 if ( div.get(i).className.indexOf("test") != -1 ) pass = false;
821         }
822         ok( pass, "Remove Class" );
823         
824         reset();
825         var div = $("div").addClass("test").addClass("foo").addClass("bar");
826         div.removeClass("test").removeClass("bar").removeClass("foo");
827         var pass = true;
828         for ( var i = 0; i < div.size(); i++ ) {
829          if ( div.get(i).className.match(/test|bar|foo/) ) pass = false;
830         }
831         ok( pass, "Remove multiple classes" );
832         
833         reset();
834         var div = $("div:eq(0)").addClass("test").removeClass("");
835         ok( div.is('.test'), "Empty string passed to removeClass" );
836         
837 });
838
839 test("toggleClass(String)", function() {
840         expect(3);
841         var e = $("#firstp");
842         ok( !e.is(".test"), "Assert class not present" );
843         e.toggleClass("test");
844         ok( e.is(".test"), "Assert class present" ); 
845         e.toggleClass("test");
846         ok( !e.is(".test"), "Assert class not present" );
847 });
848
849 test("removeAttr(String", function() {
850         expect(1);
851         ok( $('#mark').removeAttr("class")[0].className == "", "remove class" );
852 });
853
854 test("text(String)", function() {
855         expect(1);
856         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" );
857 });
858
859 test("$.each(Object,Function)", function() {
860         expect(8);
861         $.each( [0,1,2], function(i, n){
862                 ok( i == n, "Check array iteration" );
863         });
864         
865         $.each( [5,6,7], function(i, n){
866                 ok( i == n - 5, "Check array iteration" );
867         });
868          
869         $.each( { name: "name", lang: "lang" }, function(i, n){
870                 ok( i == n, "Check object iteration" );
871         });
872 });
873
874 test("$.prop", function() {
875         expect(2);
876         var handle = function() { return this.id };
877         ok( $.prop($("#ap")[0], handle) == "ap", "Check with Function argument" );
878         ok( $.prop($("#ap")[0], "value") == "value", "Check with value argument" );
879 });
880
881 test("$.className", function() {
882         expect(6);
883         var x = $("<p>Hi</p>")[0];
884         var c = $.className;
885         c.add(x, "hi");
886         ok( x.className == "hi", "Check single added class" );
887         c.add(x, "foo bar");
888         ok( x.className == "hi foo bar", "Check more added classes" );
889         c.remove(x);
890         ok( x.className == "", "Remove all classes" );
891         c.add(x, "hi foo bar");
892         c.remove(x, "foo");
893         ok( x.className == "hi bar", "Check removal of one class" );
894         ok( c.has(x, "hi"), "Check has1" );
895         ok( c.has(x, "bar"), "Check has2" );
896 });
897
898 test("remove()", function() {
899         expect(4);
900         $("#ap").children().remove();
901         ok( $("#ap").text().length > 10, "Check text is not removed" );
902         ok( $("#ap").children().length == 0, "Check remove" );
903         
904         reset();
905         $("#ap").children().remove("a");
906         ok( $("#ap").text().length > 10, "Check text is not removed" );
907         ok( $("#ap").children().length == 1, "Check filtered remove" );
908 });
909
910 test("empty()", function() {
911         expect(2);
912         ok( $("#ap").children().empty().text().length == 0, "Check text is removed" );
913         ok( $("#ap").children().length == 4, "Check elements are not removed" );
914 });
915
916 test("eq(), gt(), lt(), contains()", function() {
917         expect(4);
918         ok( $("#ap a").eq(1)[0].id == "groups", "eq()" );
919         isSet( $("#ap a").gt(0).get(), q("groups", "anchor1", "mark"), "gt()" );
920         isSet( $("#ap a").lt(3).get(), q("google", "groups", "anchor1"), "lt()" );
921         isSet( $("#foo a").contains("log").get(), q("anchor2", "simon"), "contains()" );
922 });