1
require 'abstract_unit'
3
Bunny = Struct.new(:Bunny, :id)
8
def new_record?; @id.nil? end
10
@id.nil? ? 'new author' : "author ##{@id}"
16
attr_reader :author_id
17
def save; @id = 1; @author_id = 1 end
18
def new_record?; @id.nil? end
20
@id.nil? ? 'new article' : "article ##{@id}"
24
class Author::Nested < Author; end
27
class PrototypeHelperBaseTest < ActionView::TestCase
28
attr_accessor :template_format, :output_buffer
32
@controller = Class.new do
34
if options.is_a?(String)
37
url = "http://www.example.com/"
38
url << options[:action].to_s if options and options[:action]
39
url << "?a=#{options[:a]}" if options && options[:a]
40
url << "&b=#{options[:b]}" if options && options[:a] && options[:b]
48
def request_forgery_protection_token
52
def protect_against_forgery?
57
block = Proc.new { |*args| yield *args if block_given? }
58
JavaScriptGenerator.new self, &block
62
class PrototypeHelperTest < PrototypeHelperBaseTest
64
@record = @author = Author.new
65
@article = Article.new
69
def test_link_to_remote
70
assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outauthor</a>),
71
link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" })
72
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onComplete:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
73
link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" })
74
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onSuccess:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
75
link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" })
76
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
77
link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot" })
78
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.responseText)}}); return false;\">Remote outauthor</a>),
79
link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' })
80
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:false, evalScripts:true}); return false;\">Remote outauthor</a>),
81
link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :type => :synchronous)
82
assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, insertion:'bottom'}); return false;\">Remote outauthor</a>),
83
link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :position => :bottom)
86
def test_link_to_remote_html_options
87
assert_dom_equal %(<a class=\"fine\" href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true}); return false;\">Remote outauthor</a>),
88
link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } })
91
def test_link_to_remote_url_quote_escaping
92
assert_dom_equal %(<a href="#" onclick="new Ajax.Request('http://www.example.com/whatnot\\\'s', {asynchronous:true, evalScripts:true}); return false;">Remote</a>),
93
link_to_remote("Remote", { :url => { :action => "whatnot's" } })
96
def test_button_to_remote
97
assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true});\" />),
98
button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" })
99
assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onComplete:function(request){alert(request.reponseText)}});\" />),
100
button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" })
101
assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onSuccess:function(request){alert(request.reponseText)}});\" />),
102
button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" })
103
assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}});\" />),
104
button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" })
105
assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot?a=10&b=20', {asynchronous:true, evalScripts:true, onFailure:function(request){alert(request.reponseText)}});\" />),
106
button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' })
109
def test_periodically_call_remote
110
assert_dom_equal %(<script type="text/javascript">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Updater('schremser_bier', 'http://www.example.com/mehr_bier', {asynchronous:true, evalScripts:true})}, 10)\n//]]>\n</script>),
111
periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" })
114
def test_periodically_call_remote_with_frequency
116
"<script type=\"text/javascript\">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true})}, 2)\n//]]>\n</script>",
117
periodically_call_remote(:frequency => 2)
121
def test_form_remote_tag
122
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
123
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast })
124
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
125
form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast })
126
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
127
form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast })
128
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">),
129
form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast })
132
def test_form_remote_tag_with_method
133
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div>),
134
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put })
137
def test_form_remote_tag_with_block_in_erb
138
__in_erb_template = ''
139
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" }
140
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\">Hello world!</form>), output_buffer
143
def test_remote_form_for_with_record_identification_with_new_record
144
remote_form_for(@record, {:html => { :id => 'create-author' }}) {}
146
expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' id='create-author' method='post'></form>)
147
assert_dom_equal expected, output_buffer
150
def test_remote_form_for_with_record_identification_without_html_options
151
remote_form_for(@record) {}
153
expected = %(<form action='#{authors_path}' onsubmit="new Ajax.Request('#{authors_path}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_author' method='post' id='new_author'></form>)
154
assert_dom_equal expected, output_buffer
157
def test_remote_form_for_with_record_identification_with_existing_record
159
remote_form_for(@record) {}
161
expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' onsubmit="new Ajax.Request('#{author_path(@record)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_author'><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>)
162
assert_dom_equal expected, output_buffer
165
def test_remote_form_for_with_new_object_in_list
166
remote_form_for([@author, @article]) {}
168
expected = %(<form action='#{author_articles_path(@author)}' onsubmit="new Ajax.Request('#{author_articles_path(@author)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='new_article' method='post' id='new_article'></form>)
169
assert_dom_equal expected, output_buffer
172
def test_remote_form_for_with_existing_object_in_list
175
remote_form_for([@author, @article]) {}
177
expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_1' method='post' onsubmit="new Ajax.Request('#{author_article_path(@author, @article)}', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" class='edit_article'><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>)
178
assert_dom_equal expected, output_buffer
181
def test_on_callbacks
182
callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure]
183
callbacks.each do |callback|
184
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
185
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
186
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
187
form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();")
188
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({failure:'glass_of_beer'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
189
form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();")
190
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater({success:'glass_of_beer',failure:'glass_of_water'}, 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
191
form_remote_tag(:update => { :success => "glass_of_beer", :failure => "glass_of_water" }, :url => { :action => :fast }, callback=>"monkeys();")
194
#HTTP status codes 200 up to 599 have callbacks
196
100.upto(599) do |callback|
197
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on#{callback.to_s.capitalize}:function(request){monkeys();}, parameters:Form.serialize(this)}); return false;">),
198
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
202
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on200:function(request){monkeys();}, on404:function(request){bananas();}, parameters:Form.serialize(this)}); return false;">),
203
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, 200=>"monkeys();", 404=>"bananas();")
206
1.upto(99) do |callback|
207
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">),
208
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
210
600.upto(999) do |callback|
211
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;">),
212
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();")
216
assert_dom_equal %(<form action=\"http://www.example.com/fast\" method=\"post\" onsubmit=\"new Ajax.Updater('glass_of_beer', 'http://www.example.com/fast', {asynchronous:true, evalScripts:true, on200:function(request){monkeys();}, on404:function(request){bananas();}, onComplete:function(request){c();}, onFailure:function(request){f();}, onLoading:function(request){c1()}, onSuccess:function(request){s()}, parameters:Form.serialize(this)}); return false;\">),
217
form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();")
221
def test_submit_to_remote
222
assert_dom_equal %(<input name=\"More beer!\" onclick=\"new Ajax.Updater('empty_bottle', 'http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this.form)});\" type=\"button\" value=\"1000000\" />),
223
submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle")
226
def test_observe_field
227
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/reorder_if_empty', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
228
observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" })
231
def test_observe_field_using_with_option
232
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(value)})})\n//]]>\n</script>)
233
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id')
234
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)")
237
def test_observe_field_using_json_in_with_option
238
expected = %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {new Ajax.Request('http://www.example.com/check_value', {asynchronous:true, evalScripts:true, parameters:{'id':value}})})\n//]]>\n</script>)
239
assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}")
242
def test_observe_field_using_function_for_callback
243
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>),
244
observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')")
247
def test_observe_form
248
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {new Ajax.Request('http://www.example.com/cart_changed', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
249
observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" })
252
def test_observe_form_using_function_for_callback
253
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>),
254
observe_form("cart", :frequency => 2, :function => "alert('Form changed')")
257
def test_observe_field_without_frequency
258
assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.EventObserver('glass', function(element, value) {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:value})})\n//]]>\n</script>),
259
observe_field("glass")
263
old_output_buffer = output_buffer
265
block = Proc.new { |page| page.replace_html('foo', 'bar') }
266
assert_equal create_generator(&block).to_s, update_page(&block)
268
assert_equal old_output_buffer, output_buffer
271
def test_update_page_tag
272
block = Proc.new { |page| page.replace_html('foo', 'bar') }
273
assert_equal javascript_tag(create_generator(&block).to_s), update_page_tag(&block)
276
def test_update_page_tag_with_html_options
277
block = Proc.new { |page| page.replace_html('foo', 'bar') }
278
assert_equal javascript_tag(create_generator(&block).to_s, {:defer => 'true'}), update_page_tag({:defer => 'true'}, &block)
283
def author_path(record)
284
"/authors/#{record.id}"
291
def author_articles_path(author)
292
"/authors/#{author.id}/articles"
295
def author_article_path(author, article)
296
"/authors/#{author.id}/articles/#{article.id}"
300
class JavaScriptGeneratorTest < PrototypeHelperBaseTest
303
@generator = create_generator
306
def test_insert_html_with_string
307
assert_equal 'Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });',
308
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
309
assert_equal 'Element.insert("element", { bottom: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
310
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
311
assert_equal 'Element.insert("element", { before: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
312
@generator.insert_html(:before, 'element', '<p>This is a test</p>')
313
assert_equal 'Element.insert("element", { after: "\\u003Cp\u003EThis is a test\\u003C/p\u003E" });',
314
@generator.insert_html(:after, 'element', '<p>This is a test</p>')
317
def test_replace_html_with_string
318
assert_equal 'Element.update("element", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");',
319
@generator.replace_html('element', '<p>This is a test</p>')
322
def test_replace_element_with_string
323
assert_equal 'Element.replace("element", "\\u003Cdiv id=\"element\"\\u003E\\u003Cp\\u003EThis is a test\\u003C/p\\u003E\\u003C/div\\u003E");',
324
@generator.replace('element', '<div id="element"><p>This is a test</p></div>')
328
assert_equal 'Element.remove("foo");',
329
@generator.remove('foo')
330
assert_equal '["foo","bar","baz"].each(Element.remove);',
331
@generator.remove('foo', 'bar', 'baz')
335
assert_equal 'Element.show("foo");',
336
@generator.show('foo')
337
assert_equal '["foo","bar","baz"].each(Element.show);',
338
@generator.show('foo', 'bar', 'baz')
342
assert_equal 'Element.hide("foo");',
343
@generator.hide('foo')
344
assert_equal '["foo","bar","baz"].each(Element.hide);',
345
@generator.hide('foo', 'bar', 'baz')
349
assert_equal 'Element.toggle("foo");',
350
@generator.toggle('foo')
351
assert_equal '["foo","bar","baz"].each(Element.toggle);',
352
@generator.toggle('foo', 'bar', 'baz')
356
assert_equal 'alert("hello");', @generator.alert('hello')
360
assert_equal 'window.location.href = "http://www.example.com/welcome";',
361
@generator.redirect_to(:action => 'welcome')
362
assert_equal 'window.location.href = "http://www.example.com/welcome?a=b&c=d";',
363
@generator.redirect_to("http://www.example.com/welcome?a=b&c=d")
367
assert_equal 'window.location.reload();',
372
@generator.delay(20) do
373
@generator.hide('foo')
376
assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
380
@generator.insert_html(:top, 'element', '<p>This is a test</p>')
381
@generator.insert_html(:bottom, 'element', '<p>This is a test</p>')
382
@generator.remove('foo', 'bar')
383
@generator.replace_html('baz', '<p>This is a test</p>')
385
assert_equal <<-EOS.chomp, @generator.to_s
386
Element.insert("element", { top: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
387
Element.insert("element", { bottom: "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E" });
388
["foo","bar"].each(Element.remove);
389
Element.update("baz", "\\u003Cp\\u003EThis is a test\\u003C/p\\u003E");
393
def test_element_access
394
assert_equal %($("hello");), @generator['hello']
397
def test_element_access_on_records
398
assert_equal %($("bunny_5");), @generator[Bunny.new(:id => 5)]
399
assert_equal %($("new_bunny");), @generator[Bunny.new]
402
def test_element_proxy_one_deep
403
@generator['hello'].hide
404
assert_equal %($("hello").hide();), @generator.to_s
407
def test_element_proxy_variable_access
408
@generator['hello']['style']
409
assert_equal %($("hello").style;), @generator.to_s
412
def test_element_proxy_variable_access_with_assignment
413
@generator['hello']['style']['color'] = 'red'
414
assert_equal %($("hello").style.color = "red";), @generator.to_s
417
def test_element_proxy_assignment
418
@generator['hello'].width = 400
419
assert_equal %($("hello").width = 400;), @generator.to_s
422
def test_element_proxy_two_deep
423
@generator['hello'].hide("first").clean_whitespace
424
assert_equal %($("hello").hide("first").cleanWhitespace();), @generator.to_s
427
def test_select_access
428
assert_equal %($$("div.hello");), @generator.select('div.hello')
431
def test_select_proxy_one_deep
432
@generator.select('p.welcome b').first.hide
433
assert_equal %($$("p.welcome b").first().hide();), @generator.to_s
436
def test_visual_effect
437
assert_equal %(new Effect.Puff("blah",{});),
438
@generator.visual_effect(:puff,'blah')
441
def test_visual_effect_toggle
442
assert_equal %(Effect.toggle("blah",'appear',{});),
443
@generator.visual_effect(:toggle_appear,'blah')
447
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
448
@generator.sortable('blah', :url => { :action => "order" })
449
assert_equal %(Sortable.create("blah", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:Sortable.serialize("blah")})}});),
450
@generator.sortable('blah', :url => { :action => "order" }, :type => :synchronous)
454
assert_equal %(new Draggable("blah", {});),
455
@generator.draggable('blah')
458
def test_drop_receiving
459
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
460
@generator.drop_receiving('blah', :url => { :action => "order" })
461
assert_equal %(Droppables.add("blah", {onDrop:function(element){new Ajax.Request('http://www.example.com/order', {asynchronous:false, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}});),
462
@generator.drop_receiving('blah', :url => { :action => "order" }, :type => :synchronous)
465
def test_collection_first_and_last
466
@generator.select('p.welcome b').first.hide()
467
@generator.select('p.welcome b').last.show()
468
assert_equal <<-EOS.strip, @generator.to_s
469
$$("p.welcome b").first().hide();
470
$$("p.welcome b").last().show();
474
def test_collection_proxy_with_each
475
@generator.select('p.welcome b').each do |value|
476
value.remove_class_name 'selected'
478
@generator.select('p.welcome b').each do |value, index|
479
@generator.visual_effect :highlight, value
481
assert_equal <<-EOS.strip, @generator.to_s
482
$$("p.welcome b").each(function(value, index) {
483
value.removeClassName("selected");
485
$$("p.welcome b").each(function(value, index) {
486
new Effect.Highlight(value,{});
491
def test_collection_proxy_on_collect
492
@generator.select('p').collect('a') { |para| para.show }
493
@generator.select('p').collect { |para| para.hide }
494
assert_equal <<-EOS.strip, @generator.to_s
495
var a = $$("p").collect(function(value, index) {
498
$$("p").collect(function(value, index) {
502
@generator = create_generator
505
def test_collection_proxy_with_grep
506
@generator.select('p').grep 'a', /^a/ do |value|
507
@generator << '(value.className == "welcome")'
509
@generator.select('p').grep 'b', /b$/ do |value, index|
510
@generator.call 'alert', value
511
@generator << '(value.className == "welcome")'
514
assert_equal <<-EOS.strip, @generator.to_s
515
var a = $$("p").grep(/^a/, function(value, index) {
516
return (value.className == "welcome");
518
var b = $$("p").grep(/b$/, function(value, index) {
520
return (value.className == "welcome");
525
def test_collection_proxy_with_inject
526
@generator.select('p').inject 'a', [] do |memo, value|
527
@generator << '(value.className == "welcome")'
529
@generator.select('p').inject 'b', nil do |memo, value, index|
530
@generator.call 'alert', memo
531
@generator << '(value.className == "welcome")'
534
assert_equal <<-EOS.strip, @generator.to_s
535
var a = $$("p").inject([], function(memo, value, index) {
536
return (value.className == "welcome");
538
var b = $$("p").inject(null, function(memo, value, index) {
540
return (value.className == "welcome");
545
def test_collection_proxy_with_pluck
546
@generator.select('p').pluck('a', 'className')
547
assert_equal %(var a = $$("p").pluck("className");), @generator.to_s
550
def test_collection_proxy_with_zip
551
ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('a', [4, 5, 6], [7, 8, 9])
552
ActionView::Helpers::JavaScriptCollectionProxy.new(@generator, '[1, 2, 3]').zip('b', [4, 5, 6], [7, 8, 9]) do |array|
553
@generator.call 'array.reverse'
556
assert_equal <<-EOS.strip, @generator.to_s
557
var a = [1, 2, 3].zip([4,5,6], [7,8,9]);
558
var b = [1, 2, 3].zip([4,5,6], [7,8,9], function(array) {
559
return array.reverse();
564
def test_collection_proxy_with_find_all
565
@generator.select('p').find_all 'a' do |value, index|
566
@generator << '(value.className == "welcome")'
569
assert_equal <<-EOS.strip, @generator.to_s
570
var a = $$("p").findAll(function(value, index) {
571
return (value.className == "welcome");
576
def test_collection_proxy_with_in_groups_of
577
@generator.select('p').in_groups_of('a', 3)
578
@generator.select('p').in_groups_of('a', 3, 'x')
579
assert_equal <<-EOS.strip, @generator.to_s
580
var a = $$("p").inGroupsOf(3);
581
var a = $$("p").inGroupsOf(3, "x");
585
def test_collection_proxy_with_each_slice
586
@generator.select('p').each_slice('a', 3)
587
@generator.select('p').each_slice('a', 3) do |group, index|
591
assert_equal <<-EOS.strip, @generator.to_s
592
var a = $$("p").eachSlice(3);
593
var a = $$("p").eachSlice(3, function(value, index) {
594
return value.reverse();
600
ActionView::Base.debug_rjs = true
601
@generator['welcome'].replace_html 'Welcome'
602
assert_equal "try {\n$(\"welcome\").update(\"Welcome\");\n} catch (e) { alert('RJS error:\\n\\n' + e.toString()); alert('$(\\\"welcome\\\").update(\\\"Welcome\\\");'); throw e }", @generator.to_s
604
ActionView::Base.debug_rjs = false
608
literal = @generator.literal("function() {}")
609
assert_equal "function() {}", ActiveSupport::JSON.encode(literal)
610
assert_equal "", @generator.to_s
614
@generator.form.focus('my_field')
615
assert_equal "Form.focus(\"my_field\");", @generator.to_s
618
def test_call_with_block
619
@generator.call(:before)
620
@generator.call(:my_method) do |p|
624
@generator.call(:in_between)
625
@generator.call(:my_method_with_arguments, true, "hello") do |p|
626
p[:three].visual_effect(:highlight)
628
assert_equal "before();\nmy_method(function() { $(\"one\").show();\n$(\"two\").hide(); });\nin_between();\nmy_method_with_arguments(true, \"hello\", function() { $(\"three\").visualEffect(\"highlight\"); });", @generator.to_s
631
def test_class_proxy_call_with_block
632
@generator.my_object.my_method do |p|
636
assert_equal "MyObject.myMethod(function() { $(\"one\").show();\n$(\"two\").hide(); });", @generator.to_s