From 2e338aed706b3ee8fb8d51040be87689597e087b Mon Sep 17 00:00:00 2001 From: Stefan Penner Date: Wed, 27 Jan 2010 12:35:32 -0600 Subject: updated tests + docs, plus minor inconsistency fixes --- actionpack/test/template/ajax_helper_test.rb | 268 ++++++++++++++++++--------- 1 file changed, 182 insertions(+), 86 deletions(-) (limited to 'actionpack/test/template/ajax_helper_test.rb') diff --git a/actionpack/test/template/ajax_helper_test.rb b/actionpack/test/template/ajax_helper_test.rb index ed020d74f8..c925dbb8f6 100644 --- a/actionpack/test/template/ajax_helper_test.rb +++ b/actionpack/test/template/ajax_helper_test.rb @@ -6,8 +6,14 @@ class Author include ActiveModel::Conversion attr_reader :id - def save; @id = 1 end - def new_record?; @id.nil? end + def save + @id = 1 + end + + def new_record? + @id.nil? + end + def name @id.nil? ? 'new author' : "author ##{@id}" end @@ -18,8 +24,16 @@ class Article 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 save + @id = 1 + @author_id = 1 + end + + def new_record? + @id.nil? + end + def name @id.nil? ? 'new article' : "article ##{@id}" end @@ -36,17 +50,34 @@ class AjaxHelperBaseTest < ActionView::TestCase 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 + return optons unless options.is_a?(Hash) + + url = options.delete(:only_path) ? '/' : 'http://www.example.com' + + if controller = options.delete(:controller) + url << '/' << controller.to_s + end + if action = options.delete(:action) + url << '/' << action.to_s end + + if id = options.delete(:id) + url << '/' << id.to_s + end + + url << hash_to_param(options) if options.any? + + url.gsub!(/\/\/+/,'/') + + url end + + private + def hash_to_param(hash) + hash.map { |k,v| "#{k}=#{v}" }.join('&').insert(0,'?') + end end.new end @@ -70,24 +101,36 @@ class AjaxHelperTest < AjaxHelperBaseTest end 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) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", { :url => { :action => "whatnot" }}, { :class => "fine" }) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :url => { :action => "whatnot" }, :type => :synchronous) + assert_dom_equal %(Remove Author), + link_to_remote("Remove Author", :url => { :action => "whatnot" }, :position => :bottom) + end + + test "link_to_remote with url and oncomplete" do + actual = link_to_remote "undo", :url => { :controller => "words", :action => "undo", :n => 5 }, :complete => "undoRequestCompleted(request)" + expected = 'undo' + assert_dom_equal expected, actual + end + + test "link_to_remote with delete" do + actual = link_to_remote("Remove Author", { :url => { :action => "whatnot" }, :method => 'delete'}, { :class => "fine" }) + expected = 'Remove Author' + assert_dom_equal expected, actual end - + test "link_to_remote using both url and href" do - expected = 'Delete this Post' + expected = 'Delete this Post' assert_dom_equal expected, link_to_remote( "Delete this Post", { :update => "posts", :url => { :action => "destroy" } }, @@ -95,135 +138,162 @@ class AjaxHelperTest < AjaxHelperBaseTest end test "link_to_remote with update-success and url" do - expected = 'Delete this Post' - assert_dom_equal expected, link_to_remote( "Delete this Post", :url => { :action => "destroy", :id => 5 }, + expected = 'Delete this Post' + assert_dom_equal expected, link_to_remote( "Delete this Post", :url => { :action => "destroy"}, :update => { :success => "posts", :failure => "error" }) end test "link_to_remote with before/after callbacks" do - assert_dom_equal %(Remote outauthor), + assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :before => "before();", :after => "after();") end test "link_to_remote using :with expression" do - expected = %(Remote outauthor) + expected = %(Remote outauthor) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :with => "id=123") end test "link_to_remote using :condition expression" do - expected = %(Remote outauthor) + expected = %(Remote outauthor) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :condition => '$(\'foo\').val() == true') end test "link_to_remote using explicit :href" do - expected = %(Remote outauthor) + expected = %(Remote outauthor) assert_dom_equal expected, link_to_remote("Remote outauthor", {:url => { :action => "whatnot" }, :condition => '$(\'foo\').val() == true'}, :href => 'http://www.example.com/testhref') end test "link_to_remote using :submit" do - expected = %(Remote outauthor) + expected = %(Remote outauthor) assert_dom_equal expected, link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :submit => 'myForm') end test "link_to_remote with method delete" do - assert_dom_equal %(Remote outauthor), + assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :method => "delete"}, { :class => "fine" }) end test "link_to_remote with method delete as symbol" do - assert_dom_equal %(Remote outauthor), + assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :method => :delete}, { :class => "fine" }) end test "link_to_remote html options" do - assert_dom_equal %(Remote outauthor), + assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) end test "link_to_remote url quote escaping" do - assert_dom_equal %(Remote), + assert_dom_equal %(Remote), link_to_remote("Remote", { :url => { :action => "whatnot's" } }) end test "link_to_remote with confirm" do - assert_dom_equal %(Remote confirm), + assert_dom_equal %(Remote confirm), link_to_remote("Remote confirm", { :url => { :action => "whatnot" }, :method => "delete", :confirm => "Are you sure?"}, { :class => "fine" }) end test "button_to_remote" do - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) end test "button_to_remote with confirm" do - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }, :confirm => "Are you sure?"}, { :class => "fine" }) end test "button_to_remote with :submit" do - assert_dom_equal %(), + assert_dom_equal %(), button_to_remote("Remote outpost", { :url => { :action => "whatnot" }, :submit => "myForm"}, { :class => "fine" }) end test "periodically_call_remote" do - assert_dom_equal %(), - periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) + expected = "" + actual = periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) + assert_dom_equal expected, actual end test "periodically_call_remote_with_frequency" do - assert_dom_equal( - "", - periodically_call_remote(:frequency => 2) - ) + expected = "" + actual = periodically_call_remote(:frequency => 2) + assert_dom_equal expected, actual end test "periodically_call_remote_with_function" do - assert_dom_equal( - "", - periodically_call_remote(:frequency => 2, :function => "alert('test')") - ) + expected = "" + actual = periodically_call_remote(:frequency => 2, :function => "alert('test')") + assert_dom_equal expected, actual + end + + test "periodically_call_remote_with_update" do + actual = periodically_call_remote(:url => { :action => 'get_averages' }, :update => 'avg') + expected = "" + assert_dom_equal expected, actual + end + + test "periodically_call_remote with update success and failure" do + actual = periodically_call_remote(:url => { :action => 'invoice', :id => 1 },:update => { :success => "invoice", :failure => "error" }) + expected = "" + assert_dom_equal expected, actual + end + + test "periodically_call_remote with frequency and update" do + actual = periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block') + expected = "" + assert_dom_equal expected, actual end test "form_remote_tag" do - assert_dom_equal %(
), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) - assert_dom_equal %(), + 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 %(), + assert_dom_equal %(), form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast }) - assert_dom_equal %(), + assert_dom_equal %(), form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) end test "form_remote_tag with method" do - assert_dom_equal %(
), + assert_dom_equal %(
), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put }) end + test "form_remote_tag with url" do + form_remote_tag(:url => '/posts' ){} + expected = "
" + assert_dom_equal expected, output_buffer + end + 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 + assert_dom_equal %(
Hello world!
), output_buffer end test "remote_form_for with record identification with new record" do remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - expected = %(
) assert_dom_equal expected, output_buffer end + test "remote_form_for with url" do + remote_form_for(@record, {:html => { :id => 'create-author' }}) {} + expected = "
" + assert_dom_equal expected, output_buffer + end + test "remote_form_for with record identification without html options" do remote_form_for(@record) {} - expected = %(
) assert_dom_equal expected, output_buffer end @@ -236,7 +306,8 @@ class AjaxHelperTest < AjaxHelperBaseTest assert_dom_equal expected, output_buffer end - test "remote_form_for with new object in list" do + test "remote_form_for with new nested object and an excisting parent" do + @author.save remote_form_for([@author, @article]) {} expected = %(
) @@ -246,94 +317,119 @@ class AjaxHelperTest < AjaxHelperBaseTest test "remote_form_for with existing object in list" do @author.save @article.save + remote_form_for([@author, @article]) {} - expected = %(
) + 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 %(
), + assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(), + assert_dom_equal %(), form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(), + assert_dom_equal %(), form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(), + 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 %(), + assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end #test 200 and 404 - assert_dom_equal %(), + 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 %(), + assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end 600.upto(999) do |callback| - assert_dom_equal %(), + assert_dom_equal %(), form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") end #test ultimate combo - assert_dom_equal %(), + 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 test "submit_to_remote" do - assert_dom_equal %(), + assert_dom_equal %(), submit_to_remote("More beer!", 1_000_000, :url => { :action => 'empty_bottle' }, :update => "empty_bottle") end + test "submit_to_remote simple" do + expected = "" + actual = submit_to_remote 'create_btn', 'Create', :url => { :action => 'create' } + assert_dom_equal expected, actual + end + + test "submit_to_remote with success and failure" do + expected = "" + actual = submit_to_remote 'update_btn', 'Update', :url => { :action => 'update' }, :update => { :success => "succeed", :failure => "fail" } + assert_dom_equal expected, actual + end + test "observe_field" do - assert_dom_equal %(), + assert_dom_equal %(), observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) end + test "observe_field with url, frequency, update and with" do + actual = observe_field :suggest, :url => { :action => :find_suggestion }, :frequency => 0.25, :update => :suggest, :with => 'q' + expected = "" + assert_dom_equal actual, expected + end + + test "observe_field default frequency" do + actual = observe_field :suggest + expected = "" + assert_dom_equal actual, expected + end + test "observe_field using with option" do - expected = %() + expected = %() assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => 'id=123') end test "observe_field using condition option" do - expected = %() + expected = %() assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :condition => '$(\'foo\').val() == true') end test "observe_field using json in with option" do - expected = %() + expected = %() assert_dom_equal expected, observe_field("glass", :frequency => 5.minutes, :url => { :action => "check_value" }, :with => "{'id':value}") end test "observe_field using function for callback" do - assert_dom_equal %(), + assert_dom_equal %(), observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") end test "observe_form" do - assert_dom_equal %(), + assert_dom_equal %(), observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) end test "observe_form using function for callback" do - assert_dom_equal %(), + assert_dom_equal %(), observe_form("cart", :frequency => 2, :function => "alert('Form changed')") end test "observe_field without frequency" do - assert_dom_equal %(), + assert_dom_equal %(), observe_field("glass") end -- cgit v1.2.3