2011-02-09 21:03:53 +00:00
require 'spec_helper'
describe Cocoon do
class TestClass < ActionView :: Base
end
subject { TestClass . new }
it { should respond_to ( :link_to_add_association ) }
it { should respond_to ( :link_to_remove_association ) }
2012-11-22 00:31:19 +00:00
before ( :each ) do
@tester = TestClass . new
@post = Post . new
@form_obj = stub ( :object = > @post , :object_name = > @post . class . name )
end
2011-03-06 21:40:37 +00:00
context " link_to_add_association " do
before ( :each ) do
2011-03-15 14:41:17 +00:00
@tester . stub ( :render_association ) . and_return ( 'form<tag>' )
2011-03-06 21:40:37 +00:00
end
2011-03-07 21:02:21 +00:00
context " without a block " do
2012-03-12 22:53:52 +00:00
it " accepts a name " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">add something</a>'
2011-03-07 21:02:21 +00:00
end
2012-03-12 22:53:52 +00:00
it " accepts html options and pass them to link_to " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments , { :class = > 'something silly' } )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="something silly add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">add something</a>'
2011-03-07 21:02:21 +00:00
end
2012-04-09 10:45:59 +00:00
it " allows to explicitly hand the wanted partial " do
@tester . unstub ( :render_association )
@tester . should_receive ( :render_association ) . with ( anything ( ) , anything ( ) , anything ( ) , anything ( ) , " shared/partial " ) . and_return ( 'partiallll' )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments , :partial = > " shared/partial " )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="partiallll" data-association="comment" data-associations="comments">add something</a>'
2012-04-09 10:45:59 +00:00
end
2012-09-19 10:37:35 +00:00
it " gives an opportunity to wrap/decorate created objects " do
@tester . unstub ( :render_association )
@tester . should_receive ( :render_association ) . with ( anything ( ) , anything ( ) , kind_of ( CommentDecorator ) , anything ( ) , anything ( ) ) . and_return ( 'partiallll' )
@tester . link_to_add_association ( 'add something' , @form_obj , :comments , :wrap_object = > Proc . new { | comment | CommentDecorator . new ( comment ) } )
end
2012-11-22 00:31:19 +00:00
context " force non association create " do
it " default it uses the association " do
@tester . should_receive ( :create_object ) . with ( anything , :comments , false )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments )
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">add something</a>'
end
it " specifying false is the same as default: create object on association " do
@tester . should_receive ( :create_object ) . with ( anything , :comments , false )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments , :force_non_association_create = > false )
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">add something</a>'
end
it " specifying true will not create objects on association but using the conditions " do
@tester . should_receive ( :create_object ) . with ( anything , :comments , true )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :comments , :force_non_association_create = > true )
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">add something</a>'
end
end
2011-03-07 21:02:21 +00:00
end
context " with a block " do
it " the block gives the link text " do
result = @tester . link_to_add_association ( @form_obj , :comments ) do
" some long name "
end
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">some long name</a>'
2011-03-07 21:02:21 +00:00
end
2012-03-12 22:53:52 +00:00
it " accepts html options and pass them to link_to " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_add_association ( @form_obj , :comments , { :class = > 'floppy disk' } ) do
" some long name "
end
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="floppy disk add_fields" data-association-insertion-template="form<tag>" data-association="comment" data-associations="comments">some long name</a>'
2011-04-24 12:36:50 +00:00
end
2012-04-09 10:45:59 +00:00
it " allows to explicitly hand the wanted partial " do
2012-04-07 15:34:10 +00:00
@tester . unstub ( :render_association )
@tester . should_receive ( :render_association ) . with ( anything ( ) , anything ( ) , anything ( ) , anything ( ) , " shared/partial " ) . and_return ( 'partiallll' )
2012-04-09 10:45:59 +00:00
result = @tester . link_to_add_association ( @form_obj , :comments , :class = > 'floppy disk' , :partial = > " shared/partial " ) do
2012-04-07 15:34:10 +00:00
" some long name "
end
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="floppy disk add_fields" data-association-insertion-template="partiallll" data-association="comment" data-associations="comments">some long name</a>'
2012-04-07 15:34:10 +00:00
end
2011-04-24 12:36:50 +00:00
end
context " with an irregular plural " do
2012-03-12 22:53:52 +00:00
it " uses the correct plural " do
2011-04-24 12:36:50 +00:00
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="person" data-associations="people">add something</a>'
2011-03-07 21:02:21 +00:00
end
2011-10-21 23:42:07 +00:00
end
2012-04-06 22:35:57 +00:00
context " when using aliased association and class-name " do
it " uses the correct name " do
result = @tester . link_to_add_association ( 'add something' , @form_obj , :admin_comments )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="admin_comment" data-associations="admin_comments">add something</a>'
2012-04-06 22:35:57 +00:00
end
end
2011-10-21 23:42:07 +00:00
it " tttt " do
@post . class . reflect_on_association ( :people ) . klass . new . should be_a ( Person )
end
context " with extra render-options for rendering the child relation " do
2012-03-12 22:53:52 +00:00
it " uses the correct plural " do
2012-03-12 19:30:11 +00:00
@tester . should_receive ( :render_association ) . with ( :people , @form_obj , anything , { :wrapper = > 'inline' } , nil )
2011-10-21 23:42:07 +00:00
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people , :render_options = > { :wrapper = > 'inline' } )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tag>" data-association="person" data-associations="people">add something</a>'
2011-10-21 23:42:07 +00:00
end
end
2012-04-07 22:56:02 +00:00
context " passing locals to the partial " do
it " when given: passes the locals to the partials " do
2012-04-07 00:14:26 +00:00
@tester . unstub ( :render_association )
@form_obj . should_receive ( :fields_for ) { | association , new_object , options_hash , & block | block . call }
@tester . should_receive ( :render ) . with ( " person_fields " , { :f = > nil , :dynamic = > true , :alfred = > " Judoka " } ) . and_return ( " partiallll " )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people , :render_options = > { :wrapper = > 'inline' , :locals = > { :alfred = > 'Judoka' } } )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="partiallll" data-association="person" data-associations="people">add something</a>'
2012-04-07 00:14:26 +00:00
end
2012-04-07 22:56:02 +00:00
it " if no locals are given it still works " do
@tester . unstub ( :render_association )
@form_obj . should_receive ( :fields_for ) { | association , new_object , options_hash , & block | block . call }
@tester . should_receive ( :render ) . with ( " person_fields " , { :f = > nil , :dynamic = > true } ) . and_return ( " partiallll " )
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people , :render_options = > { :wrapper = > 'inline' } )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="partiallll" data-association="person" data-associations="people">add something</a>'
2012-04-07 22:56:02 +00:00
end
2012-04-07 00:14:26 +00:00
end
2011-10-21 23:42:07 +00:00
context " when using formtastic " do
before ( :each ) do
@tester . unstub ( :render_association )
@form_obj . stub ( :semantic_fields_for ) . and_return ( 'form<tagzzz>' )
end
it " calls semantic_fields_for and not fields_for " do
@form_obj . should_receive ( :semantic_fields_for )
@form_obj . should_receive ( :fields_for ) . never
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tagzzz>" data-association="person" data-associations="people">add something</a>'
2011-03-07 21:02:21 +00:00
2011-10-21 23:42:07 +00:00
end
end
context " when using simple_form " do
before ( :each ) do
@tester . unstub ( :render_association )
@form_obj . stub ( :simple_fields_for ) . and_return ( 'form<tagxxx>' )
end
it " responds_to :simple_fields_for " do
@form_obj . should respond_to ( :simple_fields_for )
end
it " calls simple_fields_for and not fields_for " do
@form_obj . should_receive ( :simple_fields_for )
@form_obj . should_receive ( :fields_for ) . never
result = @tester . link_to_add_association ( 'add something' , @form_obj , :people )
2012-08-29 08:42:15 +00:00
result . to_s . should == '<a href="#" class="add_fields" data-association-insertion-template="form<tagxxx>" data-association="person" data-associations="people">add something</a>'
2011-10-21 23:42:07 +00:00
end
2011-03-07 21:02:21 +00:00
end
end
context " link_to_remove_association " do
2012-03-12 22:53:52 +00:00
context " without a block " do
it " accepts a name " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_remove_association ( 'remove something' , @form_obj )
result . to_s . should == " <input id= \" Post__destroy \" name= \" Post[_destroy] \" type= \" hidden \" /><a href= \" # \" class= \" remove_fields dynamic \" >remove something</a> "
end
2012-03-12 22:53:52 +00:00
it " accepts html options and pass them to link_to " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_remove_association ( 'remove something' , @form_obj , { :class = > 'add_some_class' , :'data-something' = > 'bla' } )
result . to_s . should == " <input id= \" Post__destroy \" name= \" Post[_destroy] \" type= \" hidden \" /><a href= \" # \" class= \" add_some_class remove_fields dynamic \" data-something= \" bla \" >remove something</a> "
end
2011-03-06 21:40:37 +00:00
end
2011-03-07 21:02:21 +00:00
context " with a block " do
it " the block gives the name " do
result = @tester . link_to_remove_association ( @form_obj ) do
" remove some long name "
end
result . to_s . should == " <input id= \" Post__destroy \" name= \" Post[_destroy] \" type= \" hidden \" /><a href= \" # \" class= \" remove_fields dynamic \" >remove some long name</a> "
end
2012-03-12 22:53:52 +00:00
it " accepts html options and pass them to link_to " do
2011-03-07 21:02:21 +00:00
result = @tester . link_to_remove_association ( @form_obj , { :class = > 'add_some_class' , :'data-something' = > 'bla' } ) do
" remove some long name "
end
result . to_s . should == " <input id= \" Post__destroy \" name= \" Post[_destroy] \" type= \" hidden \" /><a href= \" # \" class= \" add_some_class remove_fields dynamic \" data-something= \" bla \" >remove some long name</a> "
2011-03-06 21:40:37 +00:00
end
end
2012-11-22 00:31:19 +00:00
end
2012-03-12 19:30:11 +00:00
2012-11-22 00:31:19 +00:00
context " create_object " do
it " creates correct association with conditions " do
@tester . should_not_receive ( :create_object_with_conditions )
result = @tester . create_object ( @form_obj , :admin_comments )
result . author . should == " Admin "
end
2012-05-04 23:25:59 +00:00
2012-11-22 00:31:19 +00:00
it " creates correct association for belongs_to associations " do
result = @tester . create_object ( stub ( :object = > Comment . new ) , :post )
result . should be_a Post
end
2012-09-24 07:47:47 +00:00
2012-11-22 00:31:19 +00:00
it " raises an error if cannot reflect on association " do
expect { @tester . create_object ( stub ( :object = > Comment . new ) , :not_existing ) } . to raise_error / association /i
end
2012-09-24 08:08:07 +00:00
2012-11-22 00:31:19 +00:00
it " creates an association if object responds to 'build_association' as singular " do
object = Comment . new
object . should_receive ( :build_custom_item ) . and_return 'custom'
@tester . create_object ( stub ( :object = > object ) , :custom_item ) . should == 'custom'
end
2012-09-24 08:20:18 +00:00
2012-11-22 00:31:19 +00:00
it " creates an association if object responds to 'build_association' as plural " do
object = Comment . new
object . should_receive ( :build_custom_item ) . and_return 'custom'
@tester . create_object ( stub ( :object = > object ) , :custom_items ) . should == 'custom'
2012-03-12 19:30:11 +00:00
end
2012-11-22 00:31:19 +00:00
it " can create using only conditions not the association " do
@tester . should_receive ( :create_object_with_conditions ) . and_return ( 'flappie' )
@tester . create_object ( @form_obj , :comments , true ) . should == 'flappie'
end
end
context " get_partial_path " do
it " generates the default partial name if no partial given " do
result = @tester . get_partial_path ( nil , :admin_comments )
result . should == " admin_comment_fields "
end
it " uses the given partial name " do
result = @tester . get_partial_path ( " comment_fields " , :admin_comments )
result . should == " comment_fields "
2012-03-12 19:30:11 +00:00
end
2011-03-06 21:40:37 +00:00
end
2011-03-15 14:41:17 +00:00
end