Skip to content

Commit

Permalink
added specs for currency select
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzan authored and justinfrench committed Jan 18, 2010
1 parent 4c9dd06 commit 5c196c4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 29 deletions.
62 changes: 33 additions & 29 deletions spec/input_spec.rb
Expand Up @@ -2,9 +2,9 @@
require File.dirname(__FILE__) + '/spec_helper'

describe 'SemanticFormBuilder#input' do

include FormtasticSpecHelper

before do
@output_buffer = ''
mock_everything
Expand Down Expand Up @@ -150,54 +150,54 @@
it 'should be not be required if the optional :if condition is not satisifed' do
should_be_required(:required => false, :options => { :if => false })
end

it 'should not be required if the optional :if proc evaluates to false' do
should_be_required(:required => false, :options => { :if => proc { |record| false } })
end

it 'should be required if the optional :if proc evaluates to true' do
should_be_required(:required => true, :options => { :if => proc { |record| true } })
end

it 'should not be required if the optional :unless proc evaluates to true' do
should_be_required(:required => false, :options => { :unless => proc { |record| true } })
end

it 'should be required if the optional :unless proc evaluates to false' do
should_be_required(:required => true, :options => { :unless => proc { |record| false } })
end

it 'should be required if the optional :if with a method string evaluates to true' do
@new_post.should_receive(:required_condition).and_return(true)
should_be_required(:required => true, :options => { :if => :required_condition })
end

it 'should be required if the optional :if with a method string evaluates to false' do
@new_post.should_receive(:required_condition).and_return(false)
should_be_required(:required => false, :options => { :if => :required_condition })
end

it 'should not be required if the optional :unless with a method string evaluates to false' do
@new_post.should_receive(:required_condition).and_return(false)
should_be_required(:required => true, :options => { :unless => :required_condition })
end

it 'should be required if the optional :unless with a method string evaluates to true' do
@new_post.should_receive(:required_condition).and_return(true)
should_be_required(:required => false, :options => { :unless => :required_condition })
end
end

# TODO make a matcher for this?
def should_be_required(options)
@new_post.class.should_receive(:reflect_on_validations_for).with(:body).and_return([
mock('MacroReflection', :macro => :validates_presence_of, :name => :body, :options => options[:options])
])

semantic_form_for(@new_post) do |builder|
concat(builder.input(:body))
end

if options[:required]
output_buffer.should_not have_tag('form li.optional')
output_buffer.should have_tag('form li.required')
Expand Down Expand Up @@ -325,11 +325,15 @@ def should_be_required(options)
default_input_type(:float).should == :numeric
default_input_type(:decimal).should == :numeric
end

it 'should default to :country for :string columns named country' do
default_input_type(:string, :country).should == :country
end

it 'should default to :currency for :string columns named currency' do
default_input_type(:string, :currency).should == :currency
end

describe 'defaulting to file column' do
::Formtastic::SemanticFormBuilder.file_methods.each do |method|
it "should default to :file for attributes that respond to ##{method}" do
Expand Down Expand Up @@ -374,7 +378,7 @@ def should_be_required(options)
end

describe ':label option' do

describe 'when provided' do
it 'should be passed down to the label tag' do
semantic_form_for(@new_post) do |builder|
Expand All @@ -399,26 +403,26 @@ def should_be_required(options)
end

describe 'when not provided' do
describe 'when localized label is provided' do
describe 'and object is given' do
describe 'when localized label is provided' do
describe 'and object is given' do
describe 'and label_str_method not default' do
it 'should render a label with localized label (I18n)' do
with_config :label_str_method, :capitalize do
@localized_label_text = 'Localized title'
@new_post.stub!(:meta_description)
@new_post.class.should_receive(:human_attribute_name).with('meta_description').and_return(@localized_label_text)

semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end

output_buffer.should have_tag('form li label', @localized_label_text)
end
end
end
end
end

