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