Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 112 lines (94 sloc) 3.58 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
# encoding: utf-8
require 'spec_helper'

describe 'Formtastic::FormBuilder#semantic_errors' do

  include FormtasticSpecHelper

  before do
    @output_buffer = ''
    mock_everything
    @title_errors = ['must not be blank', 'must be awesome']
    @base_errors = ['base error message', 'nasty error']
    @base_error = 'one base error'
    @errors = mock('errors')
    @new_post.stub!(:errors).and_return(@errors)
  end

  describe 'when there is only one error on base' do
    before do
      @errors.stub!(:[]).with(:base).and_return(@base_error)
    end

    it 'should render an unordered list' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_errors.should have_tag('ul.errors li', @base_error)
      end
    end
  end

  describe 'when there is more than one error on base' do
    before do
      @errors.stub!(:[]).with(:base).and_return(@base_errors)
    end

    it 'should render an unordered list' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_errors.should have_tag('ul.errors')
        @base_errors.each do |error|
          builder.semantic_errors.should have_tag('ul.errors li', error)
        end
      end
    end
  end

  describe 'when there are errors on title' do
    before do
      @errors.stub!(:[]).with(:title).and_return(@title_errors)
      @errors.stub!(:[]).with(:base).and_return([])
    end

    it 'should render an unordered list' do
      semantic_form_for(@new_post) do |builder|
        title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title)
        builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence)
      end
    end
  end

  describe 'when there are errors on title and base' do
    before do
      @errors.stub!(:[]).with(:title).and_return(@title_errors)
      @errors.stub!(:[]).with(:base).and_return(@base_error)
    end

    it 'should render an unordered list' do
      semantic_form_for(@new_post) do |builder|
        title_name = builder.send(:localized_string, :title, :title, :label) || builder.send(:humanized_attribute_name, :title)
        builder.semantic_errors(:title).should have_tag('ul.errors li', title_name << " " << @title_errors.to_sentence)
        builder.semantic_errors(:title).should have_tag('ul.errors li', @base_error)
      end
    end
  end

  describe 'when there are no errors' do
    before do
      @errors.stub!(:[]).with(:title).and_return(nil)
      @errors.stub!(:[]).with(:base).and_return(nil)
    end

    it 'should return nil' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_errors(:title).should be_nil
      end
    end
  end

  describe 'when there is one error on base and options with class is passed' do
    before do
      @errors.stub!(:[]).with(:base).and_return(@base_error)
    end

    it 'should render an unordered list with given class' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_errors(:class => "awesome").should have_tag('ul.awesome li', @base_error)
      end
    end
  end
  
  describe 'when :base is passed in as an argument' do
    before do
      @errors.stub!(:[]).with(:base).and_return(@base_error)
    end

    it 'should ignore :base and only render base errors once' do
      semantic_form_for(@new_post) do |builder|
        builder.semantic_errors(:base).should have_tag('ul li', :count => 1)
        builder.semantic_errors(:base).should_not have_tag('ul li', "Base #{@base_error}")
      end
    end
  end
  
end
Something went wrong with that request. Please try again.