1 require 'abstract_unit'
3 class JavaScriptHelperTest
< ActionView
::TestCase
4 tests ActionView
::Helpers::JavaScriptHelper
6 attr_accessor
:template_format, :output_buffer
12 def test_escape_javascript
13 assert_equal
'', escape_javascript(nil)
14 assert_equal
%(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
15 assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) )
16 assert_equal %(dont <\\/close> tags), escape_javascript(%(dont </close> tags))
19 def test_link_to_function
20 assert_dom_equal %(<a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>),
21 link_to_function("Greeting", "alert('Hello world!')")
24 def test_link_to_function_with_existing_onclick
25 assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>),
26 link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
29 def test_link_to_function_with_rjs_block
30 html = link_to_function( "Greet me!" ) do |page|
31 page.replace_html 'header', "<h1>Greetings</h1>"
33 assert_dom_equal %(<a href="#" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");; return false;">Greet me!</a>), html
36 def test_link_to_function_with_rjs_block_and_options
37 html = link_to_function( "Greet me!", :class => "updater" ) do |page|
38 page.replace_html 'header', "<h1>Greetings</h1>"
40 assert_dom_equal %(<a href="#" class="updater" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");; return false;">Greet me!</a>), html
43 def test_link_to_function_with_href
44 assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
45 link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
48 def test_button_to_function
49 assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
50 button_to_function("Greeting", "alert('Hello world!')")
53 def test_button_to_function_with_rjs_block
54 html = button_to_function( "Greet me!" ) do |page|
55 page.replace_html 'header', "<h1>Greetings</h1>"
57 assert_dom_equal %(<input type="button" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");;" value="Greet me!" />), html
60 def test_button_to_function_with_rjs_block_and_options
61 html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
62 page.replace_html 'header', "<h1>Greetings</h1>"
64 assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\/h1\\u003E");;" value="Greet me!" />), html
67 def test_button_to_function_with_onclick
68 assert_dom_equal "<input onclick=\"alert('Goodbye World :('); alert('Hello world!');\" type=\"button\" value=\"Greeting\" />",
69 button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
72 def test_button_to_function_without_function
73 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
74 button_to_function("Greeting")
77 def test_javascript_tag
78 self.output_buffer = 'foo'
80 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
81 javascript_tag("alert('hello')")
83 assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer'
86 def test_javascript_tag_with_options
87 assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
88 javascript_tag("alert('hello')", :id => "the_js_tag")
91 def test_javascript_tag_with_block_in_erb
92 __in_erb_template = ''
93 javascript_tag { concat "alert('hello')" }
94 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
97 def test_javascript_tag_with_block_and_options_in_erb
98 __in_erb_template = ''
99 javascript_tag(:id => "the_js_tag") { concat "alert('hello')" }
100 assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
103 def test_javascript_cdata_section
104 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")