draper/spec/base_spec.rb

193 lines
5.4 KiB
Ruby
Raw Normal View History

require 'spec_helper'
require 'draper'
describe Draper::Base do
subject{ Draper::Base.new(source) }
2011-07-30 18:25:45 +00:00
let(:source){ Product.new }
context(".lazy_helpers") do
it "makes Rails helpers available without using the h. proxy" do
Draper::Base.lazy_helpers
subject.send(:pluralize, 5, "cat").should == "5 cats"
end
end
2011-07-30 18:25:45 +00:00
context(".model_name") do
it "should return an ActiveModel::Name instance" do
Draper::Base.model_name.should be_instance_of(ActiveModel::Name)
end
end
context(".decorates") do
it "sets the model class for the decorator" do
ProductDecorator.new(source).model_class == Product
end
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:49:18 +00:00
context(".model / .to_model") do
it "should return the wrapped object" do
subject.to_model.should == source
subject.model.should == source
end
2011-07-23 14:48:14 +00:00
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:49:18 +00:00
context("selecting methods") do
it "echos the methods of the wrapped class except default exclusions" do
2011-07-23 15:49:18 +00:00
source.methods.each do |method|
unless Draper::Base::DEFAULT_DENIED.include?(method)
subject.should respond_to(method)
end
2011-07-23 15:49:18 +00:00
end
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:49:18 +00:00
it "should not override a defined method with a source method" do
DecoratorWithApplicationHelper.new(source).length.should == "overridden"
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:49:18 +00:00
it "should always proxy to_param" do
source.send :class_eval, "def to_param; 1; end"
Draper::Base.new(source).to_param.should == 1
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:49:18 +00:00
it "should not copy the .class, .inspect, or other existing methods" do
source.class.should_not == subject.class
source.inspect.should_not == subject.inspect
source.to_s.should_not == subject.to_s
end
2011-07-30 18:25:45 +00:00
end
2011-07-23 15:49:18 +00:00
it "should wrap source methods so they still accept blocks" do
subject.block{"marker"}.should == "marker"
end
2011-07-30 18:25:45 +00:00
context ".find" do
it "should lookup the associated model when passed an integer" do
pd = ProductDecorator.find(1)
pd.should be_instance_of(ProductDecorator)
pd.model.should be_instance_of(Product)
end
2011-07-30 18:25:45 +00:00
it "should lookup the associated model when passed a string" do
pd = ProductDecorator.find("1")
pd.should be_instance_of(ProductDecorator)
pd.model.should be_instance_of(Product)
end
end
2011-07-30 18:25:45 +00:00
2011-07-23 15:12:26 +00:00
context ".decorate" do
2011-08-08 23:32:22 +00:00
subject { Draper::Base.decorate(source) }
context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] }
its(:size) { should == source.size }
it "returns a collection of wrapped objects" do
subject.each{ |decorated| decorated.should be_instance_of(Draper::Base) }
end
end
2011-07-30 18:25:45 +00:00
2011-08-08 23:32:22 +00:00
context "when given a single source object" do
let(:source) { Product.new }
it { should be_instance_of(Draper::Base) }
end
end
2011-07-30 18:25:45 +00:00
describe "a sample usage with denies" do
2011-06-30 22:45:37 +00:00
let(:subject_with_denies){ DecoratorWithDenies.new(source) }
2011-07-30 18:25:45 +00:00
it "should proxy methods not listed in denies" do
subject_with_denies.should respond_to(:hello_world)
end
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
it "should not echo methods specified with denies" do
subject_with_denies.should_not respond_to(:goodnight_moon)
end
2011-06-30 22:45:37 +00:00
it "should not clobber other decorators' methods" do
subject.should respond_to(:hello_world)
2011-07-30 18:25:45 +00:00
end
it "should not allow method_missing to circumvent a deny" do
expect{subject_with_denies.title}.to raise_error(NoMethodError)
2011-07-30 18:25:45 +00:00
end
end
2011-07-30 18:25:45 +00:00
describe "a sample usage with allows" do
let(:subject_with_allows){ DecoratorWithAllows.new(source) }
2011-07-30 18:25:45 +00:00
it "should echo the allowed method" do
subject_with_allows.should respond_to(:upcase)
end
2011-07-30 18:25:45 +00:00
it "should echo _only_ the allowed method" do
subject_with_allows.should_not respond_to(:downcase)
end
end
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
describe "invalid usages of allows and denies" do
let(:blank_allows){
2011-07-30 18:25:45 +00:00
class DecoratorWithInvalidAllows < Draper::Base
allows
end
}
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
let(:blank_denies){
2011-07-30 18:25:45 +00:00
class DecoratorWithInvalidDenies < Draper::Base
2011-06-30 22:45:37 +00:00
denies
end
}
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
let(:using_allows_then_denies){
2011-07-30 18:25:45 +00:00
class DecoratorWithAllowsAndDenies < Draper::Base
allows :hello_world
denies :goodnight_moon
end
2011-07-30 18:25:45 +00:00
}
2011-06-30 22:45:37 +00:00
let(:using_denies_then_allows){
2011-07-30 18:25:45 +00:00
class DecoratorWithDeniesAndAllows < Draper::Base
denies :goodnight_moon
2011-07-30 18:25:45 +00:00
allows :hello_world
end
}
it "should raise an exception for a blank allows" do
expect {blank_allows}.should raise_error(ArgumentError)
end
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
it "should raise an exception for a blank denies" do
expect {blank_denies}.should raise_error(ArgumentError)
end
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
it "should raise an exception for calling allows then denies" do
expect {using_allows_then_denies}.should raise_error(ArgumentError)
end
2011-07-30 18:25:45 +00:00
2011-06-30 22:45:37 +00:00
it "should raise an exception for calling denies then allows" do
expect {using_denies_then_allows}.should raise_error(ArgumentError)
end
end
2011-07-30 18:25:45 +00:00
context "in a Rails application" do
let(:decorator){ DecoratorWithApplicationHelper.decorate(Object.new) }
2011-07-30 18:25:45 +00:00
it "should have access to ApplicationHelper helpers" do
decorator.uses_hello_world == "Hello, World!"
end
2011-07-30 18:25:45 +00:00
it "should be able to use the content_tag helper" do
decorator.sample_content.to_s.should == "<span>Hello, World!</span>"
end
2011-07-30 18:25:45 +00:00
it "should be able to use the link_to helper" do
decorator.sample_link.should == "<a href=\"/World\">Hello</a>"
end
2011-07-30 18:25:45 +00:00
it "should be able to use the pluralize helper" do
decorator.sample_truncate.should == "Once..."
end
end
2011-07-30 18:25:45 +00:00
end