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