Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / template / javascript_helper_test.rb
1 require 'abstract_unit'
2
3 class JavaScriptHelperTest < ActionView::TestCase
4 tests ActionView::Helpers::JavaScriptHelper
5
6 attr_accessor :template_format, :output_buffer
7
8 def setup
9 @template = self
10 end
11
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))
17 end
18
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!')")
22 end
23
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!')")
27 end
28
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>"
32 end
33 assert_dom_equal %(<a href="#" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);; return false;">Greet me!</a>), html
34 end
35
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>"
39 end
40 assert_dom_equal %(<a href="#" class="updater" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);; return false;">Greet me!</a>), html
41 end
42
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/')
46 end
47
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!')")
51 end
52
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>"
56 end
57 assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);;" value="Greet me!" />), html
58 end
59
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>"
63 end
64 assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C\/h1\\u003E&quot;);;" value="Greet me!" />), html
65 end
66
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 :(')")
70 end
71
72 def test_button_to_function_without_function
73 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
74 button_to_function("Greeting")
75 end
76
77 def test_javascript_tag
78 self.output_buffer = 'foo'
79
80 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
81 javascript_tag("alert('hello')")
82
83 assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer'
84 end
85
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")
89 end
90
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
95 end
96
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
101 end
102
103 def test_javascript_cdata_section
104 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")
105 end
106 end