Skip to content

Commit

Permalink
remote_form_for tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
foobarfighter authored and stefanpenner committed Jan 27, 2010
1 parent 118a720 commit 2981153
Showing 1 changed file with 93 additions and 10 deletions.
103 changes: 93 additions & 10 deletions actionpack/test/javascript/ajax_test.rb
@@ -1,21 +1,18 @@
require "abstract_unit"

class AjaxTestCase < ActiveSupport::TestCase
include ActionView::Helpers::AjaxHelper
#TODO: Switch to assert_dom_equal where appropriate. assert_html is not robust enough for all tests - BR

# TODO: Ask Katz: Should these be included by the AjaxHelper? - BR
include ActionView::Helpers::TagHelper
include ActionView::Helpers::FormTagHelper
class AjaxTestCase < ActionView::TestCase
include ActionView::Helpers::AjaxHelper

# TODO: Replace with the real url_for method - BR
def url_for(url)
case url
def url_for(options)
case options
when Hash
"/url/hash"
when String
url
options
else
raise TypeError.new("Unsupported url type (#{url.class}) for this test helper")
raise TypeError.new("Unsupported url type (#{options.class}) for this test helper")
end
end

Expand Down Expand Up @@ -218,7 +215,93 @@ def concat(str)
expected_form_attributes + expected_inner_html
end

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 RemoteFormForTest < AjaxTestCase

def setup
super
@record = @author = Author.new
@article = Article.new
end

test "remote_form_for with record identification with new record" 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" data-remote="true" class="new_author" id="new_author" method="post"></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="/authors/1" data-remote="true" class="edit_author" id="edit_author_1" method="post"><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 object in list" do
remote_form_for([@author, @article]) {}

expected = %(<form action="#{author_articles_path(@author)}" class="new_article" method="post" id="new_article" data-remote="true"></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_1' method='post' class='edit_article' data-remote="true"><div style='margin:0;padding:0;display:inline'><input name='_method' type='hidden' value='put' /></div></form>)
assert_dom_equal expected, output_buffer
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

class ButtonToRemoteTest < AjaxTestCase
Expand Down

0 comments on commit 2981153

Please sign in to comment.