Froze rails gems
[depot.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_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/')
51 end
52
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!')")
56 end
57
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>"
61 end
62 assert_dom_equal %(<input type="button" onclick="Element.update(&quot;header&quot;, &quot;\\u003Ch1\\u003EGreetings\\u003C/h1\\u003E&quot;);;" value="Greet me!" />), html
63 end
64
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>"
68 end
69 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
70 end
71
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 :(')")
75 end
76
77 def test_button_to_function_without_function
78 assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
79 button_to_function("Greeting")
80 end
81
82 def test_javascript_tag
83 self.output_buffer = 'foo'
84
85 assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
86 javascript_tag("alert('hello')")
87
88 assert_equal 'foo', output_buffer, 'javascript_tag without a block should not concat to output_buffer'
89 end
90
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")
94 end
95
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
100 end
101
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
106 end
107
108 def test_javascript_cdata_section
109 assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")
110 end
111 end