Refactor input_checkboxes_input_spec to use subject #705

Closed
wants to merge 3 commits into
from
View
10 lib/formtastic/inputs/check_boxes_input.rb
@@ -109,7 +109,7 @@ def choice_html(choice)
end
def hidden_field_for_all
- if hidden_fields?
+ unless hidden_collection?
""
else
options = {}
@@ -120,7 +120,13 @@ def hidden_field_for_all
end
def hidden_fields?
- options[:hidden_fields]
+ render_hidden = options[:hidden_fields]
+ return false if render_hidden == :never
+ render_hidden
+ end
+
+ def hidden_collection?
+ options[:hidden_fields] != :never
end
def check_box_with_hidden_input(choice)
View
141 spec/inputs/check_boxes_input_spec.rb
@@ -5,6 +5,8 @@
include FormtasticSpecHelper
+ subject { output_buffer }
+
describe 'for a has_many association' do
before do
@output_buffer = ''
@@ -26,75 +28,75 @@
it_should_use_the_collection_when_provided(:check_boxes, 'input[@type="checkbox"]')
it 'should generate a legend containing a label with text for the input' do
- output_buffer.should have_tag('form li fieldset legend.label label')
- output_buffer.should have_tag('form li fieldset legend.label label', /Posts/)
+ should have_tag('form li fieldset legend.label label')
+ should have_tag('form li fieldset legend.label label', /Posts/)
end
it 'should not link the label within the legend to any input' do
- output_buffer.should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
+ should_not have_tag('form li fieldset legend label[@for^="author_post_ids_"]')
end
it 'should generate an ordered list with an li.choice for each choice' do
- output_buffer.should have_tag('form li fieldset ol')
- output_buffer.should have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
+ should have_tag('form li fieldset ol')
+ should have_tag('form li fieldset ol li.choice input[@type=checkbox]', :count => ::Post.all.size)
end
it 'should have one option with a "checked" attribute' do
- output_buffer.should have_tag('form li input[@checked]', :count => 1)
+ should have_tag('form li input[@checked]', :count => 1)
end
it 'should not generate hidden inputs with default value blank' do
- output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
+ should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']")
end
it 'should not render hidden inputs inside the ol' do
- output_buffer.should_not have_tag("form li fieldset ol li input[@type='hidden']")
+ should_not have_tag("form li fieldset ol li input[@type='hidden']")
end
it 'should render one hidden input for each choice outside the ol' do
- output_buffer.should have_tag("form li fieldset > input[@type='hidden']", :count => 1)
+ should have_tag("form li fieldset > input[@type='hidden']", :count => 1)
end
describe "each choice" do
it 'should not give the choice label the .label class' do
- output_buffer.should_not have_tag('li.choice label.label')
+ should_not have_tag('li.choice label.label')
end
it 'should not be marked as required' do
- output_buffer.should_not have_tag('li.choice input[@required]')
+ should_not have_tag('li.choice input[@required]')
end
it 'should contain a label for the radio input with a nested input and label text' do
::Post.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}']")
+ should have_tag('form li fieldset ol li label', /#{post.to_label}/)
+ 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.all.each do |post|
- output_buffer.should have_tag("form li fieldset ol li.post_#{post.id} label")
+ should have_tag("form li fieldset ol li.post_#{post.id} label")
end
end
it 'should have a checkbox input but no hidden field for each post' do
::Post.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 => 1)
+ should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
+ should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => 1)
end
end
it 'should have a hidden field with an empty array value for the collection to allow clearing of all checkboxes' do
- output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
+ should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value='']", :count => 1)
end
it 'the hidden field with an empty array value should be followed by the ol' do
- output_buffer.should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
+ should have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids][]'][@value=''] + ol", :count => 1)
end
it 'should not have a hidden field with an empty string value for the collection' do
- output_buffer.should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
+ should_not have_tag("form li fieldset > input[@type=hidden][@name='author[post_ids]'][@value='']", :count => 1)
end
it 'should have a checkbox and a hidden field for each post with :hidden_field => true' do
@@ -105,15 +107,15 @@
end)
::Post.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)
+ should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
+ 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.all.include?(@fred.posts.first).should be_true
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
+ should have_tag("form li fieldset ol li label input[@checked='checked']")
end
end
@@ -126,26 +128,26 @@
end
it 'should generate a fieldset with legend' do
- output_buffer.should have_tag('form li fieldset legend', /Author/)
+ 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 input[@type=checkbox]', :count => ::Author.all.size)
+ should have_tag('form li fieldset ol li input[@type=checkbox]', :count => ::Author.all.size)
end
it 'should generate labels for each item' do
::Author.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}']")
+ should have_tag('form li fieldset ol li label', /#{author.to_label}/)
+ 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.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][]']")
+ should have_tag("form li fieldset ol li label input#project_author_id_#{author.id}")
+ should have_tag("form li fieldset ol li label input[@type='checkbox']")
+ should have_tag("form li fieldset ol li label input[@value='#{author.id}']")
+ should have_tag("form li fieldset ol li label input[@name='project[author_id][]']")
end
end
@@ -154,7 +156,7 @@
concat(builder.input(:author_id, :as => :check_boxes, :collection => [["<b>Item 1</b>", 1], ["<b>Item 2</b>", 2]]))
end)
- output_buffer.should have_tag('form li fieldset ol li label') do |label|
+ should have_tag('form li fieldset ol li label') do |label|
label.body.should match /&lt;b&gt;Item [12]&lt;\/b&gt;$/
end
end
@@ -172,18 +174,37 @@
it 'should have a checkbox input for each post' do
::Post.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 => ::Post.all.length)
+ should have_tag("form li fieldset ol li label input#author_post_ids_#{post.id}")
+ should have_tag("form li fieldset ol li label input[@name='author[post_ids][]']", :count => ::Post.all.length)
end
end
it "should mark input as checked if it's the the existing choice" do
::Post.all.include?(@fred.posts.first).should be_true
- output_buffer.should have_tag("form li fieldset ol li label input[@checked='checked']")
+ should have_tag("form li fieldset ol li label input[@checked='checked']")
end
it 'should not generate empty hidden inputs' do
- output_buffer.should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
+ should_not have_tag("form li fieldset ol li label input[@type='hidden'][@value='']", :count => ::Post.all.length)
+ end
+
+ it 'should generate collection hidden input' do
+ should have_tag("form input[@type='hidden'][@name='author[post_ids][]']", :count => 1)
+ end
+ end
+
+ describe 'when :hidden_fields is set to :never' do
+ before do
+ @output_buffer = ''
+ mock_everything
+
+ concat(semantic_form_for(@fred) do |builder|
+ concat(builder.input(:posts, :as => :check_boxes, :value_as_class => true, :hidden_fields => :never))
+ end)
+ end
+
+ it 'should not generate any hidden inputs' do
+ should_not have_tag("form li input[@type='hidden']")
end
end
@@ -202,7 +223,7 @@
end
it 'should not have any disabled item(s)' do
- output_buffer.should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
+ should_not have_tag("form li fieldset ol li label input[@disabled='disabled']")
end
end
@@ -216,9 +237,9 @@
end
it "should have one item disabled; the specified one" do
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
+ should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 1)
+ should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
+ should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
end
end
@@ -232,11 +253,11 @@
end
it "should have multiple items disabled; the specified ones" do
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
- output_buffer.should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
- output_buffer.should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
+ should have_tag("form li fieldset ol li label input[@disabled='disabled']", :count => 2)
+ should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@bob.id}']", /bob/i)
+ should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@bob.id}']")
+ should have_tag("form li fieldset ol li label[@for='post_author_ids_#{@fred.id}']", /fred/i)
+ should have_tag("form li fieldset ol li label input[@disabled='disabled'][@value='#{@fred.id}']")
end
end
@@ -259,7 +280,7 @@
end
it "should do foo" do
- output_buffer.should have_tag("legend.label label", /Translated/)
+ should have_tag("legend.label label", /Translated/)
end
end
@@ -273,7 +294,7 @@
end
it "should output the correct label title" do
- output_buffer.should have_tag("legend.label label", /The authors/)
+ should have_tag("legend.label label", /The authors/)
end
end
@@ -287,11 +308,11 @@
end
it "should not output the legend" do
- output_buffer.should_not have_tag("legend.label")
+ should_not have_tag("legend.label")
end
it "should not cause escaped HTML" do
- output_buffer.should_not include("&gt;")
+ should_not include("&gt;")
end
end
@@ -305,7 +326,7 @@
end
it "should output the correct label title" do
- output_buffer.should have_tag("legend.label label abbr")
+ should have_tag("legend.label label abbr")
end
end
@@ -324,14 +345,14 @@
it 'should render checkboxes' do
# I'm aware these two lines test the same thing
- output_buffer.should have_tag('input[type="checkbox"]', :count => 2)
- output_buffer.should have_tag('input[type="checkbox"]', :count => ::Author.all.size)
+ should have_tag('input[type="checkbox"]', :count => 2)
+ should have_tag('input[type="checkbox"]', :count => ::Author.all.size)
end
it 'should only select checkboxes that are present in the association' do
# I'm aware these two lines test the same thing
- output_buffer.should have_tag('input[checked="checked"]', :count => 1)
- output_buffer.should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
+ should have_tag('input[checked="checked"]', :count => 1)
+ should have_tag('input[checked="checked"]', :count => @freds_post.authors.size)
end
end
@@ -352,7 +373,7 @@
concat(semantic_form_for(@new_post) do |builder|
concat(builder.input(:author, :as => :check_boxes, :member_value => :custom_value, :collection => [item, item, item]))
end)
- output_buffer.should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
+ should have_tag('input[@type=checkbox][@value="custom_value"]', :count => 3)
end
end
end
@@ -369,8 +390,8 @@
end
it 'should check the correct checkboxes' do
- output_buffer.should have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
- output_buffer.should have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
+ should have_tag("form li fieldset ol li label input[@value='fiction'][@checked='checked']")
+ should have_tag("form li fieldset ol li label input[@value='biography'][@checked='checked']")
end
end
@@ -386,7 +407,7 @@
end
it "should have a label for #context2_author_post_ids_19" do
- output_buffer.should have_tag("form li label[@for='context2_author_post_ids_19']")
+ should have_tag("form li label[@for='context2_author_post_ids_19']")
end
it_should_have_input_with_id('context2_author_post_ids_19')
@@ -406,13 +427,13 @@
it "should use array items for labels and values" do
@_collection.each do |post|
- output_buffer.should have_tag('form li fieldset ol li label', /#{post.first}/)
- output_buffer.should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
+ should have_tag('form li fieldset ol li label', /#{post.first}/)
+ should have_tag("form li fieldset ol li label[@for='author_post_ids_#{post.last}']")
end
end
it "should not check any items" do
- output_buffer.should have_tag('form li input[@checked]', :count => 0)
+ should have_tag('form li input[@checked]', :count => 0)
end
end