require File.dirname(__FILE__) + '/spec_helper'
require 'merb_helpers'
include Merb::ViewContextMixin
include Merb::ErubisCaptureMixin
include Merb::Helpers::Form
describe "error_messages_for" do
before :each do
@obj = Object.new
def @obj.errors() [["foo", "bar"], ["baz", "bat"]] end
end
it "should build default error messages" do
errs = error_messages_for(@obj)
errs.should include("
Form submittal failed because of 2 problems
")
errs.should include("
foo bar
")
errs.should include("
baz bat
")
end
it "should accept a custom HTML class" do
errs = error_messages_for(@obj, nil, "foo")
errs.should include("
")
end
it "should accept a custom header block" do
errs = error_messages_for(@obj) {|errs| "
Failure: #{errs.size} issues
"}
errs.should include("
Failure: 2 issues
")
end
it "should accept a custom error list item block" do
errs = error_messages_for(@obj, proc {|err| "
#{err[0]} column: #{err[1]}
"})
errs.should include("
foo column: bar
")
end
end
describe "form_tag" do
it_should_behave_like "FakeBufferConsumer"
it "should use the post method by default" do
form_tag do
_buffer << "CONTENT"
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should include("CONTENT")
end
it "should use the get method if set" do
form_tag :method => :get do
_buffer << "CONTENT"
end
_buffer.should match_tag(:form, :method => "get")
_buffer.should include("CONTENT")
end
it "should fake out the put method if set" do
form_tag :method => :put do
_buffer << "CONTENT"
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should match_tag(:input, :type => "hidden", :name => "_method", :value => "put")
end
it "should fake out the delete method if set" do
form_tag :method => :delete do
_buffer << "CONTENT"
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should match_tag(:input, :type => "hidden", :name => "_method", :value => "delete")
end
it "should silently set method to post if an unsupported method is used" do
form_tag :method => :dodgy do
_buffer << "CONTENT"
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should_not match_tag(:input, :type => "hidden", :name => "_method", :value => "dodgy")
end
it "should take create a form" do
form_tag(:action => "foo", :method => :post) do
_buffer << "Hello"
end
_buffer.should match_tag(:form, :action => "foo", :method => "post")
_buffer.should include("Hello")
end
it "should set a form to be mutlipart" do
form_tag( :action => "foo", :method => :post, :multipart => true ) do
_buffer << "CONTENT"
end
_buffer.should match_tag( :form, :action => "foo", :method => "post", :enctype => "multipart/form-data")
_buffer.should include("CONTENT")
end
end
describe "form_for" do
it_should_behave_like "FakeBufferConsumer"
it "should wrap the contents in a form tag" do
form_for(:obj) do
_buffer << "Hello"
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should match_tag(:input, :type => "hidden", :value => "put", :name => "_method")
end
it "should set the method to post be default" do
@obj2 = FakeModel2.new
form_for(:obj2) do
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should_not match_tag(:input, :type => "hidden", :name => "_method")
end
it "should support PUT if the object passed in is not a new_record? via a hidden field" do
form_for(:obj) do
end
_buffer.should match_tag(:form, :method => "post")
_buffer.should match_tag(:input, :type => "hidden", :value => "put", :name => "_method")
end
end
describe "fields_for" do
it_should_behave_like "FakeBufferConsumer"
it "should dump the contents in the context of the object" do
fields_for(:obj) do
text_control(:foo).should match_tag(:input, :type => "text", :value => "foowee")
_buffer << "Hello"
end
_buffer.should == "Hello"
end
it "should be able to modify the context midstream" do
@obj2 = FakeModel2.new
form_for(:obj) do
text_control(:foo).should match_tag(:input, :type => "text", :value => "foowee")
fields_for(:obj2) do
text_control(:foo).should match_tag(:input, :name => "fake_model2[foo]", :type => "text", :value => "foowee2")
end
text_control(:foo).should match_tag(:input, :type => "text", :value => "foowee")
end
end
it "should handle an explicit nil attribute" do
fields_for(:obj, nil) do
_buffer << text_control(:foo)
end
_buffer.should match_tag(:input, :name => "fake_model[foo]", :value => "foowee", :type => "text")
end
end
describe "text_field (basic)" do
it_should_behave_like "FakeBufferConsumer"
it "should return a basic text field based on the values passed in" do
text_field(:name => "foo", :value => "bar").should match_tag( :input, :type => "text", :name => "foo", :value => "bar")
end
it "should wrap the field in a label if the :label option is passed to the text_field" do
result = text_field(:label => "LABEL" )
result.should match(/