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 = \{/)
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) )
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!')")
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!')")
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>"
40 assert_dom_equal %(<a href="#" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");; return false;">Greet me!</a>), html
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>"
47 assert_dom_equal %(<a href="#" class="updater" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");; return false;">Greet me!</a>), html
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/')
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!')")
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>"
64 assert_dom_equal %(<input type="button" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");;" value="Greet me!" />), html
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>"
71 assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");;" value="Greet me!" />), html
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 :(')")
79 def test_button_to_function_without_function
80 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
81 button_to_function("Greeting")
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')")
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")
94 def test_javascript_cdata_section
95 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")