spec/input_spec.rb in formtastic-rails3-0.9.10.1 vs spec/input_spec.rb in formtastic-rails3-1.0.0.beta3

- old
+ new

@@ -1,7 +1,7 @@ # coding: utf-8 -require File.dirname(__FILE__) + '/spec_helper' +require 'spec_helper' describe 'SemanticFormBuilder#input' do include FormtasticSpecHelper @@ -134,17 +134,10 @@ before do ::Post.stub!(:reflect_on_validations_for).and_return([]) @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(true) end - unless defined?(Rspec) - after do - ::Post.unstub!(:reflect_on_validations_for) - @new_post.class.stub!(:method_defined?).with(:reflect_on_validations_for).and_return(false) - end - end - describe 'and validates_presence_of was called for the method' do it 'should be required' do @new_post.class.should_receive(:reflect_on_validations_for).with(:title).and_return([ mock('MacroReflection', :macro => :validates_presence_of, :name => :title, :options => nil) ]) @@ -238,119 +231,115 @@ end end end - unless defined?(Rspec) - describe 'and its a ActiveModel' do - before do - ::Post.stub!(:validators_on).and_return([]) - @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(true) - end + describe 'and its a ActiveModel' do + before do + @new_post.stub!(:class).and_return(::PostModel) + end - after do - ::Post.unstub!(:validators_on) - @new_post.class.stub!(:method_defined?).with(:validators_on).and_return(false) - end - describe 'and validates_presence_of was called for the method' do - it 'should be required' do + after do + @new_post.stub!(:class).and_return(::Post) + end + describe 'and validates_presence_of was called for the method' do + it 'should be required' do - @new_post.class.should_receive(:validators_on).with(:title).and_return([ - active_model_presence_validator([:title]) - ]) + @new_post.class.should_receive(:validators_on).with(:title).and_return([ + active_model_presence_validator([:title]) + ]) - @new_post.class.should_receive(:validators_on).with(:body).and_return([ - active_model_presence_validator([:body], {:if => true}) - ]) + @new_post.class.should_receive(:validators_on).with(:body).and_return([ + active_model_presence_validator([:body], {:if => true}) + ]) - form = semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - concat(builder.input(:body)) - end - output_buffer.concat(form) if Formtastic::Util.rails3? - output_buffer.should have_tag('form li.required') - output_buffer.should_not have_tag('form li.optional') + form = semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + concat(builder.input(:body)) end + output_buffer.concat(form) if Formtastic::Util.rails3? + output_buffer.should have_tag('form li.required') + output_buffer.should_not have_tag('form li.optional') + end - it 'should be not be required if the optional :if condition is not satisifed' do - should_be_required(:required => false, :options => { :if => false }) - end + it 'should be not be required if the optional :if condition is not satisifed' do + should_be_required(:required => false, :options => { :if => false }) + end - it 'should not be required if the optional :if proc evaluates to false' do - should_be_required(:required => false, :options => { :if => proc { |record| false } }) - end + it 'should not be required if the optional :if proc evaluates to false' do + should_be_required(:required => false, :options => { :if => proc { |record| false } }) + end - it 'should be required if the optional :if proc evaluates to true' do - should_be_required(:required => true, :options => { :if => proc { |record| true } }) - end + it 'should be required if the optional :if proc evaluates to true' do + should_be_required(:required => true, :options => { :if => proc { |record| true } }) + end - it 'should not be required if the optional :unless proc evaluates to true' do - should_be_required(:required => false, :options => { :unless => proc { |record| true } }) - end + it 'should not be required if the optional :unless proc evaluates to true' do + should_be_required(:required => false, :options => { :unless => proc { |record| true } }) + end - it 'should be required if the optional :unless proc evaluates to false' do - should_be_required(:required => true, :options => { :unless => proc { |record| false } }) - end + it 'should be required if the optional :unless proc evaluates to false' do + should_be_required(:required => true, :options => { :unless => proc { |record| false } }) + end - it 'should be required if the optional :if with a method string evaluates to true' do - @new_post.should_receive(:required_condition).and_return(true) - should_be_required(:required => true, :options => { :if => :required_condition }) - end + it 'should be required if the optional :if with a method string evaluates to true' do + @new_post.should_receive(:required_condition).and_return(true) + should_be_required(:required => true, :options => { :if => :required_condition }) + end - it 'should be required if the optional :if with a method string evaluates to false' do - @new_post.should_receive(:required_condition).and_return(false) - should_be_required(:required => false, :options => { :if => :required_condition }) - end + it 'should be required if the optional :if with a method string evaluates to false' do + @new_post.should_receive(:required_condition).and_return(false) + should_be_required(:required => false, :options => { :if => :required_condition }) + end - it 'should not be required if the optional :unless with a method string evaluates to false' do - @new_post.should_receive(:required_condition).and_return(false) - should_be_required(:required => true, :options => { :unless => :required_condition }) - end - - it 'should be required if the optional :unless with a method string evaluates to true' do - @new_post.should_receive(:required_condition).and_return(true) - should_be_required(:required => false, :options => { :unless => :required_condition }) - end + it 'should not be required if the optional :unless with a method string evaluates to false' do + @new_post.should_receive(:required_condition).and_return(false) + should_be_required(:required => true, :options => { :unless => :required_condition }) end - # TODO make a matcher for this? - def should_be_required(options) - @new_post.class.should_receive(:validators_on).with(:body).and_return([ - active_model_presence_validator([:body], options[:options]) - ]) + it 'should be required if the optional :unless with a method string evaluates to true' do + @new_post.should_receive(:required_condition).and_return(true) + should_be_required(:required => false, :options => { :unless => :required_condition }) + end + end - form = semantic_form_for(@new_post) do |builder| - concat(builder.input(:body)) - end + # TODO make a matcher for this? + def should_be_required(options) + @new_post.class.should_receive(:validators_on).with(:body).and_return([ + active_model_presence_validator([:body], options[:options]) + ]) - output_buffer.concat(form) if Formtastic::Util.rails3? + form = semantic_form_for(@new_post) do |builder| + concat(builder.input(:body)) + end - if options[:required] - output_buffer.should_not have_tag('form li.optional') - output_buffer.should have_tag('form li.required') - else - output_buffer.should have_tag('form li.optional') - output_buffer.should_not have_tag('form li.required') - end + output_buffer.concat(form) if Formtastic::Util.rails3? + + if options[:required] + output_buffer.should_not have_tag('form li.optional') + output_buffer.should have_tag('form li.required') + else + output_buffer.should have_tag('form li.optional') + output_buffer.should_not have_tag('form li.required') end + end - describe 'and validates_presence_of was not called for the method' do - before do - @new_post.class.should_receive(:validators_on).with(:title).and_return([]) - end + describe 'and validates_presence_of was not called for the method' do + before do + @new_post.class.should_receive(:validators_on).with(:title).and_return([]) + end - it 'should not be required' do - form = semantic_form_for(@new_post) do |builder| - concat(builder.input(:title)) - end - output_buffer.concat(form) if Formtastic::Util.rails3? - output_buffer.should_not have_tag('form li.required') - output_buffer.should have_tag('form li.optional') + it 'should not be required' do + form = semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) end + output_buffer.concat(form) if Formtastic::Util.rails3? + output_buffer.should_not have_tag('form li.required') + output_buffer.should have_tag('form li.optional') end - end + end describe 'and the validation reflection plugin is not available' do it 'should use the default value' do @@ -712,9 +701,26 @@ concat(builder.input(:title, :hint => false)) end output_buffer.concat(form) if Formtastic::Util.rails3? output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text) end + end + end + + describe 'when localized hint (I18n) is a model with attribute hints' do + it "should see the provided hash as a blank entry" do + ::I18n.backend.store_translations :en, + :formtastic => { + :hints => { + :title => { # movie title + :summary => @localized_hint_text # summary of movie + } + } + } + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :hint => true)) + end + output_buffer.should_not have_tag('form li p.inline-hints', @localized_hint_text) end end describe 'when localized hint (I18n) is not provided' do it 'should not render a hint paragraph' do