Lots going on
[lyrix.git] / vendor / rails / actionpack / test / template / javascript_helper_test.rb
blobce830494a7843b4104efa3247fa7b7c17630e06a
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   end
26   def test_link_to_function
27     assert_dom_equal %(<a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>),
28       link_to_function("Greeting", "alert('Hello world!')")
29   end
31   def test_link_to_function_with_existing_onclick
32     assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>),
33       link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
34   end
36   def test_link_to_function_with_rjs_block
37     html = link_to_function( "Greet me!" ) do |page|
38       page.replace_html 'header', "<h1>Greetings</h1>"
39     end
40     assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074/h1\\076&quot;);; return false;">Greet me!</a>), html
41   end
43   def test_link_to_function_with_rjs_block_and_options
44     html = link_to_function( "Greet me!", :class => "updater" ) do |page|
45       page.replace_html 'header', "<h1>Greetings</h1>"
46     end
47     assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074/h1\\076&quot;);; return false;">Greet me!</a>), html
48   end
50   def test_link_to_function_with_href
51     assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
52       link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
53   end
55   def test_button_to_function
56     assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
57       button_to_function("Greeting", "alert('Hello world!')")
58   end
60   def test_button_to_function_with_rjs_block
61     html = button_to_function( "Greet me!" ) do |page|
62       page.replace_html 'header', "<h1>Greetings</h1>"
63     end
64     assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074/h1\\076&quot;);;" value="Greet me!" />), html
65   end
67   def test_button_to_function_with_rjs_block_and_options
68     html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
69       page.replace_html 'header', "<h1>Greetings</h1>"
70     end
71     assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\074h1\\076Greetings\\074/h1\\076&quot;);;" value="Greet me!" />), html
72   end
74   def test_button_to_function_with_onclick
75     assert_dom_equal "<input onclick=\"alert('Goodbye World :('); alert('Hello world!');\" type=\"button\" value=\"Greeting\" />",
76       button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
77   end
79   def test_button_to_function_without_function
80     assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
81       button_to_function("Greeting")
82   end
84   def test_javascript_tag
85     assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
86       javascript_tag("alert('hello')")
87   end
89   def test_javascript_tag_with_options
90     assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
91       javascript_tag("alert('hello')", :id => "the_js_tag")
92   end
94   def test_javascript_cdata_section
95     assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")
96   end
97 end