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