~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/actionpack/test/template/prototype_helper_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'abstract_unit'
2
 
 
3
 
Bunny = Struct.new(:Bunny, :id)
4
 
 
5
 
class Author
6
 
  attr_reader :id
7
 
  def save; @id = 1 end
8
 
  def new_record?; @id.nil? end
9
 
  def name
10
 
    @id.nil? ? 'new author' : "author ##{@id}"
11
 
  end
12
 
end
13
 
 
14
 
class Article
15
 
  attr_reader :id
16
 
  attr_reader :author_id
17
 
  def save; @id = 1; @author_id = 1 end
18
 
  def new_record?; @id.nil? end
19
 
  def name
20
 
    @id.nil? ? 'new article' : "article ##{@id}"
21
 
  end
22
 
end
23
 
 
24
 
class Author::Nested < Author; end
25
 
 
26
 
 
27
 
class PrototypeHelperBaseTest < ActionView::TestCase
28
 
  attr_accessor :template_format, :output_buffer
29
 
 
30
 
  def setup
31
 
    @template = self
32
 
    @controller = Class.new do
33
 
      def url_for(options)
34
 
        if options.is_a?(String)
35
 
          options
36
 
        else
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]
41
 
          url
42
 
        end
43
 
      end
44
 
    end.new
45
 
  end
46
 
 
47
 
  protected
48
 
    def request_forgery_protection_token
49
 
      nil
50
 
    end
51
 
 
52
 
    def protect_against_forgery?
53
 
      false
54
 
    end
55
 
 
56
 
    def create_generator
57
 
      block = Proc.new { |*args| yield *args if block_given? }
58
 
      JavaScriptGenerator.new self, &block
59
 
    end
60
 
end
61
 
 
62
 
class PrototypeHelperTest < PrototypeHelperBaseTest
63
 
  def setup
64
 
    @record = @author = Author.new
65
 
    @article = Article.new
66
 
    super
67
 
  end
68
 
 
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&amp;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)
84
 
  end
85
 
 
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" } })
89
 
  end
90
 
 
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" } })
94
 
  end
95
 
 
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&amp;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' })
107
 
  end
108
 
 
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" })
112
 
  end
113
 
 
114
 
  def test_periodically_call_remote_with_frequency
115
 
    assert_dom_equal(
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)
118
 
    )
119
 
  end
120
 
 
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  })
130
 
  end
131
 
 
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 })
135
 
  end
136
 
 
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
141
 
  end
142
 
 
143
 
  def test_remote_form_for_with_record_identification_with_new_record
144
 
    remote_form_for(@record, {:html => { :id => 'create-author' }}) {}
145
 
 
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
148
 
  end
149
 
 
150
 
  def test_remote_form_for_with_record_identification_without_html_options
151
 
    remote_form_for(@record) {}
152
 
 
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
155
 
  end
156
 
 
157
 
  def test_remote_form_for_with_record_identification_with_existing_record
158
 
    @record.save
159
 
    remote_form_for(@record) {}
160
 
 
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
163
 
  end
164
 
 
165
 
  def test_remote_form_for_with_new_object_in_list
166
 
    remote_form_for([@author, @article]) {}
167
 
 
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
170
 
  end
171
 
 
172
 
  def test_remote_form_for_with_existing_object_in_list
173
 
    @author.save
174
 
    @article.save
175
 
    remote_form_for([@author, @article]) {}
176
 
 
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
179
 
  end
180
 
 
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();")
192
 
    end
193
 
 
194
 
    #HTTP status codes 200 up to 599 have callbacks
195
 
    #these should work
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();")
199
 
    end
200
 
 
201
 
    #test 200 and 404
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();")
204
 
 
205
 
    #these shouldn't
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();")
209
 
    end
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();")
213
 
    end
214
 
 
215
 
    #test ultimate combo
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();")
218
 
 
219
 
  end
220
 
 
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")
224
 
  end
225
 
 
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" })
229
 
  end
230
 
 
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)")
235
 
  end
236
 
 
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}")
240
 
  end
241
 
 
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')")
245
 
  end
246
 
 
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" })
250
 
  end
251
 
 
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')")
255
 
  end
256
 
 
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")
260
 
  end
261
 
 
262
 
  def test_update_page
263
 
    old_output_buffer = output_buffer
264
 
 
265
 
    block = Proc.new { |page| page.replace_html('foo', 'bar') }
