spec/grape_entity/entity_spec.rb in grape-entity-0.5.2 vs spec/grape_entity/entity_spec.rb in grape-entity-0.6.0
- old
+ new
@@ -48,10 +48,22 @@
it 'allows to solve collisions providing a lambda to a :merge option' do
subject.expose(:something, merge: true)
subject.expose(:special, merge: ->(_, v1, v2) { v1 && v2 ? 'brand new val' : v2 })
expect(subject.represent(nested_hash).serializable_hash).to eq(like_nested_hash: 'brand new val')
end
+
+ context 'and nested object is nil' do
+ let(:nested_hash) do
+ { something: nil, special: { like_nested_hash: '12' } }
+ end
+
+ it 'adds nothing to output' do
+ subject.expose(:something, merge: true)
+ subject.expose(:special)
+ expect(subject.represent(nested_hash).serializable_hash).to eq(special: { like_nested_hash: '12' })
+ end
+ end
end
context 'with a block' do
it 'errors out if called with multiple attributes' do
expect { subject.expose(:name, :email) { true } }.to raise_error ArgumentError
@@ -275,12 +287,11 @@
subject.expose(:nested, merge: true) { |_| { just_a_key: 'value' } }
subject.expose(:another_nested, merge: true) { |_| { just_another_key: 'value' } }
end
additional_hash = { users: [{ id: 1, name: 'John' }, { id: 2, name: 'Jay' }],
- admins: [{ id: 3, name: 'Jack' }, { id: 4, name: 'James' }]
- }
+ admins: [{ id: 3, name: 'Jack' }, { id: 4, name: 'James' }] }
expect(subject.represent(additional_hash).serializable_hash).to eq(
profiles: additional_hash[:users] + additional_hash[:admins],
awesome: { just_a_key: 'value', just_another_key: 'value' }
)
end
@@ -340,35 +351,35 @@
date.strftime('%m/%d/%Y')
end
subject.expose :birthday, format_with: :timestamp
- model = { birthday: Time.gm(2012, 2, 27) }
+ model = { birthday: Time.gm(2012, 2, 27) }
expect(subject.new(double(model)).as_json[:birthday]).to eq '02/27/2012'
end
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
object = {}
- subject.expose(:size, format_with: ->(_value) { self.object.class.to_s })
+ subject.expose(:size, format_with: ->(_value) { object.class.to_s })
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
end
it 'formats an exposure with a :format_with symbol that returns a value from the entity instance' do
subject.format_with :size_formatter do |_date|
- self.object.class.to_s
+ object.class.to_s
end
object = {}
subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
end
it 'works global on Grape::Entity' do
Grape::Entity.format_with :size_formatter do |_date|
- self.object.class.to_s
+ object.class.to_s
end
object = {}
subject.expose(:size, format_with: :size_formatter)
expect(subject.represent(object).value_for(:size)).to eq object.class.to_s
@@ -595,18 +606,18 @@
it 'returns a single entity if called with a hash' do
expect(subject.represent({})).to be_kind_of(subject)
end
it 'returns multiple entities if called with a collection' do
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of Array
expect(representation.size).to eq(4)
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
end
it 'adds the collection: true option if called with a collection' do
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
representation.each { |r| expect(r.options[:collection]).to be true }
end
it 'returns a serialized hash of a single object if serializable: true' do
subject.expose(:awesome) { |_| true }
@@ -614,11 +625,11 @@
expect(representation).to eq(awesome: true)
end
it 'returns a serialized array of hashes of multiple objects if serializable: true' do
subject.expose(:awesome) { |_| true }
- representation = subject.represent(2.times.map { Object.new }, serializable: true)
+ representation = subject.represent(Array.new(2) { Object.new }, serializable: true)
expect(representation).to eq([{ awesome: true }, { awesome: true }])
end
it 'returns a serialized hash of a hash' do
subject.expose(:awesome)
@@ -889,11 +900,11 @@
describe '.present_collection' do
it 'make the objects accessible' do
subject.present_collection true
subject.expose :items
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of(subject)
expect(representation.object).to be_kind_of(Hash)
expect(representation.object).to have_key :items
expect(representation.object[:items]).to be_kind_of Array
expect(representation.object[:items].size).to be 4
@@ -901,11 +912,11 @@
it 'serializes items with my root name' do
subject.present_collection true, :my_items
subject.expose :my_items
- representation = subject.represent(4.times.map { Object.new }, serializable: true)
+ representation = subject.represent(Array.new(4) { 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
@@ -927,28 +938,28 @@
end
end
context 'with an array of objects' do
it 'allows a root element name to be specified' do
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of Hash
expect(representation).to have_key 'things'
expect(representation['things']).to be_kind_of Array
expect(representation['things'].size).to eq 4
expect(representation['things'].reject { |r| r.is_a?(subject) }).to be_empty
end
end
context 'it can be overridden' do
it 'can be disabled' do
- representation = subject.represent(4.times.map { Object.new }, root: false)
+ representation = subject.represent(Array.new(4) { Object.new }, root: false)
expect(representation).to be_kind_of Array
expect(representation.size).to eq 4
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
end
it 'can use a different name' do
- representation = subject.represent(4.times.map { Object.new }, root: 'others')
+ representation = subject.represent(Array.new(4) { Object.new }, root: 'others')
expect(representation).to be_kind_of Hash
expect(representation).to have_key 'others'
expect(representation['others']).to be_kind_of Array
expect(representation['others'].size).to eq 4
expect(representation['others'].reject { |r| r.is_a?(subject) }).to be_empty
@@ -970,11 +981,11 @@
end
end
context 'with an array of objects' do
it 'allows a root element name to be specified' do
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of Array
expect(representation.size).to eq 4
expect(representation.reject { |r| r.is_a?(subject) }).to be_empty
end
end
@@ -991,11 +1002,11 @@
end
end
context 'with an array of objects' do
it 'allows a root element name to be specified' do
- representation = subject.represent(4.times.map { Object.new })
+ representation = subject.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of Hash
expect(representation).to have_key('things')
expect(representation['things']).to be_kind_of Array
expect(representation['things'].size).to eq 4
expect(representation['things'].reject { |r| r.is_a?(subject) }).to be_empty
@@ -1016,11 +1027,11 @@
expect(representation['thing']).to be_kind_of(child_class)
end
it 'inherits array root root' do
child_class = Class.new(subject)
- representation = child_class.represent(4.times.map { Object.new })
+ representation = child_class.represent(Array.new(4) { Object.new })
expect(representation).to be_kind_of Hash
expect(representation).to have_key('things')
expect(representation['things']).to be_kind_of Array
expect(representation['things'].size).to eq 4
expect(representation['things'].reject { |r| r.is_a?(child_class) }).to be_empty
@@ -1331,9 +1342,25 @@
fresh_class.expose :a, :b, :c
presenter = fresh_class.new(a: 1, b: 2, c: 3)
expect(presenter.serializable_hash(only: [:a, :b])).to eq(a: 1, b: 2)
expect(presenter.serializable_hash(only: [:b, :c])).to eq(b: 2, c: 3)
end
+ end
+ end
+
+ describe '#inspect' do
+ before do
+ fresh_class.class_eval do
+ expose :name, :email
+ end
+ end
+
+ it 'does not serialize delegator or options' do
+ data = subject.inspect
+ expect(data).to include 'name='
+ expect(data).to include 'email='
+ expect(data).to_not include '@options'
+ expect(data).to_not include '@delegator'
end
end
describe '#value_for' do
before do