spec/formal_spec.rb in formal-0.0.3 vs spec/formal_spec.rb in formal-1.0.1
- old
+ new
@@ -1,50 +1,110 @@
require 'spec_helper'
+require 'formal'
-describe Formal do
+describe Formal::FormBuilder do
include FormalSpecHelper
- before do
- @post = Post.new
- @output = ''
- end
+ describe "#label" do
+ describe 'when in a valid state' do
+ context 'and text is provided' do
+ it 'returns element with provided text' do
+ form_for(TestValid.new, builder: described_class) do |f|
+ label = f.label(:body, "PROVIDED TEXT")
+ expected_result = "<label for=\"test_valid_body\">PROVIDED TEXT</label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
- it 'should yield the formal builder' do
- form_for(@post, builder: Formal::Builder) do |f|
- f.should be_kind_of(Formal::Builder)
- end
- end
+ context 'and local is unavailable' do
+ it 'returns a properly populated label element' do
+ form_for(TestValid.new, builder: described_class) do |f|
+ label = f.label(:body)
+ expected_result = "<label for=\"test_valid_body\">Body</label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
- it 'should wrap input inside dd tag' do
- with_builder do |f|
- f.text_field :body
- end
+ context 'and locale is available' do
+ before do
+ I18n.config.backend.load_translations(fixture_locale(:en))
+ end
- expected = %q{<dd><input id="post_body" name="post[body]" size="30" type="text" /></dd>}
- output.should include(expected)
- end
+ after do
+ I18n.config.backend.reload!
+ end
- it 'should wrap check box with label inside' do
- with_builder do |f|
- f.check_box_with_label :published
+ it 'returns a properly populated label element' do
+ form_for(TestValid.new, builder: described_class) do |f|
+ label = f.label(:body)
+ expected_result = "<label for=\"test_valid_body\">VALID</label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
end
- expected = %q{<dt><label for="post_published"><input name="post[published]" type="hidden" value="0" /><input id="post_published" name="post[published]" type="checkbox" value="1" /> published</label></dt>}
- output.should include(expected)
- end
- context "with error" do
- let(:invalid_post) { InvalidPost.new }
+ describe "when in an invalid state" do
+ context "and locale is unavailable" do
+ it 'returns a label with nested error element' do
+ form_for(TestInvalid.new, builder: described_class) do |f|
+ label = f.label(:body)
+ expected_result = "<label for=\"test_invalid_body\">Body <span class=\"error\">ERROR</span></label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
- it 'should add error class ONLY to the erroneous dd tag' do
- with_builder(invalid_post) { |f| f.text_field :body }
- expected_error = %q{<dd class="error"><input id="invalid_post_body" name="invalid_post[body]" size="30" type="text" /></dd>}
- output.should include(expected_error)
+ context 'and locale is available' do
+ before do
+ I18n.config.backend.load_translations(fixture_locale(:en))
+ end
+
+ after do
+ I18n.config.backend.reload!
+ end
+
+ it 'returns a label with nested error element' do
+ form_for(TestInvalid.new, builder: described_class) do |f|
+ label = f.label(:body)
+ expected_result = "<label for=\"test_invalid_body\">INVALID <span class=\"error\">ERROR</span></label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
end
- it "does not add the error class to non erroneous dd tags" do
- with_builder(invalid_post) { |f| f.text_field :title }
- expected_error = %q{<dd><input id="invalid_post_title" name="invalid_post[title]" size="30" type="text" /></dd>}
- output.should include(expected_error)
+ describe 'error element' do
+ context 'when default' do
+ it 'returns span markup' do
+ form_for(TestInvalid.new, builder: described_class) do |f|
+ label = f.label(:body)
+ expected_result = "<label for=\"test_invalid_body\">Body <span class=\"error\">ERROR</span></label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
+
+ context 'when passed in' do
+ it 'returns passed in markup' do
+ form_for(TestInvalid.new, builder: described_class) do |f|
+ label = f.label(:body, nil, error_element: :small)
+ expected_result = "<label for=\"test_invalid_body\">Body <small class=\"error\">ERROR</small></label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
end
+ describe 'hide errors' do
+ it 'removes error markup' do
+ form_for(TestInvalid.new, builder: described_class) do |f|
+ label = f.label(:body, nil, hide_errors: true)
+ expected_result = "<label for=\"test_invalid_body\">Body</label>"
+ expect(label).to eq(expected_result)
+ end
+ end
+ end
end
end