266
 
    assert_equal create_generator(&block).to_s, update_page(&block)
267
 
 
268
 
    assert_equal old_output_buffer, output_buffer
269
 
  end
270
 
 
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)
274
 
  end
275
 
 
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)
279
 
  end
280
 
 
281
 
 
282
 
  protected
283
 
    def author_path(record)
284
 
      "/authors/#{record.id}"
285
 
    end
286
 
 
287
 
    def authors_path
288
 
      "/authors"
289
 
    end
290
 
 
291
 
    def author_articles_path(author)
292
 
      "/authors/#{author.id}/articles"
293
 
    end
294
 
 
295
 
    def author_article_path(author, article)
296
 
      "/authors/#{author.id}/articles/#{article.id}"
297
 
    end
298
 
end
299
 
 
300
 
class JavaScriptGeneratorTest < PrototypeHelperBaseTest
301
 
  def setup
302
 
    super
303
 
    @generator = create_generator
304
 
  end
305
 
 
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>')
315
 
  end
316
 
 
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>')
320
 
  end
321
 
 
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>')
325
 
  end
326
 
 
327
 
  def test_remove
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')
332
 
  end
333
 
 
334
 
  def test_show
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')
339
 
  end
340
 
 
341
 
  def test_hide
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')
346
 
  end
347
 
 
348
 
  def test_toggle
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')
353
 
  end
354
 
 
355
 
  def test_alert
356
 
    assert_equal 'alert("hello");', @generator.alert('hello')
357
 
  end
358
 
 
359
 
  def test_redirect_to
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")
364
 
  end
365
 
 
366
 
  def test_reload
367
 
    assert_equal 'window.location.reload();',
368
 
      @generator.reload
369
 
  end
370
 
 
371
 
  def test_delay
372
 
    @generator.delay(20) do
373
 
      @generator.hide('foo')
374
 
    end
375
 
 
376
 
    assert_equal "setTimeout(function() {\n;\nElement.hide(\"foo\");\n}, 20000);", @generator.to_s
377
 
  end
378
 
 
379
 
  def test_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>')
384
 
 
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");
390
 
    EOS
391
 
  end
392
 
 
393
 
  def test_element_access
394
 
    assert_equal %($("hello");), @generator['hello']
395
 
  end
396
 
 
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]
400
 
  end
401
 
 
402
 
  def test_element_proxy_one_deep
403
 
    @generator['hello'].hide
404
 
    assert_equal %($("hello").hide();), @generator.to_s
405
 
  end
406
 
 
407
 
  def test_element_proxy_variable_access
408
 
    @generator['hello']['style']
409
 
    assert_equal %($("hello").style;), @generator.to_s
410
 
  end
411
 
 
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
415
 
  end
416
 
 
417
 
  def test_element_proxy_assignment
418
 
    @generator['hello'].width = 400
419
 
    assert_equal %($("hello").width = 400;), @generator.to_s
420
 
  end
421
 
 
422
 
  def test_element_proxy_two_deep
423
 
    @generator['hello'].hide("first").clean_whitespace
424
 
    assert_equal %($("hello").hide("first").cleanWhitespace();), @generator.to_s
425
 
  end
426
 
 
427
 
  def test_select_access
428
 
    assert_equal %($$("div.hello");), @generator.select('div.hello')
429
 
  end
430
 
 
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
434
 
  end
435
 
 
436
 
  def test_visual_effect
437
 
    assert_equal %(new Effect.Puff("blah",{});),
438
 
      @generator.visual_effect(:puff,'blah')
439
 
  end
440
 
 
441
 
  def test_visual_effect_toggle
442
 
    assert_equal %(Effect.toggle("blah",'appear',{});),
443
 
      @generator.visual_effect(:toggle_appear,'blah')
444
 
  end
445
 
 
446
 
  def test_sortable
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)
451
 
  end
452
 
 
453
 
  def test_draggable
454
 
    assert_equal %(new Draggable("blah", {});),
455
 
      @generator.draggable('blah')
456
 
  end
457
 
 
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)
463
 
  end
464
 
 
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();
471
 
      EOS
472
 
  end
473
 
 
474
 
  def test_collection_proxy_with_each
475
 
    @generator.select('p.welcome b').each do |value|
476
 
      value.remove_class_name 'selected'
477
 
    end
478
 
    @generator.select('p.welcome b').each do |value, index|
479
 
      @generator.visual_effect :highlight, value
480
 
    end
