Permalink
Browse files

Add size attribute specs to stringish

  • Loading branch information...
1 parent 0ab6f67 commit 80725e918b4e93b9627c28afb9b69e22f7ad8c20 @haines haines committed Jan 3, 2012
View
203 spec/inputs/string_input_spec.rb
@@ -1,208 +1,9 @@
# encoding: utf-8
require 'spec_helper'
-describe 'string input' do
+describe_input :string, :type => :text do
- include FormtasticSpecHelper
-
- before do
- @output_buffer = ''
- mock_everything
- end
-
- describe "when object is provided" do
- before do
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :string))
- end)
- end
-
- it_should_have_input_wrapper_with_class(:string)
- it_should_have_input_wrapper_with_class(:input)
- it_should_have_input_wrapper_with_class(:stringish)
- it_should_have_input_wrapper_with_id("post_title_input")
- it_should_have_label_with_text(/Title/)
- it_should_have_label_for("post_title")
- it_should_have_input_with_id("post_title")
- it_should_have_input_with_type(:text)
- it_should_have_input_with_name("post[title]")
- it_should_have_maxlength_matching_column_limit
- it_should_use_default_text_field_size_when_not_nil(:string)
- it_should_not_use_default_text_field_size_when_nil(:string)
- it_should_apply_custom_input_attributes_when_input_html_provided(:string)
- it_should_apply_custom_for_to_label_when_input_html_id_provided(:string)
- it_should_apply_error_logic_for_input_type(:string)
-
- def input_field_for_method_should_have_maxlength(method, maxlength)
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(method))
- end)
- output_buffer.should have_tag("form li input[@maxlength='#{maxlength}']")
- end
-
- describe 'and its a ActiveModel' do
- let(:default_maxlength) { 50 }
-
- before do
- @new_post.stub!(:class).and_return(::PostModel)
- end
-
- after do
- @new_post.stub!(:class).and_return(::Post)
- end
-
- describe 'and validates_length_of was called for the method' do
- def should_have_maxlength(maxlength, options)
- @new_post.class.should_receive(:validators_on).with(:title).at_least(1).and_return([
- active_model_length_validator([:title], options[:options])
- ])
-
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title))
- end)
-
- output_buffer.should have_tag("form li input##{@new_post.class.name.underscore}_title[@maxlength='#{maxlength}']")
- end
-
- it 'should have maxlength if the optional :if or :unless options are not supplied' do
- should_have_maxlength(42, :options => {:maximum => 42})
- end
-
- it 'should have default maxlength if the optional :if condition is not satisifed' do
- should_have_maxlength(default_maxlength, :options => {:maximum => 42, :if => false})
- end
-
- it 'should have default_maxlength if the optional :if proc evaluates to false' do
- should_have_maxlength(default_maxlength, :options => {:maximum => 42, :if => proc { |record| false }})
- end
-
- it 'should have maxlength if the optional :if proc evaluates to true' do
- should_have_maxlength(42, :options => { :maximum => 42, :if => proc { |record| true } })
- end
-
- it 'should have default maxlength if the optional :if with a method name evaluates to false' do
- @new_post.should_receive(:specify_maxlength).at_least(1).and_return(false)
- should_have_maxlength(default_maxlength, :options => { :maximum => 42, :if => :specify_maxlength })
- end
-
- it 'should have maxlength if the optional :if with a method name evaluates to true' do
- @new_post.should_receive(:specify_maxlength).at_least(1).and_return(true)
- should_have_maxlength(42, :options => { :maximum => 42, :if => :specify_maxlength })
- end
-
- it 'should have default maxlength if the optional :unless proc evaluates to true' do
- should_have_maxlength(default_maxlength, :options => { :maximum => 42, :unless => proc { |record| true } })
- end
-
- it 'should have maxlength if the optional :unless proc evaluates to false' do
- should_have_maxlength(42, :options => { :maximum => 42, :unless => proc { |record| false } })
- end
-
- it 'should have default maxlength if the optional :unless with a method name evaluates to true' do
- @new_post.should_receive(:specify_maxlength).at_least(1).and_return(true)
- should_have_maxlength(default_maxlength, :options => { :maximum => 42, :unless => :specify_maxlength })
- end
-
- it 'should have maxlength if the optional :unless with a method name evaluates to false' do
- @new_post.should_receive(:specify_maxlength).at_least(1).and_return(false)
- should_have_maxlength(42, :options => { :maximum => 42, :unless => :specify_maxlength })
- end
- end
- end
- end
-
- describe "when namespace is provided" do
-
- before do
- concat(semantic_form_for(@new_post, :namespace => 'context2') do |builder|
- concat(builder.input(:title, :as => :string))
- end)
- end
-
- it_should_have_input_wrapper_with_id("context2_post_title_input")
- it_should_have_label_and_input_with_id("context2_post_title")
-
- end
-
- describe "when index is provided" do
-
- before do
- @output_buffer = ''
- mock_everything
-
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.fields_for(:author, :index => 3) do |author|
- concat(author.input(:name, :as => :string))
- end)
- end)
- end
-
- it 'should index the id of the wrapper' do
- output_buffer.should have_tag("li#post_author_attributes_3_name_input")
- end
-
- it 'should index the id of the select tag' do
- output_buffer.should have_tag("input#post_author_attributes_3_name")
- end
-
- it 'should index the name of the select tag' do
- output_buffer.should have_tag("input[@name='post[author_attributes][3][name]']")
- end
-
- end
-
-
- describe "when no object is provided" do
- before do
- concat(semantic_form_for(:project, :url => 'http://test.host/') do |builder|
- concat(builder.input(:title, :as => :string))
- end)
- end
-
- it_should_have_label_with_text(/Title/)
- it_should_have_label_for("project_title")
- it_should_have_input_with_id("project_title")
- it_should_have_input_with_type(:text)
- it_should_have_input_with_name("project[title]")
- end
-
- describe "when size is nil" do
- before do
- concat(semantic_form_for(:project, :url => 'http://test.host/') do |builder|
- concat(builder.input(:title, :as => :string, :input_html => {:size => nil}))
- end)
- end
-
- it "should have no size attribute" do
- output_buffer.should_not have_tag("input[@size]")
- end
- end
-
- describe "when required" do
-
- context "and configured to use HTML5 attribute" do
- it "should add the required attribute to the input's html options" do
- with_config :use_required_attribute, true do
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :string, :required => true))
- end)
- output_buffer.should have_tag("input[@required]")
- end
- end
- end
-
- context "and configured to not use HTML5 attribute" do
- it "should add the required attribute to the input's html options" do
- with_config :use_required_attribute, false do
- concat(semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :string, :required => true))
- end)
- output_buffer.should_not have_tag("input[@required]")
- end
- end
- end
-
- end
+ it_behaves_like "a stringish input"
end
View
84 spec/support/custom_macros/with_validator.rb
@@ -0,0 +1,84 @@
+# encoding: utf-8
+module CustomMacros
+
+ class WithValidator
+ attr_accessor :when_applicable, :when_not_applicable
+
+ def when_applicable_it(description, &example)
+ self.when_applicable = {:description => description, :example => example}
+ end
+
+ def when_not_applicable_it(description, &example)
+ self.when_not_applicable = {:description => description, :example => example}
+ end
+ end
+
+ def with_validator(kind, options={}, &examples)
+ behavior = WithValidator.new
+ behavior.instance_eval(&examples)
+
+ options_description = options.keys.map{|key| key.inspect if options[key]}.compact.join(", ").presence || "no options"
+
+ true_conditionals = {"by a value" => true, "from a proc" => lambda {|o| true}, "from a method" => :true_method}
+ false_conditionals = {"by a value" => false, "from a proc" => lambda {|o| false}, "from a method" => :false_method}
+
+ describe "with a #{kind} validator with #{options_description} set" do
+ before do
+ add_validator(kind, options.merge(conditionals))
+ model_object.stub(:true_method).and_return(true)
+ model_object.stub(:false_method).and_return(false)
+ end
+
+ let(:conditionals) { {} }
+
+ it(behavior.when_applicable[:description], &behavior.when_applicable[:example])
+
+ describe "and :if is set to true" do
+ true_conditionals.each do |key, value|
+ describe key do
+ let(:conditionals) { {:if => value} }
+ it(behavior.when_applicable[:description], &behavior.when_applicable[:example])
+ end
+ end
+ end
+
+ describe "and :if is set to false" do
+ false_conditionals.each do |key, value|
+ describe key do
+ let(:conditionals) { {:if => value} }
+ it(behavior.when_not_applicable[:description], &behavior.when_not_applicable[:example])
+ end
+ end
+ end
+
+ describe "and :unless is set to false" do
+ false_conditionals.each do |key, value|
+ describe key do
+ let(:conditionals) { {:unless => value} }
+ it(behavior.when_applicable[:description], &behavior.when_applicable[:example])
+ end
+ end
+ end
+
+ describe "and :unless is set to true" do
+ before { model_object.stub(:unless_true).and_return(true) }
+
+ true_conditionals.each do |key, value|
+ describe key do
+ let(:conditionals) { {:unless => value} }
+ it(behavior.when_not_applicable[:description], &behavior.when_not_applicable[:example])
+ end
+ end
+ end
+ end
+ end
+
+ def with_length_validator(options={}, &examples)
+ with_validator(:length, options, &examples)
+ end
+
+ def with_numericality_validator(options={}, &examples)
+ with_validator(:length, options, &examples)
+ end
+
+end
View
89 spec/support/shared_examples/stringish_input.rb
@@ -26,4 +26,93 @@
end
end
+ describe "size attribute" do
+ describe "when default_text_field_size is set" do
+ set_config(:default_text_field_size, 30)
+
+ it "is equal to the default" do
+ should have_input.with_size(30)
+ end
+
+ describe "and :size provided in :input_html" do
+ let(:input_options) { {:input_html => {:size => 20}} }
+
+ it "overrides the default" do
+ should have_input.with_size(20)
+ end
+
+ describe "and is nil" do
+ let(:input_options) { {:input_html => {:size => nil}} }
+
+ it "suppresses the attribute" do
+ should_not have_input.with_size
+ end
+ end
+ end
+ end
+
+ describe "when default_text_field_size is nil" do
+ set_config(:default_text_field_size, nil)
+
+ it "suppresses the attribute" do
+ should_not have_input.with_size
+ end
+
+ describe "and :size provided in :input_html" do
+ let(:input_options) { {:input_html => {:size => 20}} }
+
+ it "overrides the default" do
+ should have_input.with_size(20)
+ end
+
+ describe "and is nil" do
+ let(:input_options) { {:input_html => {:size => nil}} }
+
+ it "suppresses the attribute" do
+ should_not have_input.with_size
+ end
+ end
+ end
+ end
+ end
+
+ describe "when validates" do
+ before { set_column_type(:string, :limit => 50) }
+
+ [:is, :maximum].each do |bound|
+ with_length_validator(bound => 42) do
+ when_applicable_it "adds a maxlength attribute equal to the validation" do
+ should have_input.with_maxlength(42)
+ end
+
+ when_not_applicable_it "retains the default maxlength" do
+ should have_input.with_maxlength(50)
+ end
+ end
+ end
+
+ with_length_validator(:minimum => 42) do
+ when_applicable_it "retains the default maxlength" do
+ should have_input.with_maxlength(50)
+ end
+
+ when_not_applicable_it "retains the default maxlength" do
+ should have_input.with_maxlength(50)
+ end
+ end
+
+ [:in, :within].each do |range|
+ with_length_validator(range => 24..42) do
+ when_applicable_it "adds a maxlength attribute equal to the validation" do
+ should have_input.with_maxlength(42)
+ end
+
+ when_not_applicable_it "retains the default maxlength" do
+ should have_input.with_maxlength(50)
+ end
+ end
+ end
+
+ end
+
end

0 comments on commit 80725e9

Please sign in to comment.