spec/grape_entity/entity_spec.rb in grape-entity-0.7.1 vs spec/grape_entity/entity_spec.rb in grape-entity-0.8.0

- old
+ new

@@ -124,10 +124,30 @@ it 'raises an error when applied to multiple attribute exposures' do expect { subject.expose(:a, :b, :c, expose_nil: false) }.to raise_error ArgumentError end end + + context 'when expose_nil option is false and block passed' do + it 'does not expose if block returns nil' do + subject.expose(:a, expose_nil: false) do |_obj, _options| + nil + end + subject.expose(:b) + subject.expose(:c) + expect(subject.represent(model).serializable_hash).to eq(b: nil, c: 'value') + end + + it 'exposes is block returns a value' do + subject.expose(:a, expose_nil: false) do |_obj, _options| + 100 + end + subject.expose(:b) + subject.expose(:c) + expect(subject.represent(model).serializable_hash).to eq(a: 100, b: nil, c: 'value') + end + end end context 'when model is a hash' do let(:model) { { a: a, b: b, c: c } } @@ -1372,9 +1392,21 @@ fresh_class.expose :nonexistent_attribute, using: EntitySpec::TestEntity do |_model, _opts| 'well, I do exist after all' end res = fresh_class.new(model).serializable_hash expect(res).to have_key :nonexistent_attribute + end + + it 'exposes attributes defined through module inclusion' do + module SharedAttributes + def a_value + 3.14 + end + end + fresh_class.include(SharedAttributes) + fresh_class.expose :a_value + res = fresh_class.new(model).serializable_hash + expect(res[:a_value]).to eq(3.14) end it 'does not expose attributes that are generated by a block but have not passed criteria' do fresh_class.expose :nonexistent_attribute, proc: ->(_, _) { 'I exist, but it is not yet my time to shine' },