diff options
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/template/ajax_helper_test.rb | 452 | ||||
-rw-r--r-- | actionpack/test/template/ajax_test.rb | 114 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 12 | ||||
-rw-r--r-- | actionpack/test/template/prototype_helper_test.rb | 193 | ||||
-rw-r--r-- | actionpack/test/template/url_helper_test.rb | 18 |
6 files changed, 326 insertions, 471 deletions
diff --git a/actionpack/test/template/ajax_helper_test.rb b/actionpack/test/template/ajax_helper_test.rb deleted file mode 100644 index c925dbb8f6..0000000000 --- a/actionpack/test/template/ajax_helper_test.rb +++ /dev/null @@ -1,452 +0,0 @@ -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 - -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 reset_formats(format) - @format = format - end - - def setup - super - @template = self - @controller = Class.new do - - def url_for(options) - 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 - - protected - def request_forgery_protection_token - nil - end - - def protect_against_forgery? - false - end -end - -class AjaxHelperTest < AjaxHelperBaseTest - def _evaluate_assigns_and_ivars() end - - def setup - @record = @author = Author.new - @article = Article.new - super - end - - test "link_to_remote" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\">Remove Author</a>), - link_to_remote("Remove Author", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-oncomplete=\"alert(request.responseText)\">Remove Author</a>), - link_to_remote("Remove Author", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onsuccess=\"alert(request.responseText)\">Remove Author</a>), - link_to_remote("Remove Author", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onfailure=\"alert(request.responseText)\">Remove Author</a>), - link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot?a=10&b=20\" data-onfailure=\"alert(request.responseText)\">Remove Author</a>), - link_to_remote("Remove Author", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-remote-type=\"synchronous\">Remove Author</a>), - link_to_remote("Remove Author", :url => { :action => "whatnot" }, :type => :synchronous) - assert_dom_equal %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-update-position=\"bottom\">Remove Author</a>), - 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 = '<a href="#" data-url="/words/undo?n=5" data-remote="true" data-oncomplete="undoRequestCompleted(request)">undo</a>' - 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 = '<a class="fine" rel="nofollow" href="#" data-remote="true" data-method="delete" data-url="/whatnot">Remove Author</a>' - assert_dom_equal expected, actual - end - - test "link_to_remote using both url and href" do - expected = '<a href="/destroy" data-url="/destroy" data-update-success="posts" data-remote="true">Delete this Post</a>' - assert_dom_equal expected, link_to_remote( "Delete this Post", - { :update => "posts", - :url => { :action => "destroy" } }, - :href => url_for(:action => "destroy")) - end - - test "link_to_remote with update-success and url" do - expected = '<a href="#" data-url="/destroy" data-update-success="posts" data-update-failure="error" data-remote="true">Delete this Post</a>' - 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 %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-onbefore=\"before();\" data-onafter=\"after();\">Remote outauthor</a>), - link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :before => "before();", :after => "after();") - end - - test "link_to_remote using :with expression" do - expected = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-with=\"id=123\">Remote outauthor</a>) - 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 = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) - 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 = %(<a href=\"http://www.example.com/testhref\" data-remote=\"true\" data-url=\"/whatnot\" data-condition=\"$('foo').val() == true\">Remote outauthor</a>) - 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 = %(<a href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-submit=\"myForm\">Remote outauthor</a>) - 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 %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), - 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 %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\">Remote outauthor</a>), - link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :method => :delete}, { :class => "fine" }) - end - - test "link_to_remote html options" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\">Remote outauthor</a>), - link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) - end - - test "link_to_remote url quote escaping" do - assert_dom_equal %(<a href="#" data-remote=\"true\" data-url=\"/whatnot\\\'s\">Remote</a>), - link_to_remote("Remote", { :url => { :action => "whatnot's" } }) - end - - test "link_to_remote with confirm" do - assert_dom_equal %(<a class=\"fine\" href=\"#\" data-remote=\"true\" data-url=\"/whatnot\" data-method=\"delete\" rel=\"nofollow\" data-confirm="Are you sure?">Remote confirm</a>), - 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 %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" />), - button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-oncomplete=\"alert(request.reponseText)\" />), - button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-onsuccess=\"alert(request.reponseText)\" />), - button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-onfailure=\"alert(request.reponseText)\" />), - button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) - assert_dom_equal %(<input type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot?a=10&b=20\" data-onfailure=\"alert(request.reponseText)\" />), - 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 %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-confirm="Are you sure?" />), - 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 %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" data-remote=\"true\" data-url=\"/whatnot\" data-submit="myForm" />), - button_to_remote("Remote outpost", { :url => { :action => "whatnot" }, :submit => "myForm"}, { :class => "fine" }) - end - - test "periodically_call_remote" do - expected = "<script data-url='/mehr_bier' data-update-success='schremser_bier' type='application/json' data-frequency='10' data-periodical='true'></script>" - actual = periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) - assert_dom_equal expected, actual - end - - test "periodically_call_remote_with_frequency" do - expected = "<script data-periodical='true' type='application/json' data-frequency='2'></script>" - actual = periodically_call_remote(:frequency => 2) - assert_dom_equal expected, actual - end - - test "periodically_call_remote_with_function" do - expected = "<script data-periodical=\"true\" type=\"application/json\" data-onobserve=\"alert('test')\" data-frequency='2'></script>" - 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 = "<script data-periodical='true' data-url='/get_averages' type='application/json' data-update-success='avg' data-frequency='10'></script>" - 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 = "<script data-periodical='true' data-url='/invoice/1' type='application/json' data-update-success='invoice' data-frequency='10' data-update-failure='error'></script>" - 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 = "<script data-periodical='true' data-url='update' type='application/json' data-update-success='news_block' data-frequency='20'></script>" - assert_dom_equal expected, actual - end - - test "form_remote_tag" do - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast } ) - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">), - form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }) - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_water\">), - form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast }) - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-update-failure=\"glass_of_water\">), - 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 %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\"><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div>), - 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 = "<form action='/posts' method='post' data-remote='true'></form>" - 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 %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\">Hello world!</form>), output_buffer - end - - test "remote_form_for with record identification with new record" do - remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - expected = %(<form action='#{authors_path}' data-remote=\"true\" class='new_author' id='create-author' method='post'></form>) - assert_dom_equal expected, output_buffer - end - - test "remote_form_for with url" do - remote_form_for(@record, {:html => { :id => 'create-author' }}) {} - expected = "<form action='/authors' data-remote='true' class='new_author' id='create-author' method='post'></form>" - assert_dom_equal expected, output_buffer - end - - test "remote_form_for with record identification without html options" do - remote_form_for(@record) {} - expected = %(<form action='#{authors_path}' data-remote=\"true\" class='new_author' method='post' id='new_author'></form>) - assert_dom_equal expected, output_buffer - end - - test "remote_form_for with record identification with existing record" do - @record.save - remote_form_for(@record) {} - - expected = %(<form action='#{author_path(@record)}' id='edit_author_1' method='post' data-remote=\"true\" class='edit_author'><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>) - assert_dom_equal expected, output_buffer - end - - test "remote_form_for with new nested object and an excisting parent" do - @author.save - remote_form_for([@author, @article]) {} - - expected = %(<form action='#{author_articles_path(@author)}' data-remote=\"true\" class='new_article' method='post' id='new_article'></form>) - assert_dom_equal expected, output_buffer - end - - test "remote_form_for with existing object in list" do - @author.save - @article.save - - remote_form_for([@author, @article]) {} - - expected = %(<form action='#{author_article_path(@author, @article)}' id='edit_article_#{@article.id}' method='post' data-remote=\"true\" class='edit_article'><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>) - 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 action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-failure=\"glass_of_water\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - 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 action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - end - - #test 200 and 404 - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-on404=\"bananas();\">), - 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 action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - end - 600.upto(999) do |callback| - assert_dom_equal %(<form action=\"/fast\" method=\"post\" data-remote=\"true\" data-update-success=\"glass_of_beer\" data-on#{callback}=\"monkeys();\">), - form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") - end - - #test ultimate combo - assert_dom_equal %(<form data-on404=\"bananas();\" method=\"post\" data-onsuccess=\"s()\" action=\"/fast\" data-oncomplete=\"c();\" data-update-success=\"glass_of_beer\" data-on200=\"monkeys();\" data-onloading=\"c1()\" data-remote=\"true\" data-onfailure=\"f();\">), - 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 %(<input name=\"More beer!\" type=\"button\" value=\"1000000\" data-url=\"/empty_bottle\" data-remote-submit=\"true\" data-update-success=\"empty_bottle\" />), - submit_to_remote("More beer!", 1_000_000, :url => { :action => 'empty_bottle' }, :update => "empty_bottle") - end - - test "submit_to_remote simple" do - expected = "<input name='create_btn' type='button' value='Create' data-remote-submit='true' data-url='/create' />" - 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 = "<input name='update_btn' data-url='/update' data-remote-submit='true' data-update-failure='fail' data-update-success='succeed' value='Update' type='button' />" - 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 %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/reorder_if_empty\"></script>), - 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 = "<script type='text/javascript' data-observe='true' data-observed='suggest' data-frequency='0.25' type='application/json' data-url='/find_suggestion' data-update-success='suggest' data-with='q'></script>" - assert_dom_equal actual, expected - end - - test "observe_field default frequency" do - actual = observe_field :suggest - expected = "<script type='text/javascript' data-observe='true' data-observed='suggest' data-frequency='10' type='application/json'></script>" - assert_dom_equal actual, expected - end - - test "observe_field using with option" do - expected = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/check_value\" data-with=\"id=123\"></script>) - 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 = %(<script type=\"text/javascript\" data-observe=\"true\" data-observed=\"glass\" data-frequency=\"300\" type=\"application/json\" data-url=\"/check_value\" data-condition=\"$('foo').val() == true\"></script>) - 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 = %(<script data-with=\"{'id':value}\" data-observed=\"glass\" data-url=\"/check_value\" data-observe=\"true\" type=\"application/json\" data-frequency=\"300\"></script>) - 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 %(<script data-observed=\"glass\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Element changed')\" data-frequency=\"300\"></script>), - observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") - end - - test "observe_form" do - assert_dom_equal %(<script data-observed=\"cart\" data-url=\"/cart_changed\" data-observe=\"true\" type=\"application/json\" data-frequency=\"2\"></script>), - observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) - end - - test "observe_form using function for callback" do - assert_dom_equal %(<script data-observed=\"cart\" data-observe=\"true\" type=\"application/json\" data-onobserve=\"alert('Form changed')\" data-frequency=\"2\"></script>), - observe_form("cart", :frequency => 2, :function => "alert('Form changed')") - end - - test "observe_field without frequency" do - assert_dom_equal %(<script data-observed=\"glass\" data-observe=\"true\" type=\"application/json\" data-frequency='10'></script>), - observe_field("glass") - 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 diff --git a/actionpack/test/template/ajax_test.rb b/actionpack/test/template/ajax_test.rb new file mode 100644 index 0000000000..aeb7c09b09 --- /dev/null +++ b/actionpack/test/template/ajax_test.rb @@ -0,0 +1,114 @@ +require "abstract_unit" + +class AjaxTestCase < ActiveSupport::TestCase + include ActionView::Helpers::AjaxHelper + include ActionView::Helpers::TagHelper + + def assert_html(html, matches) + matches.each do |match| + assert_match(Regexp.new(Regexp.escape(match)), html) + end + end + + def self.assert_callbacks_work(&blk) + define_method(:assert_callbacks_work, &blk) + + [: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\)") + end + end + end +end + +class LinkToRemoteTest < AjaxTestCase + def url_for(hash) + "/blog/destroy/4" + end + + def link(options = {}) + link_to_remote("Delete this post", "/blog/destroy/3", options) + end + + test "with no update" do + assert_html link, %w(href="/blog/destroy/4" Delete\ this\ post data-remote="true") + end + + test "basic" do + assert_html link(:update => "#posts"), + %w(data-update-success="#posts") + end + + test "using a url hash" do + link = link_to_remote("Delete this post", {:controller => :blog}, :update => "#posts") + assert_html link, %w(href="/blog/destroy/4" data-update-success="#posts") + end + + test "with :html options" do + expected = %{<a href="/blog/destroy/4" data-custom="me" data-remote="true" data-update-success="#posts">Delete this post</a>} + assert_equal expected, link(:update => "#posts", :html => {"data-custom" => "me"}) + 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) + end + + test "with positional parameters" do + link = link(:position => :top, :update => "#posts") + assert_match(/data\-update\-position="top"/, link) + end + + test "with an optional method" do + link = link(:method => "delete") + assert_match(/data-method="delete"/, link) + end + + class LegacyLinkToRemoteTest < AjaxTestCase + include ActionView::Helpers::AjaxHelper::Rails2Compatibility + + def link(options) + link_to_remote("Delete this post", "/blog/destroy/3", options) + end + + test "basic link_to_remote with :url =>" do + expected = %{<a href="/blog/destroy/3" data-remote="true" data-update-success="#posts">Delete this post</a>} + assert_equal expected, + link_to_remote("Delete this post", :url => "/blog/destroy/3", :update => "#posts") + end + + assert_callbacks_work do |callback| + link(callback => "undoRequestCompleted(request)") + end + end +end + +class ButtonToRemoteTest < AjaxTestCase + def button(options, html = {}) + button_to_remote("Remote outpost", options, html) + end + + def url_for(*) + "/whatnot" + end + + class StandardTest < ButtonToRemoteTest + test "basic" do + button = button({:url => {:action => "whatnot"}}, {:class => "fine"}) + [/input/, /class="fine"/, /type="button"/, /value="Remote outpost"/, + /data-url="\/whatnot"/].each do |match| + assert_match(match, button) + end + end + end + + class LegacyButtonToRemoteTest < ButtonToRemoteTest + include ActionView::Helpers::AjaxHelper::Rails2Compatibility + + assert_callbacks_work do |callback| + button(callback => "undoRequestCompleted(request)") + end + end +end diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 147d3dc05d..0c5c5d17ee 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -1232,7 +1232,7 @@ class FormHelperTest < ActionView::TestCase # Perhaps this test should be moved to prototype helper tests. def test_remote_form_for_with_labelled_builder - self.extend ActionView::Helpers::AjaxHelper + self.extend ActionView::Helpers::PrototypeHelper remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f| concat f.text_field(:title) @@ -1241,7 +1241,7 @@ class FormHelperTest < ActionView::TestCase end expected = - %(<form action="http://www.example.com" data-remote="true" method="post">) + + %(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) + "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" + "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" + "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" + @@ -1397,10 +1397,10 @@ class FormHelperTest < ActionView::TestCase end def test_remote_form_for_with_html_options_adds_options_to_form_tag - self.extend ActionView::Helpers::AjaxHelper + self.extend ActionView::Helpers::PrototypeHelper remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end - expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" data-remote=\"true\"></form>" + expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>" assert_dom_equal expected, output_buffer end diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index c8d929cee8..47462b1237 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -285,35 +285,35 @@ class FormTagHelperTest < ActionView::TestCase def test_submit_tag assert_dom_equal( - %(<input name='commit' data-disable-with="Saving..." onclick="alert('hello!');" type="submit" value="Save" />), - submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!');") + %(<input name='commit' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';alert('hello!');result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit" value="Save" />), + submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')") ) end def test_submit_tag_with_no_onclick_options assert_dom_equal( - %(<input name='commit' data-disable-with="Saving..." type="submit" value="Save" />), + %(<input name='commit' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit" value="Save" />), submit_tag("Save", :disable_with => "Saving...") ) end def test_submit_tag_with_confirmation assert_dom_equal( - %(<input name='commit' type='submit' value='Save' data-confirm="Are you sure?"/>), + %(<input name='commit' type='submit' value='Save' onclick="if (!confirm('Are you sure?')) return false; return true;"/>), submit_tag("Save", :confirm => "Are you sure?") ) end def test_submit_tag_with_confirmation_and_with_disable_with assert_dom_equal( - %(<input name="commit" data-confirm="Are you sure?" data-disable-with="Saving..." type="submit" value="Save" />), + %(<input name="commit" onclick="if (!confirm('Are you sure?')) return false; if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = document.createElement('input');hiddenCommit.type = 'hidden';hiddenCommit.value = this.value;hiddenCommit.name = this.name;this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" type="submit" value="Save" />), submit_tag("Save", :disable_with => "Saving...", :confirm => "Are you sure?") ) end def test_image_submit_tag_with_confirmation assert_dom_equal( - %(<input type="image" src="/images/save.gif" data-confirm="Are you sure?"/>), + %(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>), image_submit_tag("save.gif", :confirm => "Are you sure?") ) end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 86f9c231c0..9225153798 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -82,6 +82,199 @@ class PrototypeHelperTest < PrototypeHelperBaseTest super end + def test_link_to_remote + 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>), + link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }}, { :class => "fine" }) + 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>), + link_to_remote("Remote outauthor", :complete => "alert(request.responseText)", :url => { :action => "whatnot" }) + 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>), + link_to_remote("Remote outauthor", :success => "alert(request.responseText)", :url => { :action => "whatnot" }) + 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>), + link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot" }) + 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>), + link_to_remote("Remote outauthor", :failure => "alert(request.responseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + assert_dom_equal %(<a href=\"#\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:false, evalScripts:true}); return false;\">Remote outauthor</a>), + link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :type => :synchronous) + 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>), + link_to_remote("Remote outauthor", :url => { :action => "whatnot" }, :position => :bottom) + end + + def test_link_to_remote_html_options + 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>), + link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) + end + + def test_link_to_remote_url_quote_escaping + assert_dom_equal %(<a href="#" onclick="new Ajax.Request('http://www.example.com/whatnot\\\'s', {asynchronous:true, evalScripts:true}); return false;">Remote</a>), + link_to_remote("Remote", { :url => { :action => "whatnot's" } }) + end + + def test_button_to_remote + assert_dom_equal %(<input class=\"fine\" type=\"button\" value=\"Remote outpost\" onclick=\"new Ajax.Request('http://www.example.com/whatnot', {asynchronous:true, evalScripts:true});\" />), + button_to_remote("Remote outpost", { :url => { :action => "whatnot" }}, { :class => "fine" }) + 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)}});\" />), + button_to_remote("Remote outpost", :complete => "alert(request.reponseText)", :url => { :action => "whatnot" }) + 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)}});\" />), + button_to_remote("Remote outpost", :success => "alert(request.reponseText)", :url => { :action => "whatnot" }) + 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)}});\" />), + button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot" }) + 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)}});\" />), + button_to_remote("Remote outpost", :failure => "alert(request.reponseText)", :url => { :action => "whatnot", :a => '10', :b => '20' }) + end + + def test_periodically_call_remote + 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>), + periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) + end + + def test_periodically_call_remote_with_frequency + assert_dom_equal( + "<script type=\"text/javascript\">\n//<![CDATA[\nnew PeriodicalExecuter(function() {new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true})}, 2)\n//]]>\n</script>", + periodically_call_remote(:frequency => 2) + ) + end + + def test_form_remote_tag + 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;\">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) + 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;\">), + form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }) + 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;\">), + form_remote_tag(:update => { :failure => "glass_of_water" }, :url => { :action => :fast }) + 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;\">), + form_remote_tag(:update => { :success => 'glass_of_beer', :failure => "glass_of_water" }, :url => { :action => :fast }) + end + + def test_form_remote_tag_with_method + 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>), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :html => { :method => :put }) + end + + def test_form_remote_tag_with_block_in_erb + __in_erb_template = '' + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }) { concat "Hello world!" } + 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 + end + + def test_remote_form_for_with_record_identification_with_new_record + remote_form_for(@record, {:html => { :id => 'create-author' }}) {} + + 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>) + assert_dom_equal expected, output_buffer + end + + def test_remote_form_for_with_record_identification_without_html_options + remote_form_for(@record) {} + + 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>) + assert_dom_equal expected, output_buffer + end + + def test_remote_form_for_with_record_identification_with_existing_record + @record.save + remote_form_for(@record) {} + + 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>) + assert_dom_equal expected, output_buffer + end + + def test_remote_form_for_with_new_object_in_list + remote_form_for([@author, @article]) {} + + 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>) + assert_dom_equal expected, output_buffer + end + + def test_remote_form_for_with_existing_object_in_list + @author.save + @article.save + remote_form_for([@author, @article]) {} + + 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>) + assert_dom_equal expected, output_buffer + end + + def test_on_callbacks + callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] + callbacks.each do |callback| + 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;">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + 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;">), + form_remote_tag(:update => { :success => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") + 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;">), + form_remote_tag(:update => { :failure => "glass_of_beer" }, :url => { :action => :fast }, callback=>"monkeys();") + 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;">), + 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 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;">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + end + + #test 200 and 404 + 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;">), + 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 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;">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + end + 600.upto(999) do |callback| + 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;">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, callback=>"monkeys();") + end + + #test ultimate combo + 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;\">), + form_remote_tag(:update => "glass_of_beer", :url => { :action => :fast }, :loading => "c1()", :success => "s()", :failure => "f();", :complete => "c();", 200=>"monkeys();", 404=>"bananas();") + + end + + def test_submit_to_remote + 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\" />), + submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle") + end + + def test_observe_field + 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>), + observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) + end + + def test_observe_field_using_with_option + 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>) + 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 = %(<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>) + 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 %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Element.Observer('glass', 300, function(element, value) {alert('Element changed')})\n//]]>\n</script>), + observe_field("glass", :frequency => 5.minutes, :function => "alert('Element changed')") + end + + def test_observe_form + 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>), + observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) + end + + def test_observe_form_using_function_for_callback + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Form.Observer('cart', 2, function(element, value) {alert('Form changed')})\n//]]>\n</script>), + observe_form("cart", :frequency => 2, :function => "alert('Form changed')") + end + + def test_observe_field_without_frequency + 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>), + observe_field("glass") + end + def test_update_page old_output_buffer = output_buffer diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 984240d46f..dd28aec786 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -76,7 +76,7 @@ class UrlHelperTest < ActionView::TestCase def test_button_to_with_javascript_confirm assert_dom_equal( - "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input data-confirm=\"Are you sure?\" type=\"submit\" value=\"Hello\" /></div></form>", + "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure?');\" type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com", :confirm => "Are you sure?") ) end @@ -170,50 +170,50 @@ class UrlHelperTest < ActionView::TestCase def test_link_tag_with_javascript_confirm assert_dom_equal( - "<a href=\"http://www.example.com\" data-confirm=\"Are you sure?\">Hello</a>", + "<a href=\"http://www.example.com\" onclick=\"return confirm('Are you sure?');\">Hello</a>", link_to("Hello", "http://www.example.com", :confirm => "Are you sure?") ) assert_dom_equal( - "<a href=\"http://www.example.com\" data-confirm=\"You can\'t possibly be sure, can you?\">Hello</a>", + "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure, can you?');\">Hello</a>", link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure, can you?") ) assert_dom_equal( - "<a href=\"http://www.example.com\" data-confirm=\"You can\'t possibly be sure,\n can you?\">Hello</a>", + "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure,\\n can you?');\">Hello</a>", link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure,\n can you?") ) end def test_link_tag_using_post_javascript assert_dom_equal( - "<a href='http://www.example.com' data-url='http://www.example.com' data-method=\"post\">Hello</a>", + "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>", link_to("Hello", "http://www.example.com", :method => :post) ) end def test_link_tag_using_delete_javascript assert_dom_equal( - "<a href='http://www.example.com' data-url='http://www.example.com' rel=\"nofollow\" data-method=\"delete\">Destroy</a>", + "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>", link_to("Destroy", "http://www.example.com", :method => :delete) ) end def test_link_tag_using_delete_javascript_and_href assert_dom_equal( - "<a href='\#' data-url='http://www.example.com' rel=\"nofollow\" data-method=\"delete\">Destroy</a>", + "<a href='\#' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = 'http://www.example.com';var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>", link_to("Destroy", "http://www.example.com", :method => :delete, :href => '#') ) end def test_link_tag_using_post_javascript_and_confirm assert_dom_equal( - "<a href=\"http://www.example.com\" data-url='http://www.example.com' data-method=\"post\" data-confirm=\"Are you serious?\">Hello</a>", + "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>", link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?") ) end def test_link_tag_using_delete_javascript_and_href_and_confirm assert_dom_equal( - "<a href=\"#\" data-url='http://www.example.com' rel=\"nofollow\" data-method=\"delete\" data-confirm=\"Are you serious?\">Destroy</a>", + "<a href='\#' onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = 'http://www.example.com';var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit(); };return false;\">Destroy</a>", link_to("Destroy", "http://www.example.com", :method => :delete, :href => '#', :confirm => "Are you serious?"), "When specifying url, form should be generated with it, but not this.href" ) |