PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/test/rdom/jquery_test.rb

https://gitlab.com/Blueprint-Marketing/rdom
Ruby | 283 lines | 20 code | 5 blank | 258 comment | 0 complexity | c5b966738f1a0a26f493d00a36c152ee MD5 | raw file
  1. require File.expand_path('../../test_helper', __FILE__)
  2. require 'webmock/test_unit'
  3. class JQueryTest < Test::Unit::TestCase
  4. attr_reader :window
  5. def setup
  6. @window = RDom::Window.new(:url => 'http://example.org')
  7. stub_get('jquery.js', File.open(File.expand_path('../../fixtures/jquery-1.4.2.js', __FILE__), 'r') { |f| f.read })
  8. end
  9. test "can load jquery", :jquery do
  10. assert_nothing_raised do
  11. window.load <<-html
  12. <html>
  13. <head>
  14. <script src="http://example.org/jquery.js"></script>
  15. </head>
  16. </html>
  17. html
  18. end
  19. end
  20. # test "can use jquery", :jquery do
  21. # window.load <<-html
  22. # <html>
  23. # <head>
  24. # <script src="http://example.org/jquery.js"></script>
  25. # <script>
  26. # var foo = -1;
  27. # $(document).ready(function() { foo = $("body").length });
  28. # </script>
  29. # </head>
  30. # <body></body>
  31. # </html>
  32. # html
  33. # assert_equal 1, window.evaluate('foo')
  34. # end
  35. #
  36. # test "jquery selectors: div", :jquery do
  37. # window.load <<-html
  38. # <html>
  39. # <head><script src="http://example.org/jquery.js"></script></head>
  40. # <body><div id="foo"><ol><li class="bar"></li></ol></div></body>
  41. # </html>
  42. # html
  43. # assert_equal 1, window.evaluate('$("div").length')
  44. # end
  45. #
  46. # test "jquery selectors: #foo", :jquery do
  47. # window.load <<-html
  48. # <html>
  49. # <head><script src="http://example.org/jquery.js"></script></head>
  50. # <body><div id="foo"><ol><li class="bar"></li></ol></div></body>
  51. # </html>
  52. # html
  53. # assert_equal 1, window.evaluate('$("#foo").length')
  54. # end
  55. #
  56. # test "jquery selectors: #foo ol li", :jquery do
  57. # window.load <<-html
  58. # <html>
  59. # <head><script src="http://example.org/jquery.js"></script></head>
  60. # <body><div id="foo"><ol><li class="bar"></li></ol></div></body>
  61. # </html>
  62. # html
  63. # assert_equal 1, window.evaluate('$("#foo ol li").length')
  64. # end
  65. #
  66. # test "jquery selectors: .bar", :jquery do
  67. # window.load <<-html
  68. # <html>
  69. # <head><script src="http://example.org/jquery.js"></script></head>
  70. # <body><div id="foo"><ol><li class="bar"></li></ol></div></body>
  71. # </html>
  72. # html
  73. # assert_equal 1, window.evaluate('$(".bar").length')
  74. # end
  75. #
  76. # test "jquery selectors: ol:nth-child(1)", :jquery do
  77. # window.load <<-html
  78. # <html>
  79. # <head><script src="http://example.org/jquery.js"></script></head>
  80. # <body><div id="foo"><ol><li class="bar"></li></ol></div></body>
  81. # </html>
  82. # html
  83. # assert_equal 1, window.evaluate('$("ol:nth-child(1)").length')
  84. # end
  85. #
  86. # test "jquery dom generation", :jquery do
  87. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  88. # html = window.evaluate("$('<div id=\"foo\"/>').toArray()")
  89. # # p html
  90. # # html = window.evaluate("jQuery('<div id=\"foo\"/><hr><code>code</code>').toArray()").join('')
  91. # # assert_equal '<div id="foo"></div><hr><code>code</code>', html
  92. # end
  93. #
  94. # test "jquery dom generation with attributes", :jquery do
  95. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  96. # tag = window.evaluate <<-js
  97. # jQuery("<div/>", {
  98. # width: 10,
  99. # css: { paddingLeft:1, paddingRight:1 },
  100. # click: function(){ ok(exec, "Click executed."); },
  101. # text: "test",
  102. # "class": "test2",
  103. # id: "test3"
  104. # })[0];
  105. # js
  106. # assert_equal 'DIV', tag.nodeName
  107. # assert_equal 'test', tag.textContent
  108. # end
  109. #
  110. # test "jquery can click on an element generated through dom generation with attributes", :jquery do
  111. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  112. # tag = window.evaluate <<-js
  113. # var clicked = false
  114. # var element = jQuery("<div/>", { click: function() { clicked = true; } })[0];
  115. # element.click();
  116. # js
  117. # assert_equal true, window.evaluate("clicked")
  118. # end
  119. #
  120. # test "jquery parseJSON", :jquery do
  121. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  122. # assert_equal nil, window.evaluate('jQuery.parseJSON()')
  123. # assert_equal nil, window.evaluate('jQuery.parseJSON(null)')
  124. # assert_equal 1, window.evaluate('jQuery.parseJSON(\'{"test":1}\')')['test']
  125. # end
  126. #
  127. # test "jQuery.support.leadingWhitespace == true" do
  128. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  129. # assert window.evaluate('jQuery.support.leadingWhitespace')
  130. # end
  131. #
  132. # test "jQuery.support.tbody == true" do
  133. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  134. # assert window.evaluate('jQuery.support.tbody')
  135. # end
  136. #
  137. # test "jQuery.support.htmlSerialize == true" do
  138. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  139. # assert window.evaluate('jQuery.support.htmlSerialize')
  140. # end
  141. #
  142. # test "jQuery.support.style == true" do
  143. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  144. # assert window.evaluate('jQuery.support.style')
  145. # end
  146. #
  147. # test "jQuery.support.hrefNormalized == true" do
  148. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  149. # assert window.evaluate('jQuery.support.hrefNormalized')
  150. # end
  151. #
  152. # # TODO gotta parse styles
  153. # test "jQuery.support.opacity == false" do
  154. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  155. # assert !window.evaluate('jQuery.support.opacity')
  156. # end
  157. #
  158. # # TODO gotta parse styles
  159. # test "jQuery.support.cssFloat == false" do
  160. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  161. # assert !window.evaluate('jQuery.support.cssFloat')
  162. # end
  163. #
  164. # # seems ok as jquery says webkit doesn't do this either
  165. # test "jQuery.support.checkOn == false" do
  166. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  167. # assert !window.evaluate('jQuery.support.checkOn')
  168. # end
  169. #
  170. # test "jQuery.support.optSelected == true" do
  171. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  172. # assert window.evaluate('jQuery.support.optSelected')
  173. # end
  174. #
  175. # test "jQuery.support.parentNode == true" do
  176. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  177. # assert window.evaluate('jQuery.support.parentNode')
  178. # end
  179. #
  180. # test "jQuery.support.deleteExpando == true" do
  181. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  182. # assert window.evaluate('jQuery.support.deleteExpando')
  183. # end
  184. #
  185. # test "jQuery.support.checkClone == true" do
  186. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  187. # assert window.evaluate('jQuery.support.checkClone')
  188. # end
  189. #
  190. # test "jQuery.support.scriptEval == true" do
  191. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  192. # assert window.evaluate('jQuery.support.scriptEval')
  193. # end
  194. #
  195. # test "jQuery.support.noCloneEvent == true" do
  196. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  197. # assert window.evaluate('jQuery.support.noCloneEvent')
  198. # end
  199. #
  200. # # seems ok as we'd need to implement the full box model for this?
  201. # test "jQuery.support.boxModel == false" do
  202. # window.load('<html><head><script src="http://example.org/jquery.js"></script></head><body></body></html>')
  203. # assert !window.evaluate('jQuery.support.boxModel')
  204. # end
  205. #
  206. # test "jquery html()" do
  207. # window.load <<-html
  208. # <html>
  209. # <head><script src="http://example.org/jquery.js"></script></head>
  210. # <body><div id="wrap">#{'<div>foo</div>' * 100}</div></body>
  211. # </html>
  212. # html
  213. # window.evaluate <<-js
  214. # var html = $('#wrap')[0].innerHTML;
  215. # function reset() { $('#wrap').html(html); }
  216. # reset();
  217. # reset();
  218. # js
  219. # assert_equal 100, window.evaluate("$('#wrap div').length")
  220. # end
  221. #
  222. # test "jquery find(option:first-child)" do
  223. # window.load <<-html
  224. # <html>
  225. # <head><script src="http://example.org/jquery.js"></script></head>
  226. # <body><select id="select"><option value="value"></option></select></body>
  227. # </html>
  228. # html
  229. # value = window.evaluate("$('#select').find('option:first-child').attr('value')")
  230. # # value = window.evaluate("$('#select')[0].firstChild.attr('value')")
  231. # assert_equal 'value', value
  232. # end
  233. #
  234. # test "jquery replaceWith" do
  235. # window.load <<-html
  236. # <html>
  237. # <head><script src="http://example.org/jquery.js"></script></head>
  238. # <body><div id="main"><a id="yahoo" href="http://www.yahoo.com/">Yahoo</a></div></body>
  239. # </html>
  240. # html
  241. #
  242. # window.evaluate <<-js
  243. # var fixture = $('#main')[0].innerHTML;
  244. # var reset = function() { $("#main").html(fixture); }
  245. #
  246. # var functionReturningObj = function(value) {
  247. # return (function() { return value; });
  248. # };
  249. # var testReplaceWith = function(val) {
  250. # jQuery('#yahoo').replaceWith(val('<b id="replace">buga</b>'));
  251. # };
  252. # testReplaceWith(functionReturningObj);
  253. # reset();
  254. #
  255. # var y = jQuery("#yahoo")[0];
  256. # var context = undefined;
  257. # jQuery(y).replaceWith(function() { context = this; });
  258. # js
  259. #
  260. # assert_equal window.evaluate('y'), window.evaluate('context');
  261. # end
  262. #
  263. # test 'jquery foo' do
  264. # window.load <<-html
  265. # <html>
  266. # <head><script src="http://example.org/jquery.js"></script></head>
  267. # <body><div id="main"><a id="yahoo" href="http://www.yahoo.com/">Yahoo</a></div></body>
  268. # </html>
  269. # html
  270. #
  271. # window.evaluate <<-js
  272. # var div = jQuery("<div class='replacewith'></div>").appendTo("body");
  273. # jQuery('.replacewith').remove();
  274. # var y = jQuery("#yahoo")[0];
  275. # jQuery(y).replaceWith(function(){});
  276. # js
  277. # end
  278. end