if ENV['RAILS_ROOT'] environment = File.expand_path('vendor/gems/environment', ENV['RAILS_ROOT']) require environment if File.exist?("#{environment}.rb") end $:.unshift File.expand_path('../../lib', __FILE__) require 'test/unit' require 'action_view' require 'action_controller' require 'active_model' require 'prototype_helper' class Bunny < Struct.new(:Bunny, :id) end class Author extend ActiveModel::Naming 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 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 Author::Nested < Author; end class PrototypeHelperTest < ActionView::TestCase attr_accessor :formats, :output_buffer, :template_format def _evaluate_assigns_and_ivars() end def reset_formats(format) @format = format end def setup @record = @author = Author.new @article = Article.new 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.new end def test_observe_form assert_dom_equal %(), observe_form("cart", :frequency => 2, :url => { :action => "cart_changed" }) end def test_observe_form_using_function_for_callback assert_dom_equal %(), observe_form("cart", :frequency => 2, :function => "alert('Form changed')") 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 def test_observe_field_without_frequency assert_dom_equal %(), observe_field("glass") end def test_periodically_call_remote assert_dom_equal %(), periodically_call_remote(:update => "schremser_bier", :url => { :action => "mehr_bier" }) end def test_periodically_call_remote_with_frequency assert_dom_equal( "", periodically_call_remote(:frequency => 2) ) end def test_form_remote_tag assert_dom_equal %(
), output_buffer end def test_on_callbacks callbacks = [:uninitialized, :loading, :loaded, :interactive, :complete, :success, :failure] callbacks.each do |callback| assert_dom_equal %() assert_dom_equal expected, output_buffer end def test_remote_form_for_with_record_identification_without_html_options remote_form_for(@record) {} expected = %() 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 = %() assert_dom_equal expected, output_buffer end def test_remote_form_for_with_new_object_in_list remote_form_for([@author, @article]) {} expected = %() 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 = %() assert_dom_equal expected, output_buffer end def test_button_to_remote 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 def test_submit_to_remote assert_dom_equal %(), submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle") end def test_link_to_remote 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 def test_link_to_remote_html_options assert_dom_equal %(Remote outauthor), link_to_remote("Remote outauthor", { :url => { :action => "whatnot" }, :html => { :class => "fine" } }) end def test_link_to_remote_url_quote_escaping assert_dom_equal %(Remote), link_to_remote("Remote", { :url => { :action => "whatnot's" } }) end protected def request_forgery_protection_token nil end def protect_against_forgery? false end def create_generator block = Proc.new { |*args| yield *args if block_given? } JavaScriptGenerator.new self, &block end 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