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
context 'with no options' do
it_behaves_like 'a bootstrap form'
end
context 'with the :namespace option' do
before(:each) do
@builder.options[:namespace] = 'foo'
end
it 'prefixs HTML ids correctly' do
@builder.text_field('name').should match /<(input) .*id="foo_item_name"/
end
end
context 'with the :index option' do
before(:each) do
@builder.options[:index] = 69
end
it 'uses the correct :index' do
@builder.text_field('name').should match / 'foo' }
end
it 'does not add the class to the elements' do
@builder.text_field('name').should_not match /.*<\/div>$/
end
it 'have a list with errors' do
@result.should match /
Name is invalid<\/li><\/ul>/
end
it 'have error title' do
@result.should match /
#{I18n.t('bootstrap_forms.errors.header', :model => Project.model_name.human)}<\/h4>/
end
it 'have 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
context "errors with translations" do
before(:all) { I18n.backend.store_translations I18n.locale, {:errors => {:format => "%{message}", :messages => {:invalid => "Nope"}}} }
after(:all) { I18n.backend.reload! }
before(:each) do
@project.errors.add('name')
@result = @builder.error_messages
end
it 'use i18n format on field error message' do
@builder.text_field('name').should match /Nope<\/span>/
end
end
context 'an attribute with a PresenceValidator' do
it 'adds the required attribute' do
@builder.text_field('owner').should match / false).should_not match / @project)
@template.output_buffer = ''
@builder = BootstrapForms::FormBuilder.new(:item, nil, @template, {}, proc {})
end
it_behaves_like 'a bootstrap form'
end
end
describe 'BootstrapForms::Helpers::FormTagHelper' do
describe '#bootstrap_text_field_tag' do
context 'with no method "validators_on"' do
before(:each) do
@non_active_record_object = {:attributes => [:id, :name]}
@template = ActionView::Base.new
@template.output_buffer = ""
@builder = BootstrapForms::FormBuilder.new(:item, @non_active_record_object, @template, {}, proc {})
end
it 'returns an empty string with no errors' do
@template.bootstrap_text_field_tag(@builder.object[:name]).should match /