spec/grape_entity/entity_spec.rb in grape-entity-0.2.0 vs spec/grape_entity/entity_spec.rb in grape-entity-0.3.0
- old
+ new
@@ -103,10 +103,44 @@
subject.new(mock(model)).as_json[:birthday].should == '02/27/2012'
end
end
end
+ describe '.with_options' do
+ it 'should apply the options to all exposures inside' do
+ subject.class_eval do
+ with_options(:if => {:awesome => true}) do
+ expose :awesome_thing, :using => 'Awesome'
+ end
+ end
+
+ subject.exposures[:awesome_thing].should == {:if => {:awesome => true}, :using => 'Awesome'}
+ end
+
+ it 'should allow for nested .with_options' do
+ subject.class_eval do
+ with_options(:if => {:awesome => true}) do
+ with_options(:using => 'Something') do
+ expose :awesome_thing
+ end
+ end
+ end
+
+ subject.exposures[:awesome_thing].should == {:if => {:awesome => true}, :using => 'Something'}
+ end
+
+ it 'should allow for overrides' do
+ subject.class_eval do
+ with_options(:if => {:awesome => true}) do
+ expose :less_awesome_thing, :if => {:awesome => false}
+ end
+ end
+
+ subject.exposures[:less_awesome_thing].should == {:if => {:awesome => false}}
+ end
+ end
+
describe '.represent' do
it 'returns a single entity if called with one object' do
subject.represent(Object.new).should be_kind_of(subject)
end
@@ -500,10 +534,28 @@
subject.send(:conditions_met?, exposure_options, :condition1 => true, :condition2 => true).should be_true
subject.send(:conditions_met?, exposure_options, :condition1 => false, :condition2 => true).should be_false
subject.send(:conditions_met?, exposure_options, :condition1 => true, :condition2 => true, :other => true).should be_true
end
+ it 'looks for presence/truthiness if a symbol is passed' do
+ exposure_options = {:if => :condition1}
+
+ subject.send(:conditions_met?, exposure_options, {}).should be_false
+ subject.send(:conditions_met?, exposure_options, {:condition1 => true}).should be_true
+ subject.send(:conditions_met?, exposure_options, {:condition1 => false}).should be_false
+ subject.send(:conditions_met?, exposure_options, {:condition1 => nil}).should be_false
+ end
+
+ it 'looks for absence/falsiness if a symbol is passed' do
+ exposure_options = {:unless => :condition1}
+
+ subject.send(:conditions_met?, exposure_options, {}).should be_true
+ subject.send(:conditions_met?, exposure_options, {:condition1 => true}).should be_false
+ subject.send(:conditions_met?, exposure_options, {:condition1 => false}).should be_true
+ subject.send(:conditions_met?, exposure_options, {:condition1 => nil}).should be_true
+ end
+
it 'only passes through proc :if exposure if it returns truthy value' do
exposure_options = {:if => lambda{|_,opts| opts[:true]}}
subject.send(:conditions_met?, exposure_options, :true => false).should be_false
subject.send(:conditions_met?, exposure_options, :true => true).should be_true
@@ -568,9 +620,13 @@
instance.entity.should be_kind_of(subject.entity_class)
end
it 'has an object of itself' do
instance.entity.object.should == instance
+ end
+
+ it 'should instantiate with options if provided' do
+ instance.entity(:awesome => true).options.should == {:awesome => true}
end
end
end
end
end