spec/formtastic_spec.rb in nofxx-formtastic-0.1.6 vs spec/formtastic_spec.rb in nofxx-formtastic-0.1.7

- old
+ new

@@ -26,10 +26,11 @@ include ActionView::Helpers::RecordIdentificationHelper include ActionView::Helpers::DateHelper include ActionView::Helpers::CaptureHelper include ActiveSupport include ActionController::PolymorphicRoutes + include ActionController include Formtastic::SemanticFormHelper attr_accessor :output_buffer @@ -559,11 +560,11 @@ end end it 'should call the corresponding input method' do - [:select, :radio, :date, :datetime, :time, :boolean].each do |input_style| + [:select, :time_zone, :radio, :date, :datetime, :time, :boolean].each do |input_style| @new_post.stub!(:generic_column_name) @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string, :limit => 255)) semantic_form_for(@new_post) do |builder| builder.should_receive(:"#{input_style}_input").once.and_return("fake HTML output from #input") concat(builder.input(:generic_column_name, :as => input_style)) @@ -645,10 +646,51 @@ output_buffer.should_not have_tag("form li p.inline-hints") end end end + + describe ':wrapper_html option' do + + describe 'when provided' do + it 'should be passed down to the li tag' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:id => :another_id})) + end + output_buffer.should have_tag("form li#another_id") + end + + it 'should append given classes to li default classes' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:class => :another_class}, :required => true)) + end + output_buffer.should have_tag("form li.string") + output_buffer.should have_tag("form li.required") + output_buffer.should have_tag("form li.another_class") + end + + it 'should allow classes to be an array' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title, :wrapper_html => {:class => [ :my_class, :another_class ]})) + end + output_buffer.should have_tag("form li.string") + output_buffer.should have_tag("form li.my_class") + output_buffer.should have_tag("form li.another_class") + end + end + + describe 'when not provided' do + it 'should use default id and class' do + semantic_form_for(@new_post) do |builder| + concat(builder.input(:title)) + end + output_buffer.should have_tag("form li#post_title_input") + output_buffer.should have_tag("form li.string") + end + end + + end end describe ':as any type of input' do it 'should create a list item for each input' do @@ -969,11 +1011,62 @@ end end end + describe ":as => :time_zone" do + before do + @new_post.stub!(:time_zone) + @new_post.stub!(:column_for_attribute).and_return(mock('column', :type => :string)) + semantic_form_for(@new_post) do |builder| + concat(builder.input(:time_zone)) + end + end + + it "should have a time_zone class on the wrapper" do + output_buffer.should have_tag('form li.time_zone') + end + + it 'should have a post_title_input id on the wrapper' do + output_buffer.should have_tag('form li#post_time_zone_input') + end + + 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 + + it 'should generate input and labels even if no object is given' do + semantic_form_for(:project, :url => 'http://test.host/') do |builder| + concat(builder.input(:time_zone, :as => :time_zone)) + end + + 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/) + + 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 + describe ':as => :radio' do before do @new_post.stub!(:author).and_return(@bob) @new_post.stub!(:author_id).and_return(@bob.id) @@ -1143,11 +1236,11 @@ output_buffer.should have_tag('form li#author_posts_input') end it 'should have a label inside the wrapper' do output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label', /Posts/) + output_buffer.should have_tag('form li label', /Post ids/) output_buffer.should have_tag("form li label[@for='author_post_ids']") end it 'should have a select inside the wrapper' do output_buffer.should have_tag('form li select') @@ -1185,11 +1278,11 @@ output_buffer.should have_tag('form li#post_authors_input') end it 'should have a label inside the wrapper' do output_buffer.should have_tag('form li label') - output_buffer.should have_tag('form li label', /Authors/) + output_buffer.should have_tag('form li label', /Author ids/) output_buffer.should have_tag("form li label[@for='post_author_ids']") end it 'should have a select inside the wrapper' do output_buffer.should have_tag('form li select') @@ -1656,26 +1749,24 @@ end describe 'when :discard_input => true is set' do it 'should use default hidden value equals to 1 when attribute returns nil' do - pending 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 fieldset ol input[@type='hidden'][@value='1']") + output_buffer.should have_tag("form li fieldset input[@type='hidden'][@value='1']") end it 'should use default attribute value when it is not nil' do - pending @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 fieldset ol input[@type='hidden'][@value='27']") + output_buffer.should have_tag("form li fieldset input[@type='hidden'][@value='27']") end end describe 'when :include_blank => true is set' do before do @@ -1785,19 +1876,17 @@ 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 - pending - 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) + output_buffer.should have_tag('form li.time fieldset li label', :count => 2) + output_buffer.should have_tag('form li.time fieldset li label', /hour/i) + output_buffer.should have_tag('form li.time fieldset li label', /minute/i) end it 'should have five selects for hour and minute' do - pending - output_buffer.should have_tag('form li.time fieldset ol li select', :count => 2) + output_buffer.should have_tag('form li.time fieldset li select', :count => 2) end end [:boolean_select, :boolean_radio].each do |type| describe ":as => #{type.inspect}" do @@ -1968,18 +2057,29 @@ output_buffer.should_not have_tag('fieldset[@builder="Formtastic::SemanticFormHelper"]') end it 'should send parent_builder as an option to allow child index interpolation' do semantic_form_for(@new_post) do |builder| - builder.should_receive(:instance_variable_get).with('@nested_child_index').and_return(0) + builder.instance_variable_set('@nested_child_index', 0) builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder| concat('input') end end output_buffer.should have_tag('fieldset legend', 'Author #1') end + + it 'should also provide child index interpolation when nested child index is a hash' do + semantic_form_for(@new_post) do |builder| + builder.instance_variable_set('@nested_child_index', :author => 10) + builder.inputs :for => [:author, @bob], :name => 'Author #%i' do |bob_builder| + concat('input') + end + end + + output_buffer.should have_tag('fieldset legend', 'Author #11') + end end describe 'when a :name option is provided' do before do @legend_text = "Advanced options" @@ -2312,10 +2412,20 @@ it 'should render an input with a name attribute of "commit"' do output_buffer.should have_tag('li.commit input[@name="commit"]') end + it 'should pass options given in :button_html to the button' do + @new_post.stub!(:new_record?).and_return(false) + semantic_form_for(@new_post) do |builder| + concat(builder.commit_button('text', :button_html => {:class => 'my_class', :id => 'my_id'})) + end + + output_buffer.should have_tag('li.commit input#my_id') + output_buffer.should have_tag('li.commit input.my_class') + end + end describe 'when used on an existing record' do it 'should render an input with a value attribute of "Save Post"' do @@ -2406,40 +2516,43 @@ end describe "Nested Js Helpers" do it "should have a add method" do - @new_post.stub!(:authors).and_return(@mock_authors = mock("Authors", :build => [])) + @new_post.stub!(:authors).and_return(mock("Authors", :build => mock('Author'))) semantic_form_for(@new_post) do |builder| - builder.inputs :name => "Contacts" do + builder.should_receive(:render).with(hash_including(:partial => "mock")).and_return('"attributes___idx___test__idxx__"') + builder.inputs :name => "Contacts", :id => "contacts" do concat(builder.add_associated_link(:name, :authors)) end end - output_buffer.should eql("<form action=\"/posts\" class=\"formtastic post\" id=\"new_post\" method=\"post\"><fieldset class=\"inputs\"><legend><span>Contacts</span></legend><ol><a href=\"#\" onclick=\"if (typeof formtastic_next_array_id == 'undefined') formtastic_next_array_id = 0;\n $('#arrays').append($.template(null), { number: --formtastic_next_array_id});; return false;\">name</a></ol></fieldset></form>") + output_buffer.should have_tag('form fieldset a[@href=#]') + output_buffer.should match(/\\&quot;attributes___idx___test__idxx__\\&quot;&quot;.replace/) end - it "should have a remove method" do + it "should have a remove method" do semantic_form_for(@new_post) do |builder| builder.semantic_fields_for(:links) do |link_builder| concat(link_builder.remove_link("Remove")) end end output_buffer.should eql("<form action=\"/posts\" class=\"formtastic post\" id=\"new_post\" method=\"post\"><input id=\"post_links__delete\" name=\"post[links][_delete]\" type=\"hidden\" /><a href=\"#\" onclick=\"$(this).parents('.nil_class').hide(); $(this).prev(':input').val('1');; return false;\">Remove</a></form>") end it "should have a show method" do - @new_post.stub!(:authors).and_return(@mock_authors = mock("Authors")) - pending + @new_post.stub!(:authors).and_return([mock("Author")]) + semantic_form_for(@new_post) do |builder| + builder.should_receive(:render).with(hash_including(:partial => "mock")).and_return("fields") builder.inputs :name => "Contacts" do concat(builder.render_associated_form @new_post.authors) end end - output_buffer.should eql("<form action=\"/posts\" class=\"formtastic post\" id=\"new_post\" method=\"post\"><fieldset class=\"inputs\"><legend><span>Contacts</span></legend><ol><a href=\"#\" onclick=\"if (typeof formtastic_next_array_id == 'undefined') formtastic_next_array_id = 0;\n $('#arrays').append($.template(null), { number: --formtastic_next_array_id});; return false;\">name</a></ol></fieldset></form>") + output_buffer.should eql("<form action=\"/posts\" class=\"formtastic post\" id=\"new_post\" method=\"post\"><fieldset class=\"inputs\"><legend><span>Contacts</span></legend><ol>fields</ol></fieldset></form>") end end end