Browse files

shifted all :as => :blah specs out to their own files, so we now have…

… string_input_spec.rb, etc
  • Loading branch information...
1 parent 93cd786 commit 675698e8cf12e9d2abadb9193d08c89aa8299640 @justinfrench committed Nov 12, 2009
View
1,045 spec/input_spec.rb
@@ -589,1050 +589,5 @@ def should_be_required(options)
end
end
- describe ':as => :string' do
-
- before do
- 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_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_for_columns_longer_than_default_text_field_size(:string)
- it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string)
- it_should_use_default_text_field_size_when_method_has_no_database_column(: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)
-
- describe "when no object is provided" do
- before do
- 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
-
- end
-
- describe ':as => :password' do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :password))
- end
- end
-
- it_should_have_input_wrapper_with_class(:password)
- 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(:password)
- it_should_have_input_with_name("post[title]")
- it_should_have_maxlength_matching_column_limit
- it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(:string)
- it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string)
- it_should_use_default_text_field_size_when_method_has_no_database_column(: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(:password)
-
- describe "when no object is provided" do
- before do
- semantic_form_for(:project, :url => 'http://test.host/') do |builder|
- concat(builder.input(:title, :as => :password))
- 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(:password)
- it_should_have_input_with_name("project[title]")
- end
-
- end
-
- describe ':as => :numeric' do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :numeric))
- end
- end
-
- it_should_have_input_wrapper_with_class(:numeric)
- 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_use_default_text_field_size_when_method_has_no_database_column(: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(:numeric)
-
- describe "when no object is provided" do
- before do
- semantic_form_for(:project, :url => 'http://test.host/') do |builder|
- concat(builder.input(:title, :as => :numeric))
- 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
-
- end
-
- describe ":as => :text" do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:body, :as => :text))
- end
- end
-
- it_should_have_input_wrapper_with_class("text")
- it_should_have_input_wrapper_with_id("post_body_input")
- it_should_have_label_with_text(/Body/)
- it_should_have_label_for("post_body")
- it_should_have_textarea_with_id("post_body")
- it_should_have_textarea_with_name("post[body]")
- it_should_apply_error_logic_for_input_type(:numeric)
-
- it 'should use input_html to style inputs' do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :text, :input_html => { :class => 'myclass' }))
- end
- output_buffer.should have_tag("form li textarea.myclass")
- end
-
- end
-
- describe ":as => :file" do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:body, :as => :file))
- end
- end
-
- it_should_have_input_wrapper_with_class("file")
- it_should_have_input_wrapper_with_id("post_body_input")
- it_should_have_label_with_text(/Body/)
- it_should_have_label_for("post_body")
- it_should_have_input_with_id("post_body")
- it_should_have_input_with_name("post[body]")
- it_should_apply_error_logic_for_input_type(:file)
-
- it 'should use input_html to style inputs' do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:title, :as => :file, :input_html => { :class => 'myclass' }))
- end
- output_buffer.should have_tag("form li input.myclass")
- end
-
- end
-
- describe ":as => :hidden" do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:secret, :as => :hidden))
- end
- end
-
- it_should_have_input_wrapper_with_class("hidden")
- it_should_have_input_wrapper_with_id("post_secret_input")
- it_should_not_have_a_label
-
- it "should generate a input field" do
- output_buffer.should have_tag("form li input#post_secret")
- output_buffer.should have_tag("form li input[@type=\"hidden\"]")
- output_buffer.should have_tag("form li input[@name=\"post[secret]\"]")
- end
-
- it "should not render inline errors" do
- @errors = mock('errors')
- @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"])
- @new_post.stub!(:errors).and_return(@errors)
-
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:secret, :as => :hidden))
- end
-
- output_buffer.should_not have_tag("form li p.inline-errors")
- output_buffer.should_not have_tag("form li ul.errors")
- end
-
- end
-
- describe ":as => :time_zone" do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:time_zone))
- end
- end
-
- it_should_have_input_wrapper_with_class("time_zone")
- it_should_have_input_wrapper_with_id("post_time_zone_input")
- it_should_apply_error_logic_for_input_type(:time_zone)
-
- it 'should generate a label for the input' do
- output_buffer.should have_tag('form li label')
- output_buffer.should have_tag('form li label[@for="post_time_zone"]')
- output_buffer.should have_tag('form li label', /Time zone/)
- end
-
- it "should generate a select" do
- output_buffer.should have_tag("form li select")
- output_buffer.should have_tag("form li select#post_time_zone")
- output_buffer.should have_tag("form li select[@name=\"post[time_zone]\"]")
- end
-
- it 'should use input_html to style inputs' do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:time_zone, :input_html => { :class => 'myclass' }))
- end
- output_buffer.should have_tag("form li select.myclass")
- end
-
- describe 'when no object is given' do
- before(:each) do
- semantic_form_for(:project, :url => 'http://test.host/') do |builder|
- concat(builder.input(:time_zone, :as => :time_zone))
- end
- end
-
- it 'should generate labels' do
- output_buffer.should have_tag('form li label')
- output_buffer.should have_tag('form li label[@for="project_time_zone"]')
- output_buffer.should have_tag('form li label', /Time zone/)
- end
-
- it 'should generate select inputs' do
- output_buffer.should have_tag("form li select")
- output_buffer.should have_tag("form li select#project_time_zone")
- output_buffer.should have_tag("form li select[@name=\"project[time_zone]\"]")
- end
- end
- end
-
- describe ":as => :country" do
-
- describe "when country_select is not available as a helper from a plugin" do
-
- it "should raise an error, sugesting the author installs a plugin" do
- lambda {
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:country, :as => :country))
- end
- }.should raise_error
- end
-
- end
-
- describe "when country_select is available as a helper (from a plugin)" do
-
- before do
- semantic_form_for(@new_post) do |builder|
- builder.stub!(:country_select).and_return("<select><option>...</option></select>")
- concat(builder.input(:country, :as => :country))
- end
- end
-
- it_should_have_input_wrapper_with_class("country")
- it_should_have_input_wrapper_with_id("post_country_input")
-
- # TODO -- needs stubbing inside the builder block, tricky!
- #it_should_apply_error_logic_for_input_type(:country)
-
- it 'should generate a label for the input' do
- output_buffer.should have_tag('form li label')
- output_buffer.should have_tag('form li label[@for="post_country"]')
- output_buffer.should have_tag('form li label', /Country/)
- end
-
- it "should generate a select" do
- output_buffer.should have_tag("form li select")
- end
-
- end
-
- describe ":priority_countries option" do
-
- it "should be passed down to the country_select helper when provided" do
- priority_countries = ["Foo", "Bah"]
- semantic_form_for(@new_post) do |builder|
- builder.stub!(:country_select).and_return("<select><option>...</option></select>")
- builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
-
- concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
- end
- end
-
- it "should default to the @@priority_countries config when absent" do
- priority_countries = Formtastic::SemanticFormBuilder.priority_countries
- priority_countries.should_not be_empty
- priority_countries.should_not be_nil
-
- semantic_form_for(@new_post) do |builder|
- builder.stub!(:country_select).and_return("<select><option>...</option></select>")
- builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
-
- concat(builder.input(:country, :as => :country))
- end
- end
-
- end
-
- end
-
- describe ':as => :radio' do
- describe 'for belongs_to association' do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :radio, :value_as_class => true))
- end
- end
-
- it_should_have_input_wrapper_with_class("radio")
- it_should_have_input_wrapper_with_id("post_author_input")
- it_should_have_a_nested_fieldset
- it_should_apply_error_logic_for_input_type(:radio)
- it_should_use_the_collection_when_provided(:radio, 'input')
-
-
- it 'should generate a legend containing label text for the input' do
- output_buffer.should have_tag('form li fieldset legend')
- output_buffer.should have_tag('form li fieldset legend', /Author/)
- end
-
- it 'should generate an ordered list with a list item for each choice' do
- output_buffer.should have_tag('form li fieldset ol')
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
- end
-
- it 'should have one option with a "checked" attribute' do
- output_buffer.should have_tag('form li input[@checked]', :count => 1)
- end
-
- describe "each choice" do
-
- it 'should contain a label for the radio input with a nested input and label text' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
- end
- end
-
- it 'should use values as li.class when value_as_class is true' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li fieldset ol li.#{author.id} label")
- end
- end
-
- it "should have a radio input" do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
- output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
- output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
- output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
- end
- end
-
- it "should mark input as checked if it's the the existing choice" do
- @new_post.author_id.should == @bob.id
- @new_post.author.id.should == @bob.id
- @new_post.author.should == @bob
-
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :radio))
- end
-
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
- end
- end
-
- describe 'and no object is given' do
- before(:each) do
- output_buffer.replace ''
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:author_id, :as => :radio, :collection => ::Author.find(:all)))
- end
- end
-
- it 'should generate a fieldset with legend' do
- output_buffer.should have_tag('form li fieldset legend', /Author/)
- end
-
- it 'should generate an li tag for each item in the collection' do
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
- end
-
- it 'should generate labels for each item' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
- output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
- end
- end
-
- it 'should generate inputs for each item' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
- output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
- output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
- output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']")
- end
- end
- end
- end
- end
-
-
- describe ':as => :select' do
-
- before do
- @new_post.stub!(:author).and_return(@bob)
- @new_post.stub!(:author_id).and_return(@bob.id)
- @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
- # instances for the group_by part
- @continent_names = %w(Europe Africa)
- @authors = [@bob, @fred]
- @continents = (0..1).map { |i| mock("continent", :id => (100 - i) ) }
- @authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) }
-
- @continents.each_with_index do |continent, i|
- continent.stub!(:to_label).and_return(@continent_names[i])
- continent.stub!(:authors).and_return([@authors[i]])
- end
- end
-
- [{}, { :group_by => :continent }].each do |options|
- describe 'for a belongs_to association' do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, options.merge(:as => :select) ) )
- end
- end
-
- it_should_have_input_wrapper_with_class("select")
- it_should_have_input_wrapper_with_id("post_author_input")
- it_should_have_label_with_text(/Author/)
- it_should_have_label_for('post_author_id')
-
- it 'should have a select inside the wrapper' do
- output_buffer.should have_tag('form li select')
- output_buffer.should have_tag('form li select#post_author_id')
- end
-
- it 'should not create a multi-select' do
- output_buffer.should_not have_tag('form li select[@multiple]')
- end
-
- it 'should create a select without size' do
- output_buffer.should_not have_tag('form li select[@size]')
- end
-
- it 'should have a blank option' do
- output_buffer.should have_tag("form li select option[@value='']")
- end
-
- it 'should have a select option for each Author' do
- output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
- end
- end
-
- it 'should have one option with a "selected" attribute' do
- output_buffer.should have_tag('form li select option[@selected]', :count => 1)
- end
-
- it 'should not singularize the association name' do
- @new_post.stub!(:author_status).and_return(@bob)
- @new_post.stub!(:author_status_id).and_return(@bob.id)
- @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
-
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author_status, :as => :select))
- end
-
- output_buffer.should have_tag('form li select#post_author_status_id')
- end
- end
- end
-
- describe 'for a belongs_to association and :group_by => :continent' do
- before do
- @authors = [@bob, @fred, @fred, @fred]
- ::Author.stub!(:find).and_return(@authors)
-
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :select, :group_by => :continent ) )
- concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label_method => :id ) )
- end
- end
-
- it_should_have_input_wrapper_with_class("select")
- it_should_have_input_wrapper_with_id("post_author_input")
- it_should_have_label_with_text(/Author/)
- it_should_have_label_for('post_author_id')
- it_should_apply_error_logic_for_input_type(:select)
- it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
- it_should_use_the_collection_when_provided(:select, 'option')
-
- 0.upto(1) do |i|
- it 'should have all option groups and the right values' do
- output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label)
- end
-
- it 'should have custom group labels' do
- output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label)
- end
- end
-
- it 'should have no duplicate groups' do
- output_buffer.should have_tag('form li select optgroup', :count => 4)
- end
-
- it 'should sort the groups on the label method' do
- output_buffer.should have_tag("form li select optgroup[@label='Africa']")
- output_buffer.should have_tag("form li select optgroup[@label='99']")
- end
-
-
- it 'should call find with :include for more optimized queries' do
- Author.should_receive(:find).with(:all, :include => :continent)
-
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :select, :group_by => :continent ) )
- end
- end
- end
-
- describe 'for a has_many association' do
- before do
- semantic_form_for(@fred) do |builder|
- concat(builder.input(:posts, :as => :select))
- end
- end
-
- it_should_have_input_wrapper_with_class("select")
- it_should_have_input_wrapper_with_id("author_posts_input")
- it_should_have_label_with_text(/Post/)
- it_should_have_label_for('author_post_ids')
- it_should_apply_error_logic_for_input_type(:select)
- it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
- it_should_use_the_collection_when_provided(:select, 'option')
-
- it 'should have a select inside the wrapper' do
- output_buffer.should have_tag('form li select')
- output_buffer.should have_tag('form li select#author_post_ids')
- end
-
- it 'should have a multi-select select' do
- output_buffer.should have_tag('form li select[@multiple="multiple"]')
- end
-
- it 'should have a select option for each Post' do
- output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size)
- ::Post.find(:all).each do |post|
- output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
- end
- end
-
- it 'should not have a blank option' do
- output_buffer.should_not have_tag("form li select option[@value='']")
- end
-
- it 'should have one option with a "selected" attribute' do
- output_buffer.should have_tag('form li select option[@selected]', :count => 1)
- end
- end
-
- describe 'for a has_and_belongs_to_many association' do
- before do
- semantic_form_for(@freds_post) do |builder|
- concat(builder.input(:authors, :as => :select))
- end
- end
-
- it_should_have_input_wrapper_with_class("select")
- it_should_have_input_wrapper_with_id("post_authors_input")
- it_should_have_label_with_text(/Author/)
- it_should_have_label_for('post_author_ids')
- it_should_apply_error_logic_for_input_type(:select)
- it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
- it_should_use_the_collection_when_provided(:select, 'option')
-
- it 'should have a select inside the wrapper' do
- output_buffer.should have_tag('form li select')
- output_buffer.should have_tag('form li select#post_author_ids')
- end
-
- it 'should have a multi-select select' do
- output_buffer.should have_tag('form li select[@multiple="multiple"]')
- end
-
- it 'should have a select option for each Author' do
- output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size)
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
- end
- end
-
- it 'should not have a blank option' do
- output_buffer.should_not have_tag("form li select option[@value='']")
- end
-
- it 'should have one option with a "selected" attribute' do
- output_buffer.should have_tag('form li select option[@selected]', :count => 1)
- end
- end
-
- describe 'when :prompt => "choose something" is set' do
- before do
- @new_post.stub!(:author_id).and_return(nil)
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :select, :prompt => "choose author"))
- end
- end
-
- it 'should have a select with prompt' do
- output_buffer.should have_tag("form li select option[@value='']", /choose author/)
- end
-
- it 'should not have a blank select option' do
- output_buffer.should_not have_tag("form li select option[@value='']", "")
- end
- end
-
- describe 'when no object is given' do
- before(:each) do
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all)))
- end
- end
-
- it 'should generate label' do
- output_buffer.should have_tag('form li label', /Author/)
- output_buffer.should have_tag("form li label[@for='project_author']")
- end
-
- it 'should generate select inputs' do
- output_buffer.should have_tag('form li select#project_author')
- output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
- end
-
- it 'should generate an option to each item' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
- end
- end
- end
-
- describe 'when :selected is set' do
- before do
- @new_post.stub!(:author_id).and_return(nil)
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:author, :as => :select, :selected => @bob.id ))
- end
- end
-
- it 'should have a selected item' do
- output_buffer.should have_tag("form li select option[@selected='selected']")
- end
-
- it 'bob should be selected' do
- output_buffer.should have_tag("form li select option[@selected='selected']", /bob/i)
- output_buffer.should have_tag("form li select option[@selected='selected'][@value='42']")
- end
-
- end
-
- end
-
- describe ':as => :check_boxes' do
- describe 'for a has_many association' do
- before do
- semantic_form_for(@fred) do |builder|
- concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
- end
- end
-
- it_should_have_input_wrapper_with_class("check_boxes")
- it_should_have_input_wrapper_with_id("author_posts_input")
- it_should_have_a_nested_fieldset
- it_should_apply_error_logic_for_input_type(:check_boxes)
- it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes)
-
- it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
-
-
-
- it 'should generate a legend containing label text for the input' do
- output_buffer.should have_tag('form li fieldset legend')
- output_buffer.should have_tag('form li fieldset legend', /Posts/)
- end
-
- it 'should generate an ordered list with a list item for each choice' do
- output_buffer.should have_tag('form li fieldset ol')
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size)
- end
-
- it 'should have one option with a "checked" attribute' do
- output_buffer.should have_tag('form li input[@checked]', :count => 1)
- end
-
- it 'should generate hidden inputs with default value blank' do
- output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
- end
-
- describe "each choice" do
-
- it 'should contain a label for the radio input with a nested input and label text' do
- ::Post.find(:all).each do |post|
- output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
- output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
- end
- end
-
- it 'should use values as li.class when value_as_class is true' do
- ::Post.find(:all).each do |post|
- output_buffer.should have_tag("form li fieldset ol li.#{post.id} label")
- end
- end
-
- it 'should have a checkbox input for each post' do
- ::Post.find(:all).each do |post|
- output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
- output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
- end
- end
-
- it "should mark input as checked if it's the the existing choice" do
- ::Post.find(:all).include?(@fred.posts.first).should be_true
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
- end
- end
-
- describe 'and no object is given' do
- before(:each) do
- output_buffer.replace ''
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
- end
- end
-
- it 'should generate a fieldset with legend' do
- output_buffer.should have_tag('form li fieldset legend', /Author/)
- end
-
- it 'shold generate an li tag for each item in the collection' do
- output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
- end
-
- it 'should generate labels for each item' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
- output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
- end
- end
-
- it 'should generate inputs for each item' do
- ::Author.find(:all).each do |author|
- output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
- output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
- output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
- output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
- end
- end
- end
- end
- end
-
- describe ':as => :date' do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :date))
- end
- end
-
- it_should_have_input_wrapper_with_class("date")
- it_should_have_input_wrapper_with_id("post_publish_at_input")
- it_should_have_a_nested_fieldset
- it_should_apply_error_logic_for_input_type(:date)
-
- it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.date fieldset legend', /Publish at/)
- end
-
- it 'should have an ordered list of three items inside the fieldset' do
- output_buffer.should have_tag('form li.date fieldset ol')
- output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
- end
-
- it 'should have three labels for year, month and day' do
- output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
- output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
- output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
- output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
- end
-
- it 'should have three selects for year, month and day' do
- output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
- end
- end
-
- describe ':as => :datetime' do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :datetime))
- end
- end
-
- it_should_have_input_wrapper_with_class("datetime")
- it_should_have_input_wrapper_with_id("post_publish_at_input")
- it_should_have_a_nested_fieldset
- it_should_apply_error_logic_for_input_type(:datetime)
-
- it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
- end
-
- it 'should have an ordered list of five items inside the fieldset' do
- output_buffer.should have_tag('form li.datetime fieldset ol')
- output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
- end
-
- it 'should have five labels for year, month, day, hour and minute' do
- output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
- end
-
- it 'should have five selects for year, month, day, hour and minute' do
- output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
- end
-
- it 'should generate a sanitized label and matching ids for attribute' do
- semantic_form_for(@new_post) do |builder|
- builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
- concat(bob_builder.input(:created_at, :as => :datetime))
- end
- end
-
- 1.upto(5) do |i|
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']")
- output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i")
- end
- end
-
- describe 'when :discard_input => true is set' do
- it 'should use default hidden value equals to 1 when attribute returns nil' do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
- end
-
- output_buffer.should have_tag("form li input[@type='hidden'][@value='1']")
- end
-
- it 'should use default attribute value when it is not nil' do
- @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
- end
-
- output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
- end
- end
-
- describe 'inputs order' do
- it 'should have a default' do
- semantic_form_for(@new_post) do |builder|
- self.should_receive(:select_year).once.ordered.and_return('')
- self.should_receive(:select_month).once.ordered.and_return('')
- self.should_receive(:select_day).once.ordered.and_return('')
- builder.input(:publish_at, :as => :datetime)
- end
- end
-
- it 'should be specified with :order option' do
- I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
- semantic_form_for(@new_post) do |builder|
- self.should_receive(:select_month).once.ordered.and_return('')
- self.should_receive(:select_year).once.ordered.and_return('')
- self.should_receive(:select_day).once.ordered.and_return('')
- builder.input(:publish_at, :as => :datetime)
- end
- end
-
- it 'should be changed through I18n' do
- semantic_form_for(@new_post) do |builder|
- self.should_receive(:select_day).once.ordered.and_return('')
- self.should_receive(:select_month).once.ordered.and_return('')
- self.should_receive(:select_year).once.ordered.and_return('')
- builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
- end
- end
- end
-
- describe 'when the locale changes the label text' do
- before do
- I18n.backend.store_translations 'en', :datetime => {:prompts => {
- :year => 'The Year', :month => 'The Month', :day => 'The Day',
- :hour => 'The Hour', :minute => 'The Minute'
- }}
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :datetime))
- end
- end
-
- after do
- I18n.backend.store_translations 'en', :formtastic => {
- :year => nil, :month => nil, :day => nil,
- :hour => nil, :minute => nil
- }
- end
-
- it 'should have translated labels for year, month, day, hour and minute' do
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
- output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
- end
- end
-
- describe 'when no object is given' do
- before(:each) do
- output_buffer.replace ''
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:publish_at, :as => :datetime))
- end
- end
-
- it 'should have fieldset with legend' do
- output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
- end
-
- it 'should have labels for each input' do
- output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
- end
-
- it 'should have selects for each inputs' do
- output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
- end
- end
- end
-
- describe ':as => :time' do
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:publish_at, :as => :time))
- end
- end
-
- it_should_have_input_wrapper_with_class("time")
- it_should_have_input_wrapper_with_id("post_publish_at_input")
- it_should_have_a_nested_fieldset
- it_should_apply_error_logic_for_input_type(:time)
-
- it 'should have a legend containing the label text inside the fieldset' do
- output_buffer.should have_tag('form li.time fieldset legend', /Publish at/)
- end
-
- it 'should have an ordered list of two items inside the fieldset' do
- output_buffer.should have_tag('form li.time fieldset ol')
- output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
- end
-
- it 'should have five labels for hour and minute' do
- output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2)
- output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i)
- output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i)
- end
-
- it 'should have two selects for hour and minute' do
- output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
- end
- end
-
- describe ':as => :boolean' do
-
- before do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:allow_comments, :as => :boolean))
- end
- end
-
- it_should_have_input_wrapper_with_class("boolean")
- it_should_have_input_wrapper_with_id("post_allow_comments_input")
- it_should_apply_error_logic_for_input_type(:boolean)
-
- it 'should generate a label containing the input' do
- output_buffer.should have_tag('form li label', :count => 1)
- output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
- output_buffer.should have_tag('form li label', /Allow comments/)
- output_buffer.should have_tag('form li label input[@type="checkbox"]')
- end
-
- it 'should generate a checkbox input' do
- output_buffer.should have_tag('form li label input')
- output_buffer.should have_tag('form li label input#post_allow_comments')
- output_buffer.should have_tag('form li label input[@type="checkbox"]')
- output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
- output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
- end
-
- it 'should allow checked and unchecked values to be sent' do
- semantic_form_for(@new_post) do |builder|
- concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
- end
-
- output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]')
- output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
- end
-
- it 'should generate a label and a checkbox even if no object is given' do
- semantic_form_for(:project, :url => 'http://test.host') do |builder|
- concat(builder.input(:allow_comments, :as => :boolean))
- end
-
- output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
- output_buffer.should have_tag('form li label', /Allow comments/)
- output_buffer.should have_tag('form li label input[@type="checkbox"]')
-
- output_buffer.should have_tag('form li label input#project_allow_comments')
- output_buffer.should have_tag('form li label input[@type="checkbox"]')
- output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
- end
-
- end
-
end
View
60 spec/inputs/boolean_input_spec.rb
@@ -0,0 +1,60 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'boolean input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:allow_comments, :as => :boolean))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("boolean")
+ it_should_have_input_wrapper_with_id("post_allow_comments_input")
+ it_should_apply_error_logic_for_input_type(:boolean)
+
+ it 'should generate a label containing the input' do
+ output_buffer.should have_tag('form li label', :count => 1)
+ output_buffer.should have_tag('form li label[@for="post_allow_comments"]')
+ output_buffer.should have_tag('form li label', /Allow comments/)
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
+ end
+
+ it 'should generate a checkbox input' do
+ output_buffer.should have_tag('form li label input')
+ output_buffer.should have_tag('form li label input#post_allow_comments')
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
+ output_buffer.should have_tag('form li label input[@name="post[allow_comments]"]')
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="1"]')
+ end
+
+ it 'should allow checked and unchecked values to be sent' do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:allow_comments, :as => :boolean, :checked_value => 'checked', :unchecked_value => 'unchecked'))
+ end
+
+ output_buffer.should have_tag('form li label input[@type="checkbox"][@value="checked"]')
+ output_buffer.should have_tag('form li label input[@type="hidden"][@value="unchecked"]')
+ end
+
+ it 'should generate a label and a checkbox even if no object is given' do
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:allow_comments, :as => :boolean))
+ end
+
+ output_buffer.should have_tag('form li label[@for="project_allow_comments"]')
+ output_buffer.should have_tag('form li label', /Allow comments/)
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
+
+ output_buffer.should have_tag('form li label input#project_allow_comments')
+ output_buffer.should have_tag('form li label input[@type="checkbox"]')
+ output_buffer.should have_tag('form li label input[@name="project[allow_comments]"]')
+ end
+
+end
+
View
106 spec/inputs/check_boxes_input_spec.rb
@@ -0,0 +1,106 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'check_boxes input' do
+
+ include FormtasticSpecHelper
+
+ describe 'for a has_many association' do
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@fred) do |builder|
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("check_boxes")
+ it_should_have_input_wrapper_with_id("author_posts_input")
+ it_should_have_a_nested_fieldset
+ it_should_apply_error_logic_for_input_type(:check_boxes)
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:check_boxes)
+ it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
+
+ it 'should generate a legend containing label text for the input' do
+ output_buffer.should have_tag('form li fieldset legend')
+ output_buffer.should have_tag('form li fieldset legend', /Posts/)
+ end
+
+ it 'should generate an ordered list with a list item for each choice' do
+ output_buffer.should have_tag('form li fieldset ol')
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Post.find(:all).size)
+ end
+
+ it 'should have one option with a "checked" attribute' do
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
+ end
+
+ it 'should generate hidden inputs with default value blank' do
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.find(:all).size)
+ end
+
+ describe "each choice" do
+
+ it 'should contain a label for the radio input with a nested input and label text' do
+ ::Post.find(:all).each do |post|
+ output_buffer.should have_tag('form li fieldset ol li label', /#{post.to_label}/)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.id}']")
+ end
+ end
+
+ it 'should use values as li.class when value_as_class is true' do
+ ::Post.find(:all).each do |post|
+ output_buffer.should have_tag("form li fieldset ol li.#{post.id} label")
+ end
+ end
+
+ it 'should have a checkbox input for each post' do
+ ::Post.find(:all).each do |post|
+ output_buffer.should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 2)
+ end
+ end
+
+ it "should mark input as checked if it's the the existing choice" do
+ ::Post.find(:all).include?(@fred.posts.first).should be_true
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
+ end
+ end
+
+ describe 'and no object is given' do
+ before(:each) do
+ output_buffer.replace ''
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:author_id, :as => :check_boxes, :collection => ::Author.find(:all)))
+ end
+ end
+
+ it 'should generate a fieldset with legend' do
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
+ end
+
+ it 'shold generate an li tag for each item in the collection' do
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
+ end
+
+ it 'should generate labels for each item' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
+ end
+ end
+
+ it 'should generate inputs for each item' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='checkbox']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
+ end
+ end
+ end
+ end
+
+end
+
View
80 spec/inputs/country_input_spec.rb
@@ -0,0 +1,80 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'country input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe "when country_select is not available as a helper from a plugin" do
+
+ it "should raise an error, sugesting the author installs a plugin" do
+ lambda {
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:country, :as => :country))
+ end
+ }.should raise_error
+ end
+
+ end
+
+ describe "when country_select is available as a helper (from a plugin)" do
+
+ before do
+ semantic_form_for(@new_post) do |builder|
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
+ concat(builder.input(:country, :as => :country))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("country")
+ it_should_have_input_wrapper_with_id("post_country_input")
+
+ # TODO -- needs stubbing inside the builder block, tricky!
+ #it_should_apply_error_logic_for_input_type(:country)
+
+ it 'should generate a label for the input' do
+ output_buffer.should have_tag('form li label')
+ output_buffer.should have_tag('form li label[@for="post_country"]')
+ output_buffer.should have_tag('form li label', /Country/)
+ end
+
+ it "should generate a select" do
+ output_buffer.should have_tag("form li select")
+ end
+
+ end
+
+ describe ":priority_countries option" do
+
+ it "should be passed down to the country_select helper when provided" do
+ priority_countries = ["Foo", "Bah"]
+ semantic_form_for(@new_post) do |builder|
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
+
+ concat(builder.input(:country, :as => :country, :priority_countries => priority_countries))
+ end
+ end
+
+ it "should default to the @@priority_countries config when absent" do
+ priority_countries = Formtastic::SemanticFormBuilder.priority_countries
+ priority_countries.should_not be_empty
+ priority_countries.should_not be_nil
+
+ semantic_form_for(@new_post) do |builder|
+ builder.stub!(:country_select).and_return("<select><option>...</option></select>")
+ builder.should_receive(:country_select).with(:country, priority_countries, {}, {}).and_return("<select><option>...</option></select>")
+
+ concat(builder.input(:country, :as => :country))
+ end
+ end
+
+ end
+
+end
+
View
43 spec/inputs/date_input_spec.rb
@@ -0,0 +1,43 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'date input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :date))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("date")
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
+ it_should_have_a_nested_fieldset
+ it_should_apply_error_logic_for_input_type(:date)
+
+ it 'should have a legend containing the label text inside the fieldset' do
+ output_buffer.should have_tag('form li.date fieldset legend', /Publish at/)
+ end
+
+ it 'should have an ordered list of three items inside the fieldset' do
+ output_buffer.should have_tag('form li.date fieldset ol')
+ output_buffer.should have_tag('form li.date fieldset ol li', :count => 3)
+ end
+
+ it 'should have three labels for year, month and day' do
+ output_buffer.should have_tag('form li.date fieldset ol li label', :count => 3)
+ output_buffer.should have_tag('form li.date fieldset ol li label', /year/i)
+ output_buffer.should have_tag('form li.date fieldset ol li label', /month/i)
+ output_buffer.should have_tag('form li.date fieldset ol li label', /day/i)
+ end
+
+ it 'should have three selects for year, month and day' do
+ output_buffer.should have_tag('form li.date fieldset ol li select', :count => 3)
+ end
+
+end
+
View
155 spec/inputs/datetime_input_spec.rb
@@ -0,0 +1,155 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'datetime input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("datetime")
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
+ it_should_have_a_nested_fieldset
+ it_should_apply_error_logic_for_input_type(:datetime)
+
+ it 'should have a legend containing the label text inside the fieldset' do
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
+ end
+
+ it 'should have an ordered list of five items inside the fieldset' do
+ output_buffer.should have_tag('form li.datetime fieldset ol')
+ output_buffer.should have_tag('form li.datetime fieldset ol li', :count => 5)
+ end
+
+ it 'should have five labels for year, month, day, hour and minute' do
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /year/i)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /month/i)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /day/i)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /hour/i)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /minute/i)
+ end
+
+ it 'should have five selects for year, month, day, hour and minute' do
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
+ end
+
+ it 'should generate a sanitized label and matching ids for attribute' do
+ semantic_form_for(@new_post) do |builder|
+ builder.semantic_fields_for(@bob, :index => 10) do |bob_builder|
+ concat(bob_builder.input(:created_at, :as => :datetime))
+ end
+ end
+
+ 1.upto(5) do |i|
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_10_created_at_#{i}i']")
+ output_buffer.should have_tag("form li fieldset ol li #post_author_10_created_at_#{i}i")
+ end
+ end
+
+ describe 'when :discard_input => true is set' do
+ it 'should use default hidden value equals to 1 when attribute returns nil' do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
+ end
+
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='1']")
+ end
+
+ it 'should use default attribute value when it is not nil' do
+ @new_post.stub!(:publish_at).and_return(Date.new(2007,12,27))
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime, :discard_day => true))
+ end
+
+ output_buffer.should have_tag("form li input[@type='hidden'][@value='27']")
+ end
+ end
+
+ describe 'inputs order' do
+ it 'should have a default' do
+ semantic_form_for(@new_post) do |builder|
+ self.should_receive(:select_year).once.ordered.and_return('')
+ self.should_receive(:select_month).once.ordered.and_return('')
+ self.should_receive(:select_day).once.ordered.and_return('')
+ builder.input(:publish_at, :as => :datetime)
+ end
+ end
+
+ it 'should be specified with :order option' do
+ I18n.backend.store_translations 'en', :date => { :order => [:month, :year, :day] }
+ semantic_form_for(@new_post) do |builder|
+ self.should_receive(:select_month).once.ordered.and_return('')
+ self.should_receive(:select_year).once.ordered.and_return('')
+ self.should_receive(:select_day).once.ordered.and_return('')
+ builder.input(:publish_at, :as => :datetime)
+ end
+ end
+
+ it 'should be changed through I18n' do
+ semantic_form_for(@new_post) do |builder|
+ self.should_receive(:select_day).once.ordered.and_return('')
+ self.should_receive(:select_month).once.ordered.and_return('')
+ self.should_receive(:select_year).once.ordered.and_return('')
+ builder.input(:publish_at, :as => :datetime, :order => [:day, :month, :year])
+ end
+ end
+ end
+
+ describe 'when the locale changes the label text' do
+ before do
+ I18n.backend.store_translations 'en', :datetime => {:prompts => {
+ :year => 'The Year', :month => 'The Month', :day => 'The Day',
+ :hour => 'The Hour', :minute => 'The Minute'
+ }}
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :datetime))
+ end
+ end
+
+ after do
+ I18n.backend.store_translations 'en', :formtastic => {
+ :year => nil, :month => nil, :day => nil,
+ :hour => nil, :minute => nil
+ }
+ end
+
+ it 'should have translated labels for year, month, day, hour and minute' do
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Year/)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Month/)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Day/)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Hour/)
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', /The Minute/)
+ end
+ end
+
+ describe 'when no object is given' do
+ before(:each) do
+ output_buffer.replace ''
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:publish_at, :as => :datetime))
+ end
+ end
+
+ it 'should have fieldset with legend' do
+ output_buffer.should have_tag('form li.datetime fieldset legend', /Publish at/)
+ end
+
+ it 'should have labels for each input' do
+ output_buffer.should have_tag('form li.datetime fieldset ol li label', :count => 5)
+ end
+
+ it 'should have selects for each inputs' do
+ output_buffer.should have_tag('form li.datetime fieldset ol li select', :count => 5)
+ end
+ end
+
+end
+
View
33 spec/inputs/file_input_spec.rb
@@ -0,0 +1,33 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'file input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:body, :as => :file))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("file")
+ it_should_have_input_wrapper_with_id("post_body_input")
+ it_should_have_label_with_text(/Body/)
+ it_should_have_label_for("post_body")
+ it_should_have_input_with_id("post_body")
+ it_should_have_input_with_name("post[body]")
+ it_should_apply_error_logic_for_input_type(:file)
+
+ it 'should use input_html to style inputs' do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :file, :input_html => { :class => 'myclass' }))
+ end
+ output_buffer.should have_tag("form li input.myclass")
+ end
+
+end
+
View
41 spec/inputs/hidden_input_spec.rb
@@ -0,0 +1,41 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'hidden input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:secret, :as => :hidden))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("hidden")
+ it_should_have_input_wrapper_with_id("post_secret_input")
+ it_should_not_have_a_label
+
+ it "should generate a input field" do
+ output_buffer.should have_tag("form li input#post_secret")
+ output_buffer.should have_tag("form li input[@type=\"hidden\"]")
+ output_buffer.should have_tag("form li input[@name=\"post[secret]\"]")
+ end
+
+ it "should not render inline errors" do
+ @errors = mock('errors')
+ @errors.stub!(:[]).with(:secret).and_return(["foo", "bah"])
+ @new_post.stub!(:errors).and_return(@errors)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:secret, :as => :hidden))
+ end
+
+ output_buffer.should_not have_tag("form li p.inline-errors")
+ output_buffer.should_not have_tag("form li ul.errors")
+ end
+
+end
+
View
44 spec/inputs/numeric_input_spec.rb
@@ -0,0 +1,44 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'numeric input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :numeric))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:numeric)
+ 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_use_default_text_field_size_when_method_has_no_database_column(: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(:numeric)
+
+ describe "when no object is provided" do
+ before do
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
+ concat(builder.input(:title, :as => :numeric))
+ 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
+
+end
+
View
46 spec/inputs/password_input_spec.rb
@@ -0,0 +1,46 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'password input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :password))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class(:password)
+ 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(:password)
+ it_should_have_input_with_name("post[title]")
+ it_should_have_maxlength_matching_column_limit
+ it_should_use_default_text_field_size_for_columns_longer_than_default_text_field_size(:string)
+ it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string)
+ it_should_use_default_text_field_size_when_method_has_no_database_column(: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(:password)
+
+ describe "when no object is provided" do
+ before do
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
+ concat(builder.input(:title, :as => :password))
+ 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(:password)
+ it_should_have_input_with_name("project[title]")
+ end
+
+end
View
113 spec/inputs/radio_input_spec.rb
@@ -0,0 +1,113 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'radio input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe 'for belongs_to association' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :radio, :value_as_class => true))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("radio")
+ it_should_have_input_wrapper_with_id("post_author_input")
+ it_should_have_a_nested_fieldset
+ it_should_apply_error_logic_for_input_type(:radio)
+ it_should_use_the_collection_when_provided(:radio, 'input')
+
+
+ it 'should generate a legend containing label text for the input' do
+ output_buffer.should have_tag('form li fieldset legend')
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
+ end
+
+ it 'should generate an ordered list with a list item for each choice' do
+ output_buffer.should have_tag('form li fieldset ol')
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
+ end
+
+ it 'should have one option with a "checked" attribute' do
+ output_buffer.should have_tag('form li input[@checked]', :count => 1)
+ end
+
+ describe "each choice" do
+
+ it 'should contain a label for the radio input with a nested input and label text' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_id_#{author.id}']")
+ end
+ end
+
+ it 'should use values as li.class when value_as_class is true' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li fieldset ol li.#{author.id} label")
+ end
+ end
+
+ it "should have a radio input" do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li fieldset ol li label input#post_author_id_#{author.id}")
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='post[author_id]']")
+ end
+ end
+
+ it "should mark input as checked if it's the the existing choice" do
+ @new_post.author_id.should == @bob.id
+ @new_post.author.id.should == @bob.id
+ @new_post.author.should == @bob
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :radio))
+ end
+
+ output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
+ end
+ end
+
+ describe 'and no object is given' do
+ before(:each) do
+ output_buffer.replace ''
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:author_id, :as => :radio, :collection => ::Author.find(:all)))
+ end
+ end
+
+ it 'should generate a fieldset with legend' do
+ output_buffer.should have_tag('form li fieldset legend', /Author/)
+ end
+
+ it 'should generate an li tag for each item in the collection' do
+ output_buffer.should have_tag('form li fieldset ol li', :count => ::Author.find(:all).size)
+ end
+
+ it 'should generate labels for each item' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag('form li fieldset ol li label', /#{author.to_label}/)
+ output_buffer.should have_tag("form li fieldset ol li label[@for='project_author_id_#{author.id}']")
+ end
+ end
+
+ it 'should generate inputs for each item' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
+ output_buffer.should have_tag("form li fieldset ol li label input[@type='radio']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
+ output_buffer.should have_tag("form li fieldset ol li label input[@name='project[author_id]']")
+ end
+ end
+ end
+ end
+
+end
+
View
263 spec/inputs/select_input_spec.rb
@@ -0,0 +1,263 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'select input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+ end
+
+ describe 'for a belongs_to association' do
+ before do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("select")
+ it_should_have_input_wrapper_with_id("post_author_input")
+ it_should_have_label_with_text(/Author/)
+ it_should_have_label_for('post_author_id')
+ it_should_apply_error_logic_for_input_type(:select)
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
+ it_should_use_the_collection_when_provided(:select, 'option')
+
+ it 'should have a select inside the wrapper' do
+ output_buffer.should have_tag('form li select')
+ output_buffer.should have_tag('form li select#post_author_id')
+ end
+
+ it 'should not create a multi-select' do
+ output_buffer.should_not have_tag('form li select[@multiple]')
+ end
+
+ it 'should create a select without size' do
+ output_buffer.should_not have_tag('form li select[@size]')
+ end
+
+ it 'should have a blank option' do
+ output_buffer.should have_tag("form li select option[@value='']")
+ end
+
+ it 'should have a select option for each Author' do
+ output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
+ end
+ end
+
+ it 'should have one option with a "selected" attribute' do
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
+ end
+
+ it 'should not singularize the association name' do
+ @new_post.stub!(:author_status).and_return(@bob)
+ @new_post.stub!(:author_status_id).and_return(@bob.id)
+ @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :integer, :limit => 255))
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author_status, :as => :select))
+ end
+
+ output_buffer.should have_tag('form li select#post_author_status_id')
+ end
+ end
+
+ describe 'for a belongs_to association with :group_by => :continent' do
+ before do
+ @authors = [@bob, @fred, @fred, @fred]
+ ::Author.stub!(:find).and_return(@authors)
+ @continent_names = %w(Europe Africa)
+ @continents = (0..1).map { |i| mock("continent", :id => (100 - i) ) }
+ @authors[0..1].each_with_index { |author, i| author.stub!(:continent).and_return(@continents[i]) }
+ @continents.each_with_index do |continent, i|
+ continent.stub!(:to_label).and_return(@continent_names[i])
+ continent.stub!(:authors).and_return([@authors[i]])
+ end
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
+ concat(builder.input(:author, :as => :select, :group_by => :continent, :group_label_method => :id ) )
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("select")
+ it_should_have_input_wrapper_with_id("post_author_input")
+ it_should_have_label_with_text(/Author/)
+ it_should_have_label_for('post_author_id')
+
+ # TODO, need to find a way to repeat some of the specs and logic from the belongs_to specs without grouping
+
+ 0.upto(1) do |i|
+ it 'should have all option groups and the right values' do
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continent_names[i]}']", @authors[i].to_label)
+ end
+
+ it 'should have custom group labels' do
+ output_buffer.should have_tag("form li select optgroup[@label='#{@continents[i].id}']", @authors[i].to_label)
+ end
+ end
+
+ it 'should have no duplicate groups' do
+ output_buffer.should have_tag('form li select optgroup', :count => 4)
+ end
+
+ it 'should sort the groups on the label method' do
+ output_buffer.should have_tag("form li select optgroup[@label='Africa']")
+ output_buffer.should have_tag("form li select optgroup[@label='99']")
+ end
+
+ it 'should call find with :include for more optimized queries' do
+ Author.should_receive(:find).with(:all, :include => :continent)
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select, :group_by => :continent ) )
+ end
+ end
+ end
+
+ describe 'for a has_many association' do
+ before do
+ semantic_form_for(@fred) do |builder|
+ concat(builder.input(:posts, :as => :select))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("select")
+ it_should_have_input_wrapper_with_id("author_posts_input")
+ it_should_have_label_with_text(/Post/)
+ it_should_have_label_for('author_post_ids')
+ it_should_apply_error_logic_for_input_type(:select)
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
+ it_should_use_the_collection_when_provided(:select, 'option')
+
+ it 'should have a select inside the wrapper' do
+ output_buffer.should have_tag('form li select')
+ output_buffer.should have_tag('form li select#author_post_ids')
+ end
+
+ it 'should have a multi-select select' do
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
+ end
+
+ it 'should have a select option for each Post' do
+ output_buffer.should have_tag('form li select option', :count => ::Post.find(:all).size)
+ ::Post.find(:all).each do |post|
+ output_buffer.should have_tag("form li select option[@value='#{post.id}']", /#{post.to_label}/)
+ end
+ end
+
+ it 'should not have a blank option' do
+ output_buffer.should_not have_tag("form li select option[@value='']")
+ end
+
+ it 'should have one option with a "selected" attribute' do
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
+ end
+ end
+
+ describe 'for a has_and_belongs_to_many association' do
+ before do
+ semantic_form_for(@freds_post) do |builder|
+ concat(builder.input(:authors, :as => :select))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("select")
+ it_should_have_input_wrapper_with_id("post_authors_input")
+ it_should_have_label_with_text(/Author/)
+ it_should_have_label_for('post_author_ids')
+ it_should_apply_error_logic_for_input_type(:select)
+ it_should_call_find_on_association_class_when_no_collection_is_provided(:select)
+ it_should_use_the_collection_when_provided(:select, 'option')
+
+ it 'should have a select inside the wrapper' do
+ output_buffer.should have_tag('form li select')
+ output_buffer.should have_tag('form li select#post_author_ids')
+ end
+
+ it 'should have a multi-select select' do
+ output_buffer.should have_tag('form li select[@multiple="multiple"]')
+ end
+
+ it 'should have a select option for each Author' do
+ output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size)
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
+ end
+ end
+
+ it 'should not have a blank option' do
+ output_buffer.should_not have_tag("form li select option[@value='']")
+ end
+
+ it 'should have one option with a "selected" attribute' do
+ output_buffer.should have_tag('form li select option[@selected]', :count => 1)
+ end
+ end
+
+ describe 'when :prompt => "choose something" is set' do
+ before do
+ @new_post.stub!(:author_id).and_return(nil)
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select, :prompt => "choose author"))
+ end
+ end
+
+ it 'should have a select with prompt' do
+ output_buffer.should have_tag("form li select option[@value='']", /choose author/)
+ end
+
+ it 'should not have a blank select option' do
+ output_buffer.should_not have_tag("form li select option[@value='']", "")
+ end
+ end
+
+ describe 'when no object is given' do
+ before(:each) do
+ semantic_form_for(:project, :url => 'http://test.host') do |builder|
+ concat(builder.input(:author, :as => :select, :collection => ::Author.find(:all)))
+ end
+ end
+
+ it 'should generate label' do
+ output_buffer.should have_tag('form li label', /Author/)
+ output_buffer.should have_tag("form li label[@for='project_author']")
+ end
+
+ it 'should generate select inputs' do
+ output_buffer.should have_tag('form li select#project_author')
+ output_buffer.should have_tag('form li select option', :count => ::Author.find(:all).size + 1)
+ end
+
+ it 'should generate an option to each item' do
+ ::Author.find(:all).each do |author|
+ output_buffer.should have_tag("form li select option[@value='#{author.id}']", /#{author.to_label}/)
+ end
+ end
+ end
+
+ describe 'when :selected is set' do
+ before do
+ @new_post.stub!(:author_id).and_return(nil)
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:author, :as => :select, :selected => @bob.id ))
+ end
+ end
+
+ it 'should have a selected item' do
+ output_buffer.should have_tag("form li select option[@selected='selected']")
+ end
+
+ it 'bob should be selected' do
+ output_buffer.should have_tag("form li select option[@selected='selected']", /bob/i)
+ output_buffer.should have_tag("form li select option[@selected='selected'][@value='42']")
+ end
+
+ end
+
+end
+
View
47 spec/inputs/string_input_spec.rb
@@ -0,0 +1,47 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'string input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ 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_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_for_columns_longer_than_default_text_field_size(:string)
+ it_should_use_column_size_for_columns_shorter_than_default_text_field_size(:string)
+ it_should_use_default_text_field_size_when_method_has_no_database_column(: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)
+
+ describe "when no object is provided" do
+ before do
+ 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
+
+end
+
View
33 spec/inputs/text_input_spec.rb
@@ -0,0 +1,33 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'text input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:body, :as => :text))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("text")
+ it_should_have_input_wrapper_with_id("post_body_input")
+ it_should_have_label_with_text(/Body/)
+ it_should_have_label_for("post_body")
+ it_should_have_textarea_with_id("post_body")
+ it_should_have_textarea_with_name("post[body]")
+ it_should_apply_error_logic_for_input_type(:numeric)
+
+ it 'should use input_html to style inputs' do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:title, :as => :text, :input_html => { :class => 'myclass' }))
+ end
+ output_buffer.should have_tag("form li textarea.myclass")
+ end
+
+end
+
View
42 spec/inputs/time_input_spec.rb
@@ -0,0 +1,42 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'time input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:publish_at, :as => :time))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("time")
+ it_should_have_input_wrapper_with_id("post_publish_at_input")
+ it_should_have_a_nested_fieldset
+ it_should_apply_error_logic_for_input_type(:time)
+
+ it 'should have a legend containing the label text inside the fieldset' do
+ output_buffer.should have_tag('form li.time fieldset legend', /Publish at/)
+ end
+
+ it 'should have an ordered list of two items inside the fieldset' do
+ output_buffer.should have_tag('form li.time fieldset ol')
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
+ end
+
+ it 'should have five labels for hour and minute' do
+ output_buffer.should have_tag('form li.time fieldset ol li label', :count => 2)
+ output_buffer.should have_tag('form li.time fieldset ol li label', /hour/i)
+ output_buffer.should have_tag('form li.time fieldset ol li label', /minute/i)
+ end
+
+ it 'should have two selects for hour and minute' do
+ output_buffer.should have_tag('form li.time fieldset ol li', :count => 2)
+ end
+
+end
+
View
59 spec/inputs/time_zone_input_spec.rb
@@ -0,0 +1,59 @@
+# coding: utf-8
+require File.dirname(__FILE__) + '/../test_helper'
+
+describe 'time_zone input' do
+
+ include FormtasticSpecHelper
+
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:time_zone))
+ end
+ end
+
+ it_should_have_input_wrapper_with_class("time_zone")
+ it_should_have_input_wrapper_with_id("post_time_zone_input")
+ it_should_apply_error_logic_for_input_type(:time_zone)
+
+ it 'should generate a label for the input' do
+ output_buffer.should have_tag('form li label')
+ output_buffer.should have_tag('form li label[@for="post_time_zone"]')
+ output_buffer.should have_tag('form li label', /Time zone/)
+ end
+
+ it "should generate a select" do
+ output_buffer.should have_tag("form li select")
+ output_buffer.should have_tag("form li select#post_time_zone")
+ output_buffer.should have_tag("form li select[@name=\"post[time_zone]\"]")
+ end
+
+ it 'should use input_html to style inputs' do
+ semantic_form_for(@new_post) do |builder|
+ concat(builder.input(:time_zone, :input_html => { :class => 'myclass' }))
+ end
+ output_buffer.should have_tag("form li select.myclass")
+ end
+
+ describe 'when no object is given' do
+ before(:each) do
+ semantic_form_for(:project, :url => 'http://test.host/') do |builder|
+ concat(builder.input(:time_zone, :as => :time_zone))
+ end
+ end
+
+ it 'should generate labels' do
+ output_buffer.should have_tag('form li label')
+ output_buffer.should have_tag('form li label[@for="project_time_zone"]')
+ output_buffer.should have_tag('form li label', /Time zone/)
+ end
+
+ it 'should generate select inputs' do
+ output_buffer.should have_tag("form li select")
+ output_buffer.should have_tag("form li select#project_time_zone")
+ output_buffer.should have_tag("form li select[@name=\"project[time_zone]\"]")
+ end
+ end
+end
View
4 spec/test_helper.rb
@@ -170,11 +170,9 @@ def new_author_path; "/authors/new"; end
@new_post.stub!(:column_for_attribute).with(:time_zone).and_return(mock('column', :type => :string))
@new_post.stub!(:column_for_attribute).with(:allow_comments).and_return(mock('column', :type => :boolean))
-
-
-
@new_post.stub!(:author).and_return(@bob)
@new_post.stub!(:author_id).and_return(@bob.id)
+
end
def self.included(base)

0 comments on commit 675698e

Please sign in to comment.