spec/grape_entity/entity_spec.rb in grape-entity-0.5.1 vs spec/grape_entity/entity_spec.rb in grape-entity-0.5.2

- old
+ new

@@ -903,10 +903,11 @@ subject.present_collection true, :my_items subject.expose :my_items representation = subject.represent(4.times.map { Object.new }, serializable: true) expect(representation).to be_kind_of(Grape::Entity::Exposure::NestingExposure::OutputBuilder) + expect(representation).to be_kind_of(Hash) expect(representation).to have_key :my_items expect(representation[:my_items]).to be_kind_of Array expect(representation[:my_items].size).to be 4 end end @@ -1521,33 +1522,53 @@ it 'returns a formatted value if format_with is passed a lambda' do expect(subject.value_for(:fantasies)).to eq ['Nessy', 'Double Rainbows', 'Unicorns'] end - it 'tries instance methods on the entity first' do + context 'delegate_attribute' do module EntitySpec class DelegatingEntity < Grape::Entity root 'friends', 'friend' expose :name expose :email + expose :system private def name 'cooler name' end end end - friend = double('Friend', name: 'joe', email: 'joe@example.com') - rep = EntitySpec::DelegatingEntity.new(friend) - expect(rep.value_for(:name)).to eq 'cooler name' - expect(rep.value_for(:email)).to eq 'joe@example.com' + it 'tries instance methods on the entity first' do + friend = double('Friend', name: 'joe', email: 'joe@example.com') + rep = EntitySpec::DelegatingEntity.new(friend) + expect(rep.value_for(:name)).to eq 'cooler name' + expect(rep.value_for(:email)).to eq 'joe@example.com' - another_friend = double('Friend', email: 'joe@example.com') - rep = EntitySpec::DelegatingEntity.new(another_friend) - expect(rep.value_for(:name)).to eq 'cooler name' + another_friend = double('Friend', email: 'joe@example.com') + rep = EntitySpec::DelegatingEntity.new(another_friend) + expect(rep.value_for(:name)).to eq 'cooler name' + end + + it 'does not delegate Kernel methods' do + foo = double 'Foo', system: 'System' + rep = EntitySpec::DelegatingEntity.new foo + expect(rep.value_for(:system)).to eq 'System' + end + + module EntitySpec + class DerivedEntity < DelegatingEntity + end + end + + it 'derived entity get methods from base entity' do + foo = double 'Foo', name: 'joe' + rep = EntitySpec::DerivedEntity.new foo + expect(rep.value_for(:name)).to eq 'cooler name' + end end context 'using' do before do module EntitySpec @@ -1712,9 +1733,43 @@ it 'instantiates with options if provided' do expect(instance.entity(awesome: true).options).to eq(awesome: true) end end + end + end + end + + describe Grape::Entity::Options do + module EntitySpec + class Crystalline + attr_accessor :prop1, :prop2 + + def initialize + @prop1 = 'value1' + @prop2 = 'value2' + end + end + + class CrystallineEntity < Grape::Entity + expose :prop1, if: ->(_, options) { options.fetch(:signal) } + expose :prop2, if: ->(_, options) { options.fetch(:beam, 'destructive') == 'destructive' } + end + end + + context '#fetch' do + it 'without passing in a required option raises KeyError' do + expect { EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new).as_json }.to raise_error KeyError + end + + it 'passing in a required option will expose the values' do + crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true) + expect(crystalline_entity.as_json).to eq(prop1: 'value1', prop2: 'value2') + end + + it 'with an option that is not default will not expose that value' do + crystalline_entity = EntitySpec::CrystallineEntity.represent(EntitySpec::Crystalline.new, signal: true, beam: 'intermittent') + expect(crystalline_entity.as_json).to eq(prop1: 'value1') end end end end end