Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 148 lines (113 sloc) 4.348 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
# coding: utf-8
require File.dirname(__FILE__) + '/spec_helper'

describe 'SemanticFormBuilder#buttons' do

  include FormtasticSpecHelper
  
  before do
    @output_buffer = ''
    mock_everything
  end

  describe 'with a block' do
    describe 'when no options are provided' do
      before do
        semantic_form_for(@new_post) do |builder|
          builder.buttons do
            concat('hello')
          end
        end
      end

      it 'should render a fieldset inside the form, with a class of "inputs"' do
        output_buffer.should have_tag("form fieldset.buttons")
      end

      it 'should render an ol inside the fieldset' do
        output_buffer.should have_tag("form fieldset.buttons ol")
      end

      it 'should render the contents of the block inside the ol' do
        output_buffer.should have_tag("form fieldset.buttons ol", /hello/)
      end

      it 'should not render a legend inside the fieldset' do
        output_buffer.should_not have_tag("form fieldset.buttons legend")
      end
    end

    describe 'when a :name option is provided' do
      before do
        @legend_text = "Advanced options"

        semantic_form_for(@new_post) do |builder|
          builder.buttons :name => @legend_text do
          end
        end
      end
      it 'should render a fieldset inside the form' do
        output_buffer.should have_tag("form fieldset legend", /#{@legend_text}/)
      end
    end

    describe 'when other options are provided' do
      before do
        @id_option = 'advanced'
        @class_option = 'wide'

        semantic_form_for(@new_post) do |builder|
          builder.buttons :id => @id_option, :class => @class_option do
          end
        end
      end
      it 'should pass the options into the fieldset tag as attributes' do
        output_buffer.should have_tag("form fieldset##{@id_option}")
        output_buffer.should have_tag("form fieldset.#{@class_option}")
      end
    end

  end

  describe 'without a block' do

    describe 'with no args (default buttons)' do

      before do
        semantic_form_for(@new_post) do |builder|
          concat(builder.buttons)
        end
      end

      it 'should render a form' do
        output_buffer.should have_tag('form')
      end

      it 'should render a buttons fieldset inside the form' do
        output_buffer.should have_tag('form fieldset.buttons')
      end

      it 'should not render a legend in the fieldset' do
        output_buffer.should_not have_tag('form fieldset.buttons legend')
      end

      it 'should render an ol in the fieldset' do
        output_buffer.should have_tag('form fieldset.buttons ol')
      end

      it 'should render a list item in the ol for each default button' do
        output_buffer.should have_tag('form fieldset.buttons ol li', :count => 1)
      end

      it 'should render a commit list item for the commit button' do
        output_buffer.should have_tag('form fieldset.buttons ol li.commit')
      end

    end

    describe 'with button names as args' do

      before do
        semantic_form_for(@new_post) do |builder|
          concat(builder.buttons(:commit))
        end
      end

      it 'should render a form with a fieldset containing a list item for each button arg' do
        output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
        output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit')
      end

    end

    describe 'with button names as args and an options hash' do

      before do
        semantic_form_for(@new_post) do |builder|
          concat(builder.buttons(:commit, :name => "Now click a button", :id => "my-id"))
        end
      end

      it 'should render a form with a fieldset containing a list item for each button arg' do
        output_buffer.should have_tag('form > fieldset.buttons > ol > li', :count => 1)
        output_buffer.should have_tag('form > fieldset.buttons > ol > li.commit', :count => 1)
      end

      it 'should pass the options down to the fieldset' do
        output_buffer.should have_tag('form > fieldset#my-id.buttons')
      end

      it 'should use the special :name option as a text for the legend tag' do
        output_buffer.should have_tag('form > fieldset#my-id.buttons > legend', /Now click a button/)
      end

    end

  end

end
Something went wrong with that request. Please try again.