removing log dir from .gitignore
[monkeycharger.git] / vendor / rails / actionpack / test / template / java_script_macros_helper_test.rb
blob961f12cf32e3e7df1631e4988b88390f2470c7cc
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 class JavaScriptMacrosHelperTest < Test::Unit::TestCase
4   include ActionView::Helpers::JavaScriptHelper
5   include ActionView::Helpers::JavaScriptMacrosHelper
6   
7   include ActionView::Helpers::UrlHelper
8   include ActionView::Helpers::TagHelper
9   include ActionView::Helpers::TextHelper
10   include ActionView::Helpers::FormHelper
11   include ActionView::Helpers::CaptureHelper
12   
13   def setup
14     @controller = Class.new do
15       def url_for(options)
16         url =  "http://www.example.com/"
17         url << options[:action].to_s if options and options[:action]
18         url
19       end
20     end
21     @controller = @controller.new
22   end
25   def test_auto_complete_field
26     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {})\n//]]>\n</script>),
27       auto_complete_field("some_input", :url => { :action => "autocomplete" });
28     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:','})\n//]]>\n</script>),
29       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => ',');
30     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {tokens:[',']})\n//]]>\n</script>),
31       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :tokens => [',']);  
32     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {minChars:3})\n//]]>\n</script>),
33       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :min_chars => 3);
34     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {onHide:function(element, update){alert('me');}})\n//]]>\n</script>),
35       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :on_hide => "function(element, update){alert('me');}");
36     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {frequency:2})\n//]]>\n</script>),
37       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :frequency => 2);
38     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {afterUpdateElement:function(element,value){alert('You have chosen: '+value)}})\n//]]>\n</script>),
39       auto_complete_field("some_input", :url => { :action => "autocomplete" }, 
40         :after_update_element => "function(element,value){alert('You have chosen: '+value)}");
41     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {paramName:'huidriwusch'})\n//]]>\n</script>),
42       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :param_name => 'huidriwusch');
43     assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nvar some_input_auto_completer = new Ajax.Autocompleter('some_input', 'some_input_auto_complete', 'http://www.example.com/autocomplete', {method:'get'})\n//]]>\n</script>),
44       auto_complete_field("some_input", :url => { :action => "autocomplete" }, :method => :get);
45   end
46   
47   def test_auto_complete_result
48     result = [ { :title => 'test1'  }, { :title => 'test2'  } ]
49     assert_equal %(<ul><li>test1</li><li>test2</li></ul>), 
50       auto_complete_result(result, :title)
51     assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li><li>t<strong class=\"highlight\">est</strong>2</li></ul>), 
52       auto_complete_result(result, :title, "est")
53     
54     resultuniq = [ { :title => 'test1'  }, { :title => 'test1'  } ]
55     assert_equal %(<ul><li>t<strong class=\"highlight\">est</strong>1</li></ul>), 
56       auto_complete_result(resultuniq, :title, "est")
57   end
58   
59   def test_text_field_with_auto_complete
60     assert_match %(<style type="text/css">),
61       text_field_with_auto_complete(:message, :recipient)
63     assert_dom_equal %(<input id=\"message_recipient\" name=\"message[recipient]\" size=\"30\" type=\"text\" /><div class=\"auto_complete\" id=\"message_recipient_auto_complete\"></div><script type=\"text/javascript\">\n//<![CDATA[\nvar message_recipient_auto_completer = new Ajax.Autocompleter('message_recipient', 'message_recipient_auto_complete', 'http://www.example.com/auto_complete_for_message_recipient', {})\n//]]>\n</script>),
64       text_field_with_auto_complete(:message, :recipient, {}, :skip_style => true)
65   end
66   
67   def test_in_place_editor_external_control
68       assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {externalControl:'blah'})\n//]]>\n</script>),
69         in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :external_control => 'blah'})
70   end
71   
72   def test_in_place_editor_size
73       assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {size:4})\n//]]>\n</script>),
74         in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :size => 4})
75   end
76   
77   def test_in_place_editor_cols_no_rows
78       assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {cols:4})\n//]]>\n</script>),
79         in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :cols => 4})
80   end
81   
82   def test_in_place_editor_cols_with_rows
83       assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {cols:40, rows:5})\n//]]>\n</script>),
84         in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :rows => 5, :cols => 40})
85   end
87   def test_inplace_editor_loading_text
88       assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Ajax.InPlaceEditor('some_input', 'http://www.example.com/inplace_edit', {loadingText:'Why are we waiting?'})\n//]]>\n</script>),
89         in_place_editor('some_input', {:url => {:action => 'inplace_edit'}, :loading_text => 'Why are we waiting?'})
90   end
91   
92   def test_in_place_editor_url
93     assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value')",
94     in_place_editor( 'id-goes-here', :url => { :action => "action_to_set_value" })    
95   end
96   
97   def test_in_place_editor_load_text_url
98     assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {loadTextURL:'http://www.example.com/action_to_get_value'})",
99     in_place_editor( 'id-goes-here', 
100       :url => { :action => "action_to_set_value" }, 
101       :load_text_url => { :action => "action_to_get_value" })
102   end
103   
104   def test_in_place_editor_eval_scripts
105     assert_match "Ajax.InPlaceEditor('id-goes-here', 'http://www.example.com/action_to_set_value', {evalScripts:true})",
106     in_place_editor( 'id-goes-here', 
107       :url => { :action => "action_to_set_value" }, 
108       :script => true )
109   end
110