From 37ad57596b7afdf38be443f06130b736539dd2af Mon Sep 17 00:00:00 2001 From: "Stephen St. Martin" Date: Sat, 23 Jan 2010 19:45:01 -0500 Subject: port existing test suite to check for new output, and fix all breaking tests --- actionpack/lib/action_view/helpers/ajax_helper.rb | 86 +++--- actionpack/test/template/ajax_helper_test.rb | 344 ++++++++++++++-------- 2 files changed, 268 insertions(+), 162 deletions(-) (limited to 'actionpack') diff --git a/actionpack/lib/action_view/helpers/ajax_helper.rb b/actionpack/lib/action_view/helpers/ajax_helper.rb index 6f7f3750bc..7d164929f0 100644 --- a/actionpack/lib/action_view/helpers/ajax_helper.rb +++ b/actionpack/lib/action_view/helpers/ajax_helper.rb @@ -105,13 +105,15 @@ module ActionView # <% end -%> def form_remote_tag(options = {}, &block) + html_options = options.delete(:callbacks) + attributes = {} attributes.merge!(extract_remote_attributes!(options)) + attributes.merge!(html_options) if html_options attributes.merge!(options) - - url = attributes.delete("data-url") attributes.delete(:builder) - form_tag(attributes.delete(:action) || url, attributes, &block) + + form_tag(attributes.delete(:action) || attributes.delete("data-url"), attributes, &block) end # Returns a link to a remote action defined by options[:url] @@ -287,13 +289,12 @@ module ActionView # link_to_remote "Delete this post", # { :update => "posts", :url => { :action => "destroy", :id => post.id } }, # :href => url_for(:action => "destroy", :id => post.id) - def link_to_remote(name, url, options = {}) + def link_to_remote(name, options, html_options = {}) attributes = {} attributes.merge!(extract_remote_attributes!(options)) - attributes.merge!(options) + attributes.merge!(html_options) - url = url_for(url) if url.is_a?(Hash) - content_tag(:a, name, attributes.merge(:href => url)) + content_tag(:a, name, attributes.merge(:href => "#")) end # Creates a button with an onclick event which calls a remote action @@ -303,7 +304,6 @@ module ActionView def button_to_remote(name, options = {}, html_options = {}) attributes = html_options.merge!(:type => "button", :value => name) attributes.merge!(extract_remote_attributes!(options)) - attributes.merge!(extract_request_attributes!(options)) tag(:input, attributes) end @@ -341,10 +341,12 @@ module ActionView # options argument is the same as in form_remote_tag. def submit_to_remote(name, value, options = {}) html_options = options.delete(:html) || {} - html_options.merge!(:name => name, :value => value, :type => "submit") + html_options.merge!(:name => name, :value => value, :type => "button") attributes = extract_remote_attributes!(options) attributes.merge!(html_options) + attributes["data-submit"] = true + attributes.delete("data-remote") tag(:input, attributes) end @@ -376,7 +378,7 @@ module ActionView # def periodically_call_remote(options = {}) attributes = extract_observer_attributes!(options) - attributes["data-js-type"] = "periodical_executer" + attributes["data-periodical"] = true script_decorator(attributes) end @@ -493,9 +495,11 @@ module ActionView def extract_request_attributes!(options) attributes = {} attributes["data-method"] = options.delete(:method) + attributes["data-remote-type"] = options.delete(:type) - url = options.delete(:url) - attributes["data-url"] = url.is_a?(Hash) ? url_for(url) : url + url_options = options.delete(:url) + url_options = url_options.merge(:escape => false) if url_options.is_a?(Hash) + attributes["data-url"] = escape_javascript(url_for(url_options)) #TODO: Remove all references to prototype - BR if options.delete(:form) @@ -528,18 +532,16 @@ module ActionView end def extract_observer_attributes!(options) + callback = options.delete(:function) + frequency = options.delete(:frequency) + + attributes = extract_remote_attributes!(options) attributes["data-observe"] = true attributes["data-observed"] = options.delete(:observed) - - callback = options.delete(:function) - frequency = options.delete(:frequency) - if callback - attributes["data-observer-code"] = create_js_function(callback, "element", "value") - end - if frequency && frequency != 0 - attributes["data-frequency"] = frequency.to_i - end + attributes["data-onobserve"] = create_js_function(callback, "element", "value") if callback + attributes["data-frequency"] = frequency.to_i if frequency && frequency != 0 + attributes.delete("data-remote") purge_unused_attributes!(attributes) end @@ -558,26 +560,8 @@ module ActionView module AjaxHelperCompat include AjaxHelper - def set_callbacks(options, html) - [:complete, :failure, :success, :interactive, :loaded, :loading].each do |type| - html["data-#{type}-code"] = options.delete(type.to_sym) - end - - options.each do |option, value| - if option.is_a?(Integer) - html["data-#{option}-code"] = options.delete(option) - end - end - end - - def link_to_remote(name, url, options = nil) - if !options && url.is_a?(Hash) && url.key?(:url) - url, options = url.delete(:url), url - end - options = {} if options.nil? - - set_callbacks(options, options[:html] ||= {}) - + def link_to_remote(name, options, html_options = {}) + set_callbacks(options, html_options) super end @@ -585,6 +569,26 @@ module ActionView set_callbacks(options, html_options) super end + + def form_remote_tag(options, &block) + html = {} + set_callbacks(options, html) + options.merge!(:callbacks => html) + super + end + + private + def set_callbacks(options, html) + [:uninitialized, :complete, :failure, :success, :interactive, :loaded, :loading].each do |type| + html["data-on#{type}"] = options.delete(type.to_sym) + end + + options.each do |option, value| + if option.is_a?(Integer) + html["data-on#{option}"] = options.delete(option) + end + end + end end end end diff --git a/actionpack/test/template/ajax_helper_test.rb b/actionpack/test/template/ajax_helper_test.rb index 4ca130d010..cb28448913 100644 --- a/actionpack/test/template/ajax_helper_test.rb +++ b/actionpack/test/template/ajax_helper_test.rb @@ -1,179 +1,281 @@ -require "abstract_unit" - -class AjaxTestCase < ActiveSupport::TestCase - include ActionView::Helpers::TagHelper - - def url_for(url) - case url - when Hash - "/url/hash" - when String - url - else - raise TypeError.new("Unsupported url type (#{url.class}) for this test helper") - end +require 'abstract_unit' +require 'active_model' + +class Author + extend ActiveModel::Naming + include ActiveModel::Conversion + + attr_reader :id + def save; @id = 1 end + def new_record?; @id.nil? end + def name + @id.nil? ? 'new author' : "author ##{@id}" end +end - def assert_html(html, matches) - matches.each do |match| - assert_match(Regexp.new(Regexp.escape(match)), html) - end +class Article + extend ActiveModel::Naming + include ActiveModel::Conversion + attr_reader :id + attr_reader :author_id + def save; @id = 1; @author_id = 1 end + def new_record?; @id.nil? end + def name + @id.nil? ? 'new article' : "article ##{@id}" end +end + +class AjaxHelperBaseTest < ActionView::TestCase + attr_accessor :formats, :output_buffer - def self.assert_callbacks_work(&blk) - define_method(:assert_callbacks_work, &blk) + def reset_formats(format) + @format = format + end - [:complete, :failure, :success, :interactive, :loaded, :loading, 404].each do |callback| - test "#{callback} callback" do - markup = assert_callbacks_work(callback) - assert_html markup, %W(data-#{callback}-code="undoRequestCompleted\(request\)") + def setup + super + @template = self + @controller = Class.new do + def url_for(options) + if options.is_a?(String) + options + else + url = "http://www.example.com/" + url << options[:action].to_s if options and options[:action] + url << "?a=#{options[:a]}" if options && options[:a] + url << "&b=#{options[:b]}" if options && options[:a] && options[:b] + url + end end - end + end.new end + + protected + def request_forgery_protection_token + nil + end + + def protect_against_forgery? + false + end end -class LinkToRemoteTest < AjaxTestCase - include ActionView::Helpers::AjaxHelperCompat +class AjaxHelperTest < AjaxHelperBaseTest + def _evaluate_assigns_and_ivars() end - def link(options = {}) - link_to_remote("Delete this post", "/blog/destroy/3", options) + def setup + @record = @author = Author.new + @article = Article.new + super end - test "basic" do - assert_html link(:update => "#posts"), - %w(data-update-success="#posts") + test "link_to_remote" do + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" }) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :type => :synchronous) + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :position => :bottom) end - test "using a url string" do - assert_html link_to_remote("Test", "/blog/update/1"), - %w(href="/blog/update/1") + test "link_to_remote html options" do + assert_dom_equal %(Remote outauthor), + link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) end - test "using a url hash" do - link = link_to_remote("Delete this post", {:controller => :blog}, :update => "#posts") - assert_html link, %w(href="/url/hash" data-update-success="#posts") + test "link_to_remote url quote escaping" do + assert_dom_equal %(Remote), + link_to_remote("Remote", { :url => { :action => "whatnot's" } }) end - test "with no update" do - assert_html link, %w(href="/blog/destroy/3" Delete\ this\ post data-remote="true") + test "button_to_remote" do + assert_dom_equal %(), + button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) + assert_dom_equal %(), + button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(), + button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(), + button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(), + button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) end - test "with :html options" do - expected = %{Delete this post} - assert_equal expected, link(:update => "#posts", :html => {"data-custom" => "me"}) + test "periodically_call_remote" do + assert_dom_equal %(), + periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) end - test "with a hash for :update" do - link = link(:update => {:success => "#posts", :failure => "#error"}) - assert_match(/data-update-success="#posts"/, link) - assert_match(/data-update-failure="#error"/, link) + test "periodically_call_remote_with_frequency" do + assert_dom_equal( + "", + periodically_call_remote(:frequency => 2) + ) end - test "with positional parameters" do - link = link(:position => :top, :update => "#posts") - assert_match(/data\-update\-position="top"/, link) + test "form_remote_tag" do + assert_dom_equal %(
), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) + assert_dom_equal %(), + form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }) + assert_dom_equal %(), + form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast }) + assert_dom_equal %(), + form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) end - test "with an optional method" do - link = link(:method => "delete") - assert_match(/data-method="delete"/, link) + test "form_remote_tag with method" do + assert_dom_equal %(
), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put }) end - class LegacyLinkToRemoteTest < AjaxTestCase - include ActionView::Helpers::AjaxHelperCompat + test "form_remote_tag with block in erb" do + __in_erb_template = '' + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" } + assert_dom_equal %(Hello world!
), output_buffer + end - def link(options) - link_to_remote("Delete this post", "/blog/destroy/3", options) - end + test "remote_form_for with record identification with new record" do + remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - test "basic link_to_remote with :url =>" do - expected = %{Delete this post} - assert_equal expected, - link_to_remote("Delete this post", :url => "/blog/destroy/4", :update => "#posts") - end + expected = %(
) + assert_dom_equal expected, output_buffer + end - assert_callbacks_work do |callback| - link(callback => "undoRequestCompleted(request)") - end + test "remote_form_for with record identification without html options" do + remote_form_for(@record) {} + + expected = %(
) + assert_dom_equal expected, output_buffer end -end -class ButtonToRemoteTest < AjaxTestCase - include ActionView::Helpers::AjaxHelperCompat + test "remote_form_for with record identification with existing record" do + @record.save + remote_form_for(@record) {} - def button(options, html = {}) - button_to_remote("Remote outpost", options, html) + expected = %(
) + assert_dom_equal expected, output_buffer end - class StandardTest < ButtonToRemoteTest - test "basic" do - expected = %{} - assert_equal expected, button({:url => "/url/hash"}, {:class => "fine"}) - end + test "remote_form_for with new object in list" do + remote_form_for([@author, @article]) {} + + expected = %(
) + assert_dom_equal expected, output_buffer end - class LegacyButtonToRemoteTest < ButtonToRemoteTest - include ActionView::Helpers::AjaxHelperCompat + test "remote_form_for with existing object in list" do + @author.save + @article.save + remote_form_for([@author, @article]) {} - assert_callbacks_work do |callback| - button(callback => "undoRequestCompleted(request)") + expected = %(
) + assert_dom_equal expected, output_buffer + end + + test "on callbacks" do + callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] + callbacks.each do |callback| + assert_dom_equal %(
), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + assert_dom_equal %(), + form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") + assert_dom_equal %(), + form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") + assert_dom_equal %(), + form_remote_tag(:update => { :success => "glass_of_beer", :failure => "glass_of_water" }, :url => { :action => :fast }, callback=>"monkeys();") + end + + #HTTP status codes 200 up to 599 have callbacks + #these should work + 100.upto(599) do |callback| + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + end + + #test 200 and 404 + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, 200=>"monkeys();", 404=>"bananas();") + + #these shouldn't + 1.upto(99) do |callback| + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + end + 600.upto(999) do |callback| + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end + + #test ultimate combo + assert_dom_equal %(), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();") + end -end -class ObserveFieldTest < AjaxTestCase - include ActionView::Helpers::AjaxHelperCompat + test "submit_to_remote" do + assert_dom_equal %(), + submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle") + end - def url_for(hash) - "/blog/update" + test "observe_field" do + assert_dom_equal %(), + observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) end - def protect_against_forgery? - false + test "observe_field using with option" do + expected = %() + assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id') + assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)") end - def field(options = {}) - observe_field("title", options) + test "observe_field using json in with option" do + expected = %() + assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") end - test "basic" do - assert_html field, - %w(data-observe="true") + test "observe_field using function for callback" do + assert_dom_equal %(), + observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") end - test "with a :frequency option" do - assert_html field(:frequency => 5.minutes), - %w(data-observe="true" data-frequency="300") + test "observe_form" do + assert_dom_equal %(), + observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) end - test "using a url string" do - assert_html field(:url => "/some/other/url"), - %w(data-observe="true" data-url="/some/other/url") + test "observe_form using function for callback" do + assert_dom_equal %(), + observe_form("cart", :frequency => 2, :function => "alert('Form changed')") end - test "using a url hash" do - assert_html field(:url => {:controller => :blog, :action => :update}), - %w(data-observe="true" data-url="/blog/update") + test "observe_field without frequency" do + assert_dom_equal %(), + observe_field("glass") end -# def test_observe_field -# assert_dom_equal %(), -# observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) -# end -# -# def test_observe_field_using_with_option -# expected = %() -# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id') -# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "'id=' + encodeURIComponent(value)") -# end -# -# def test_observe_field_using_json_in_with_option -# expected = %() -# assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") -# end -# -# def test_observe_field_using_function_for_callback -# assert_dom_equal %(), -# observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") -# end + protected + def author_path(record) + "/authors/#{record.id}" + end + + def authors_path + "/authors" + end + + def author_articles_path(author) + "/authors/#{author.id}/articles" + end + + def author_article_path(author, article) + "/authors/#{author.id}/articles/#{article.id}" + end end -- cgit v1.2.3