require "spec_helper"
describe "BootstrapForms::FormBuilder" do
context "given a setup builder" do
before(:each) do
@project = Project.new
@template = ActionView::Base.new
@template.output_buffer = ""
@builder = BootstrapForms::FormBuilder.new(:item, @project, @template, {}, proc {})
end
describe "with no options" do
describe "error_messages" do
it "returns empty string without errors" do
@builder.error_messages.should == ""
end
context "with errors" do
before(:each) do
@project.errors.add("name")
@result = @builder.error_messages
end
it "is wrapped in error div" do
@result.should match /^
.*<\/div>$/
end
it "has a list with errors" do
@result.should match /
Name is invalid<\/li><\/ul>/
end
it "has error title" do
@result.should match /
#{I18n.t('bootstrap_forms.errors.header', :model => Project.model_name.human)}<\/h4>/
end
it "has error message on field" do
@builder.text_field("name").should == "
Name is invalid
"
end
it "joins passed error message and validation errors with ', '" do
@builder.text_field("name", :error => 'This is an error!').should == "
This is an error!, Name is invalid
"
end
end
end
describe "text_area" do
before(:each) do
@result = @builder.text_area "name"
end
it "has textarea input" do
@result.should match /textarea/
end
end
describe "check_box" do
it "generates wrapped input" do
@builder.check_box("name").should == "
"
end
it "allows custom label" do
@builder.check_box("name", :label => "custom label").should match /custom label<\/label>/
end
it "allows no label with :label => false " do
@builder.check_box("name", :label => false).should_not match /<\/label>/
end
it "allows no label with :label => '' " do
@builder.check_box("name", :label => '').should_not match /<\/label>/
end
end
describe "radio_buttons" do
it "doesn't use field_options from previously generated field" do
@builder.text_field :name, :label => 'Heading', :help_inline => 'Inline help', :help_block => 'Block help'
@builder.radio_buttons(:name, {"One"=>"1", "Two"=>"2"}).should == "
"
end
it "sets field_options" do
@builder.radio_buttons(:name, {"One" => "1", "Two" => "2"})
@builder.instance_variable_get("@field_options").should == {:error => nil}
end
it "generates wrapped input" do
@builder.radio_buttons(:name, {"One" => "1", "Two" => "2"}).should == "
"
end
it "allows custom label" do
@builder.radio_buttons(:name, {"One" => "1", "Two" => "2"}, {:label => "custom label"}).should match /custom label<\/label>/
end
end
(%w{email file number password range search text url }.map{|field| ["#{field}_field",field]} + [["telephone_field", "tel"], ["phone_field", "tel"]]).each do |field, type|
describe "#{field}" do
context "result" do
before(:each) do
@result = @builder.send(field, "name")
end
it "is wrapped" do
@result.should match /^