2007-01-28 07:16:55 +00:00
|
|
|
require "#{File.dirname(__FILE__)}/../abstract_unit"
|
2005-03-26 14:03:55 +00:00
|
|
|
|
2005-06-29 08:09:00 +00:00
|
|
|
class JavaScriptHelperTest < Test::Unit::TestCase
|
|
|
|
include ActionView::Helpers::JavaScriptHelper
|
2007-06-13 01:20:55 +00:00
|
|
|
|
2005-06-18 05:17:39 +00:00
|
|
|
include ActionView::Helpers::UrlHelper
|
|
|
|
include ActionView::Helpers::TagHelper
|
2005-07-02 18:20:13 +00:00
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ActionView::Helpers::FormHelper
|
2005-07-04 04:57:15 +00:00
|
|
|
include ActionView::Helpers::CaptureHelper
|
2007-06-13 01:20:55 +00:00
|
|
|
|
2005-06-26 12:03:43 +00:00
|
|
|
def test_define_javascript_functions
|
|
|
|
# check if prototype.js is included first
|
2005-06-29 08:09:00 +00:00
|
|
|
assert_not_nil define_javascript_functions.split("\n")[1].match(/Prototype JavaScript framework/)
|
2007-06-13 01:20:55 +00:00
|
|
|
|
2005-09-28 08:20:47 +00:00
|
|
|
# check that scriptaculous.js is not in here, only needed if loaded remotely
|
|
|
|
assert_nil define_javascript_functions.split("\n")[1].match(/var Scriptaculous = \{/)
|
2005-06-26 12:03:43 +00:00
|
|
|
end
|
2005-03-26 14:03:55 +00:00
|
|
|
|
|
|
|
def test_escape_javascript
|
2007-06-13 01:20:55 +00:00
|
|
|
assert_equal '', escape_javascript(nil)
|
2005-03-26 14:03:55 +00:00
|
|
|
assert_equal %(This \\"thing\\" is really\\n netos\\'), escape_javascript(%(This "thing" is really\n netos'))
|
2006-10-09 00:34:43 +00:00
|
|
|
assert_equal %(backslash\\\\test), escape_javascript( %(backslash\\test) )
|
2005-03-26 14:03:55 +00:00
|
|
|
end
|
2007-06-13 01:20:55 +00:00
|
|
|
|
2005-06-18 05:17:39 +00:00
|
|
|
def test_link_to_function
|
2007-06-13 01:20:55 +00:00
|
|
|
assert_dom_equal %(<a href="#" onclick="alert('Hello world!'); return false;">Greeting</a>),
|
2005-06-18 05:17:39 +00:00
|
|
|
link_to_function("Greeting", "alert('Hello world!')")
|
|
|
|
end
|
2007-06-13 01:20:55 +00:00
|
|
|
|
2005-12-31 03:50:08 +00:00
|
|
|
def test_link_to_function_with_existing_onclick
|
2007-06-13 01:20:55 +00:00
|
|
|
assert_dom_equal %(<a href="#" onclick="confirm('Sanity!'); alert('Hello world!'); return false;">Greeting</a>),
|
2005-12-31 03:50:08 +00:00
|
|
|
link_to_function("Greeting", "alert('Hello world!')", :onclick => "confirm('Sanity!')")
|
|
|
|
end
|
2005-12-31 04:31:10 +00:00
|
|
|
|
2006-04-19 18:08:15 +00:00
|
|
|
def test_link_to_function_with_rjs_block
|
|
|
|
html = link_to_function( "Greet me!" ) do |page|
|
|
|
|
page.replace_html 'header', "<h1>Greetings</h1>"
|
|
|
|
end
|
2007-05-29 09:42:13 +00:00
|
|
|
assert_dom_equal %(<a href="#" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");; return false;">Greet me!</a>), html
|
2006-04-19 18:08:15 +00:00
|
|
|
end
|
|
|
|
|
2006-09-06 01:31:04 +00:00
|
|
|
def test_link_to_function_with_rjs_block_and_options
|
|
|
|
html = link_to_function( "Greet me!", :class => "updater" ) do |page|
|
|
|
|
page.replace_html 'header', "<h1>Greetings</h1>"
|
|
|
|
end
|
2007-05-29 09:42:13 +00:00
|
|
|
assert_dom_equal %(<a href="#" class="updater" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");; return false;">Greet me!</a>), html
|
2006-09-06 01:31:04 +00:00
|
|
|
end
|
2006-04-19 18:08:15 +00:00
|
|
|
|
2007-06-08 04:00:15 +00:00
|
|
|
def test_link_to_function_with_href
|
2007-06-13 01:20:55 +00:00
|
|
|
assert_dom_equal %(<a href="http://example.com/" onclick="alert('Hello world!'); return false;">Greeting</a>),
|
2007-06-08 04:00:15 +00:00
|
|
|
link_to_function("Greeting", "alert('Hello world!')", :href => 'http://example.com/')
|
|
|
|
end
|
|
|
|
|
2005-12-31 04:31:10 +00:00
|
|
|
def test_button_to_function
|
2007-06-13 01:20:55 +00:00
|
|
|
assert_dom_equal %(<input type="button" onclick="alert('Hello world!');" value="Greeting" />),
|
2005-12-31 04:31:10 +00:00
|
|
|
button_to_function("Greeting", "alert('Hello world!')")
|
|
|
|
end
|
2006-04-19 18:08:15 +00:00
|
|
|
|
|
|
|
def test_button_to_function_with_rjs_block
|
|
|
|
html = button_to_function( "Greet me!" ) do |page|
|
|
|
|
page.replace_html 'header', "<h1>Greetings</h1>"
|
|
|
|
end
|
2007-05-29 09:42:13 +00:00
|
|
|
assert_dom_equal %(<input type="button" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");;" value="Greet me!" />), html
|
2006-04-19 18:08:15 +00:00
|
|
|
end
|
2006-09-06 01:31:04 +00:00
|
|
|
|
|
|
|
def test_button_to_function_with_rjs_block_and_options
|
|
|
|
html = button_to_function( "Greet me!", :class => "greeter" ) do |page|
|
|
|
|
page.replace_html 'header', "<h1>Greetings</h1>"
|
|
|
|
end
|
2007-05-29 09:42:13 +00:00
|
|
|
assert_dom_equal %(<input type="button" class="greeter" onclick="Element.update("header", "\\074h1\\076Greetings\\074/h1\\076");;" value="Greet me!" />), html
|
2006-09-06 01:31:04 +00:00
|
|
|
end
|
2007-06-08 22:20:18 +00:00
|
|
|
|
2007-06-13 01:20:55 +00:00
|
|
|
def test_button_to_function_with_onclick
|
|
|
|
assert_dom_equal "<input onclick=\"alert('Goodbye World :('); alert('Hello world!');\" type=\"button\" value=\"Greeting\" />",
|
|
|
|
button_to_function("Greeting", "alert('Hello world!')", :onclick => "alert('Goodbye World :(')")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_button_to_function_without_function
|
|
|
|
assert_dom_equal "<input onclick=\";\" type=\"button\" value=\"Greeting\" />",
|
|
|
|
button_to_function("Greeting")
|
|
|
|
end
|
|
|
|
|
2007-06-08 22:20:18 +00:00
|
|
|
def test_javascript_tag
|
|
|
|
assert_dom_equal "<script type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
|
|
|
javascript_tag("alert('hello')")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_javascript_tag_with_options
|
|
|
|
assert_dom_equal "<script id=\"the_js_tag\" type=\"text/javascript\">\n//<![CDATA[\nalert('hello')\n//]]>\n</script>",
|
|
|
|
javascript_tag("alert('hello')", :id => "the_js_tag")
|
|
|
|
end
|
2007-06-13 01:20:55 +00:00
|
|
|
|
|
|
|
def test_javascript_cdata_section
|
|
|
|
assert_dom_equal "\n//<![CDATA[\nalert('hello')\n//]]>\n", javascript_cdata_section("alert('hello')")
|
|
|
|
end
|
2005-03-26 14:03:55 +00:00
|
|
|
end
|