spec/grape_entity/entity_spec.rb in grape-entity-0.4.1 vs spec/grape_entity/entity_spec.rb in grape-entity-0.4.2

- old
+ new

@@ -90,13 +90,13 @@ subject.expose :another_nested, using: 'Awesome' end subject.exposures.should == { awesome: {}, - awesome__nested: {}, - awesome__nested__moar_nested: { as: 'weee' }, - awesome__another_nested: { using: 'Awesome' } + awesome__nested: { nested: true }, + awesome__nested__moar_nested: { as: 'weee', nested: true }, + awesome__another_nested: { using: 'Awesome', nested: true } } end it 'represents the exposure as a hash of its nested exposures' do subject.expose :awesome do @@ -108,10 +108,72 @@ nested: "value", another_nested: "value" } end + it 'does not represent attributes, declared inside nested exposure, outside of it' do + subject.expose :awesome do + subject.expose(:nested) { |_| "value" } + subject.expose(:another_nested) { |_| "value" } + subject.expose :second_level_nested do + subject.expose(:deeply_exposed_attr) { |_| "value" } + end + end + + subject.represent({}).serializable_hash.should == { + awesome: { + nested: "value", + another_nested: "value", + second_level_nested: { + deeply_exposed_attr: "value" + } + } + } + end + + it "complex nested attributes" do + class ClassRoom < Grape::Entity + expose(:parents, using: 'Parent') { |_| [{}, {}] } + end + + class Person < Grape::Entity + expose :user do + expose(:in_first) { |_| 'value' } + end + end + + class Student < Person + expose :user do + expose(:user_id) { |_| 'value' } + expose(:user_display_id, as: :display_id) { |_| 'value' } + end + end + + class Parent < Person + expose(:children, using: 'Student') { |_| [{}, {}] } + end + + ClassRoom.represent({}).serializable_hash.should == { + parents: [ + { + user: { in_first: 'value' }, + children: [ + { user: { in_first: 'value', user_id: "value", display_id: "value" } }, + { user: { in_first: 'value', user_id: "value", display_id: "value" } } + ] + }, + { + user: { in_first: 'value' }, + children: [ + { user: { in_first: 'value', user_id: "value", display_id: "value" } }, + { user: { in_first: 'value', user_id: "value", display_id: "value" } } + ] + } + ] + } + end + it 'is safe if its nested exposures are safe' do subject.with_options safe: true do subject.expose :awesome do subject.expose(:nested) { |_| "value" } end @@ -119,9 +181,10 @@ subject.expose :nested end end valid_keys = subject.represent({}).valid_exposures.keys + valid_keys.include?(:awesome).should == true && \ valid_keys.include?(:not_awesome).should == false end end end