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) )
24 assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
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!')")
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!')")
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>"
41 assert_dom_equal %(<a href="#" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E");; return false;">Greet me!</a>), html
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>"
48 assert_dom_equal %(<a href="#" class="updater" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E");; return false;">Greet me!</a>), html
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/')
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/')
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!')")
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>"
70 assert_dom_equal %(<input type="button" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E");;" value="Greet me!" />), html
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>"
77 assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\\/h1\\u003E");;" value="Greet me!" />), html
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 :(')")
85 def test_button_to_function_without_function
86 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
87 button_to_function("Greeting")
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')")
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")
100 def test_javascript_tag_with_block
102 javascript_tag { _erbout.concat "alert('hello')" }
103 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", _erbout
106 def test_javascript_tag_with_block_and_options
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
112 def test_javascript_cdata_section
113 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")