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/test/template/ajax_helper_test.rb | 344 +++++++++++++++++---------- 1 file changed, 223 insertions(+), 121 deletions(-) (limited to 'actionpack/test/template') 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