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) }
|
|
|
|
|
2011-03-06 21:40:37 +00:00
|
|
|
context "link_to_add_association" do
|
|
|
|
before(:each) do
|
|
|
|
@tester = TestClass.new
|
|
|
|
@post = Post.new
|
|
|
|
@form_obj = stub(:object => @post)
|
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
|
|
|
|
it "should accept a name" do
|
|
|
|
result = @tester.link_to_add_association('add something', @form_obj, :comments)
|
2011-03-15 14:41:17 +00:00
|
|
|
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&lt;tag&gt;">add something</a>'
|
2011-03-07 21:02:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should accept html options and pass them to link_to" do
|
|
|
|
result = @tester.link_to_add_association('add something', @form_obj, :comments, {:class => 'something silly'})
|
2011-03-15 14:41:17 +00:00
|
|
|
result.to_s.should == '<a href="#" class="something silly add_fields" data-association="comment" data-template="form&lt;tag&gt;">add something</a>'
|
2011-03-07 21:02:21 +00:00
|
|
|
end
|
|
|
|
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
|
2011-03-15 14:41:17 +00:00
|
|
|
result.to_s.should == '<a href="#" class="add_fields" data-association="comment" data-template="form&lt;tag&gt;">some long name</a>'
|
2011-03-07 21:02:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should accept html options and pass them to link_to" do
|
|
|
|
result = @tester.link_to_add_association(@form_obj, :comments, {:class => 'floppy disk'}) do
|
|
|
|
"some long name"
|
|
|
|
end
|
2011-03-15 14:41:17 +00:00
|
|
|
result.to_s.should == '<a href="#" class="floppy disk add_fields" data-association="comment" data-template="form&lt;tag&gt;">some long name</a>'
|
2011-03-07 21:02:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "link_to_remove_association" do
|
|
|
|
before(:each) do
|
|
|
|
@tester = TestClass.new
|
|
|
|
@post = Post.new
|
|
|
|
@form_obj = stub(:object => @post, :object_name => @post.class.name)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "without a block" do
|
|
|
|
it "should accept a name" do
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should accept html options and pass them to link_to" do
|
|
|
|
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
|
|
|
|
|
|
|
|
it "should accept html options and pass them to link_to" do
|
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2011-03-15 14:41:17 +00:00
|
|
|
end
|