describe 'when localized label is NOT provided' do
describe 'and object is not given' do
it 'should default the humanized method name, passing it down to the label tag' do
Expand All @@ -444,22 +448,22 @@ def should_be_required(options)
output_buffer.should have_tag("form li label", /#{'meta_description'.humanize}/)
end
end

describe 'and object is given with label_str_method set to :capitalize' do
it 'should capitalize method name, passing it down to the label tag' do
with_config :label_str_method, :capitalize do
@new_post.stub!(:meta_description)

semantic_form_for(@new_post) do |builder|
concat(builder.input(:meta_description))
end

output_buffer.should have_tag("form li label", /#{'meta_description'.capitalize}/)
end
end
end
end

describe 'when localized label is provided' do
before do
@localized_label_text = 'Localized title'
Expand Down Expand Up @@ -504,7 +508,7 @@ def should_be_required(options)
end
end
end

end

describe ':hint option' do
Expand Down Expand Up @@ -535,15 +539,15 @@ def should_be_required(options)
}
::Formtastic::SemanticFormBuilder.i18n_lookups_by_default = false
end

describe 'when provided value (hint value) is set to TRUE' do
it 'should render a hint paragraph containing a localized hint (I18n)' do
semantic_form_for(@new_post) do |builder|
concat(builder.input(:title, :hint => true))
end
output_buffer.should have_tag('form li p.inline-hints', @localized_hint_text)
end

it 'should render a hint paragraph containing an optional localized hint (I18n) if first is not set' do
::I18n.backend.store_translations :en,
:formtastic => {
Expand All @@ -559,7 +563,7 @@ def should_be_required(options)
output_buffer.should have_tag('form li p.inline-hints', @default_localized_hint_text)
end
end

describe 'when provided value (label value) is set to FALSE' do
it 'should not render a hint paragraph' do
semantic_form_for(@new_post) do |builder|
Expand All @@ -569,7 +573,7 @@ def should_be_required(options)
end
end
end

describe 'when localized hint (I18n) is not provided' do
it 'should not render a hint paragraph' do
semantic_form_for(@new_post) do |builder|
Expand Down
80 changes: 80 additions & 0 deletions spec/inputs/currency_input_spec.rb
@@ -0,0 +1,80 @@
# coding: utf-8
require File.dirname(__FILE__) + '/../spec_helper'

describe 'currency input' do

include FormtasticSpecHelper

before do
@output_buffer = ''
mock_everything
end

describe "when currency_select is not available as a helper from a plugin" do

it "should raise an error, sugesting the author installs a plugin" do
lambda {
semantic_form_for(@new_post) do |builder|
concat(builder.input(:currency, :as => :currency))
end
}.should raise_error
end

end

describe "when currency_select is available as a helper (from a plugin)" do

before do
semantic_form_for(@new_post) do |builder|
builder.stub!(:currency_select).and_return("<select><option>...</option></select>")
concat(builder.input(:currency, :as => :currency))
end
end

it_should_have_input_wrapper_with_class("currency")
it_should_have_input_wrapper_with_id("post_currency_input")

# TODO -- needs stubbing inside the builder block, tricky!
#it_should_apply_error_logic_for_input_type(:currency)

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_currency"]')
output_buffer.should have_tag('form li label', /Currency/)
end

it "should generate a select" do
output_buffer.should have_tag("form li select")
end

end

describe ":priority_currencies option" do

it "should be passed down to the currency_select helper when provided" do
priority_currencies = ["Foo", "Bah"]
semantic_form_for(@new_post) do |builder|
builder.stub!(:currency_select).and_return("<select><option>...</option></select>")
builder.should_receive(:currency_select).with(:currency, priority_currencies, {}, {}).and_return("<select><option>...</option></select>")

concat(builder.input(:currency, :as => :currency, :priority_currencies => priority_currencies))
end
end

it "should default to the @@priority_currencies config when absent" do
priority_currencies = ::Formtastic::SemanticFormBuilder.priority_currencies
priority_currencies.should_not be_empty
priority_currencies.should_not be_nil

semantic_form_for(@new_post) do |builder|
builder.stub!(:currency_select).and_return("<select><option>...</option></select>")
builder.should_receive(:currency_select).with(:currency, priority_currencies, {}, {}).and_return("<select><option>...</option></select>")

concat(builder.input(:currency, :as => :currency))
end
end

end

end

0 comments on commit 5c196c4

Please sign in to comment.