481
 
    assert_equal <<-EOS.strip, @generator.to_s
482
 
$$("p.welcome b").each(function(value, index) {
483
 
value.removeClassName("selected");
484
 
});
485
 
$$("p.welcome b").each(function(value, index) {
486
 
new Effect.Highlight(value,{});
487
 
});
488
 
      EOS
489
 
  end
490
 
 
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) {
496
 
return value.show();
497
 
});
498
 
$$("p").collect(function(value, index) {
499
 
return value.hide();
500
 
});
501
 
    EOS
502
 
    @generator = create_generator
503
 
  end
504
 
 
505
 
  def test_collection_proxy_with_grep
506
 
    @generator.select('p').grep 'a', /^a/ do |value|
507
 
      @generator << '(value.className == "welcome")'
508
 
    end
509
 
    @generator.select('p').grep 'b', /b$/ do |value, index|
510
 
      @generator.call 'alert', value
511
 
      @generator << '(value.className == "welcome")'
512
 
    end
513
 
 
514
 
    assert_equal <<-EOS.strip, @generator.to_s
515
 
var a = $$("p").grep(/^a/, function(value, index) {
516
 
return (value.className == "welcome");
517
 
});
518
 
var b = $$("p").grep(/b$/, function(value, index) {
519
 
alert(value);
520
 
return (value.className == "welcome");
521
 
});
522
 
    EOS
523
 
  end
524
 
 
525
 
  def test_collection_proxy_with_inject
526
 
    @generator.select('p').inject 'a', [] do |memo, value|
527
 
      @generator << '(value.className == "welcome")'
528
 
    end
529
 
    @generator.select('p').inject 'b', nil do |memo, value, index|
530
 
      @generator.call 'alert', memo
531
 
      @generator << '(value.className == "welcome")'
532
 
    end
533
 
 
534
 
    assert_equal <<-EOS.strip, @generator.to_s
535
 
var a = $$("p").inject([], function(memo, value, index) {
536
 
return (value.className == "welcome");
537
 
});
538
 
var b = $$("p").inject(null, function(memo, value, index) {
539
 
alert(memo);
540
 
return (value.className == "welcome");
541
 
});
542
 
    EOS
543
 
  end
544
 
 
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
548
 
  end
549
 
 
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'
554
 
    end
555
 
 
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();
560
 
});
561
 
    EOS
562
 
  end
563
 
 
564
 
  def test_collection_proxy_with_find_all
565
 
    @generator.select('p').find_all 'a' do |value, index|
566
 
      @generator << '(value.className == "welcome")'
567
 
    end
568
 
 
569
 
    assert_equal <<-EOS.strip, @generator.to_s
570
 
var a = $$("p").findAll(function(value, index) {
571
 
return (value.className == "welcome");
572
 
});
573
 
    EOS
574
 
  end
575
 
 
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");
582
 
    EOS
583
 
  end
584
 
 
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|
588
 
      group.reverse
589
 
    end
590
 
 
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();
595
 
});
596
 
    EOS
597
 
  end
598
 
 
599
 
  def test_debug_rjs
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
603
 
  ensure
604
 
    ActionView::Base.debug_rjs = false
605
 
  end
606
 
 
607
 
  def test_literal
608
 
    literal = @generator.literal("function() {}")
609
 
    assert_equal "function() {}", ActiveSupport::JSON.encode(literal)
610
 
    assert_equal "", @generator.to_s
611
 
  end
612
 
 
613
 
  def test_class_proxy
614
 
    @generator.form.focus('my_field')
615
 
    assert_equal "Form.focus(\"my_field\");", @generator.to_s
616
 
  end
617
 
 
618
 
  def test_call_with_block
619
 
    @generator.call(:before)
620
 
    @generator.call(:my_method) do |p|
621
 
      p[:one].show
622
 
      p[:two].hide
623
 
    end
624
 
    @generator.call(:in_between)
625
 
    @generator.call(:my_method_with_arguments, true, "hello") do |p|
626
 
      p[:three].visual_effect(:highlight)
627
 
    end
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
629
 
  end
630
 
 
631
 
  def test_class_proxy_call_with_block
632
 
    @generator.my_object.my_method do |p|
633
 
      p[:one].show
634
 
      p[:two].hide
635
 
    end
636
 
    assert_equal "MyObject.myMethod(function() { $(\"one\").show();\n$(\"two\").hide(); });", @generator.to_s
637
 
  end
638
 
end
639