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_link_to_function_with_href
49 assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
50 link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
53 def test_button_to_function
54 assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
55 button_to_function("Greeting", "alert('Hello world!')")
58 def test_button_to_function_with_rjs_block
59 html = button_to_function( "Greet me!" ) do |page|
60 page.replace_html 'header', "<h1>Greetings</h1>"
62 assert_dom_equal %(<input type="button" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E");;" value="Greet me!" />), html
65 def test_button_to_function_with_rjs_block_and_options
66 html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
67 page.replace_html 'header', "<h1>Greetings</h1>"
69 assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update("header", "\\u003Ch1\\u003EGreetings\\u003C\/h1\\u003E");;" value="Greet me!" />), html
72 def test_button_to_function_with_onclick
73 assert_dom_equal "<input onclick=\"alert('Goodbye World :('); alert('Hello world!');\" type=\"button\" value=\"Greeting\" />",
74 button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
77 def test_button_to_function_without_function
78 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
79 button_to_function("Greeting")
82 def test_javascript_tag
83 self.output_buffer = 'foo'
85 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
86 javascript_tag("alert('hello')")
88 assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer'
91 def test_javascript_tag_with_options
92 assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
93 javascript_tag("alert('hello')", :id => "the_js_tag")
96 def test_javascript_tag_with_block_in_erb
97 __in_erb_template = ''
98 javascript_tag { concat "alert('hello')" }
99 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
102 def test_javascript_tag_with_block_and_options_in_erb
103 __in_erb_template = ''
104 javascript_tag(:id => "the_js_tag") { concat "alert('hello')" }
105 assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>", output_buffer
108 def test_javascript_cdata_section
109 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")