add the most basic sanity tests for formtastic input

This commit is contained in:
John Bintz 2012-10-25 15:17:47 -04:00
parent 042fcc9976
commit b8f373fd5e
3 changed files with 94 additions and 14 deletions

View File

@ -12,6 +12,7 @@ group :development, :test do
gem "simplecov", :require => false
gem "generator_spec"
gem "formtastic"
end
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)

View File

@ -4,26 +4,32 @@ class CocoonInput
include ::Formtastic::Inputs::Base
def to_html
output = []
output = label_html << semantic_fields_for << links
output << label_html
template.content_tag(:li, output.html_safe, wrapper_html_options)
end
output << builder.semantic_fields_for(method) do |fields|
if fields.object
template.render :partial => "#{method.to_s.singularize}_fields", :locals => { :f => fields }
end
end
output << template.content_tag(:div, :class => 'links') do
template.link_to_add_association template.t('.add'), builder, method, input_html_options
end
data = wrapper_html_options.merge(:class => 'input cocoon')
def wrapper_html_options
data = super.merge(:class => 'input cocoon')
if options[:ordered_by]
data['data-ordered_by'] = options[:ordered_by]
end
template.content_tag(:li, output.join('').html_safe, data)
data
end
def semantic_fields_for
builder.semantic_fields_for(method) do |fields|
if fields.object
template.render :partial => "#{method.to_s.singularize}_fields", :locals => { :f => fields }
end
end
end
def links
template.content_tag(:div, :class => 'links') do
template.link_to_add_association template.t('.add'), builder, method, input_html_options
end
end
end

View File

@ -0,0 +1,73 @@
require 'spec_helper'
require 'cocoon/formtastic/cocoon_input'
describe CocoonInput do
let(:input) { CocoonInput.new(builder, template, object, object_name, method, options) }
let(:builder) { stub(:auto_index => false, :options => {}, :custom_namespace => nil, :all_fields_required_by_default => false) }
let(:template) { stub }
let(:object) { stub }
let(:object_name) { :object }
let(:method) { :nested }
let(:options) { {} }
describe '#wrapper_html_options' do
subject { input.wrapper_html_options }
context 'not ordered' do
it 'should not be ordered' do
subject.should_not have_key('data-ordered_by')
end
end
context 'ordered' do
let(:field) { :field }
let(:options) { { :ordered_by => field } }
it 'should be ordered' do
subject['data-ordered_by'].should == field
end
end
end
describe '#links' do
subject { input.links }
before do
template.stub(:content_tag).and_yield
template.stub(:link_to_add_association)
template.stub(:t)
end
it 'should generate the links holder' do
subject
end
end
describe '#semantic_fields_for' do
subject { input.semantic_fields_for }
before do
builder.stub(:semantic_fields_for)
template.stub(:render)
end
it 'should pass through to semantic_fields_for on the builder' do
subject
end
end
describe '#to_html' do
subject { input.to_html }
before do
input.stub(:label_html).and_return('label')
input.stub(:semantic_fields_for).and_return('fields')
input.stub(:links).and_return('links')
template.stub(:content_tag)
end
it 'should concatenate the outputs and pass through to content_tag' do
subject
end
end
end