Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / template / javascript_helper_test.rb
blob1fabe80ba74dbde1f474e8f5c403636abb18a976
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 class JavaScriptHelperTest < Test::Unit::TestCase
4   include ActionView::Helpers::JavaScriptHelper
6   include ActionView::Helpers::UrlHelper
7   include ActionView::Helpers::TagHelper
8   include ActionView::Helpers::TextHelper
9   include ActionView::Helpers::FormHelper
10   include ActionView::Helpers::CaptureHelper
12   def test_define_javascript_functions
13     # check if prototype.js is included first
14     assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype JavaScript framework/)
16     # check that scriptaculous.js is not in here, only needed if loaded remotely
17     assert_nil define_javascript_functions.split("\n")[1].match(/var Scriptaculous = \{/)
18   end
20   def test_escape_javascript
21     assert_equal '', escape_javascript(nil)
22     assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
23     assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) )
24     assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
25   end
27   def test_link_to_function
28     assert_dom_equal %(<a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>),
29       link_to_function("Greeting", "alert('Hello world!')")
30   end
32   def test_link_to_function_with_existing_onclick
33     assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>),
34       link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
35   end
37   def test_link_to_function_with_rjs_block
38     html = link_to_function( "Greet me!" ) do |page|
39       page.replace_html 'header', "<h1>Greetings</h1>"
40     end
41     assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html
42   end
44   def test_link_to_function_with_rjs_block_and_options
45     html = link_to_function( "Greet me!", :class => "updater" ) do |page|
46       page.replace_html 'header', "<h1>Greetings</h1>"
47     end
48     assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);; return false;">Greet me!</a>), html
49   end
51   def test_link_to_function_with_href
52     assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
53       link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
54   end
56   def test_link_to_function_with_href
57     assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>), 
58       link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
59   end
61   def test_button_to_function
62     assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
63       button_to_function("Greeting", "alert('Hello world!')")
64   end
66   def test_button_to_function_with_rjs_block
67     html = button_to_function( "Greet me!" ) do |page|
68       page.replace_html 'header', "<h1>Greetings</h1>"
69     end
70     assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html
71   end
73   def test_button_to_function_with_rjs_block_and_options
74     html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
75       page.replace_html 'header', "<h1>Greetings</h1>"
76     end
77     assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E&quot;);;" value="Greet me!" />), html
78   end
80   def test_button_to_function_with_onclick
81     assert_dom_equal "<input onclick=\"alert('Goodbye World :('); alert('Hello world!');\" type=\"button\" value=\"Greeting\" />",
82       button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
83   end
85   def test_button_to_function_without_function
86     assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
87       button_to_function("Greeting")
88   end
90   def test_javascript_tag
91     assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
92       javascript_tag("alert('hello')")
93   end
95   def test_javascript_tag_with_options
96     assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
97       javascript_tag("alert('hello')", :id => "the_js_tag")
98   end
100   def test_javascript_tag_with_block
101     _erbout = ''
102     javascript_tag { _erbout.concat "alert('hello')" }
103     assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", _erbout
104   end
106   def test_javascript_tag_with_block_and_options
107     _erbout = ''
108     javascript_tag(:id => "the_js_tag") { _erbout.concat "alert('hello')" }
109     assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", _erbout
110   end
112   def test_javascript_cdata_section
113     assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")
114   end