spec/unit/yaks/mapper/has_many_spec.rb in yaks-0.4.2 vs spec/unit/yaks/mapper/has_many_spec.rb in yaks-0.4.3

- old
+ new

@@ -17,14 +17,14 @@ subject(:shoe_association) { closet_mapper.associations.first } its(:singular_name) { should eq 'shoe' } let(:closet) { - double( + fake( :shoes => [ - double(size: 9, color: :blue), - double(size: 11.5, color: :red), + fake(size: 9, color: :blue), + fake(size: 11.5, color: :red), ] ) } it 'should map the subresources' do @@ -33,17 +33,45 @@ type: 'shoe', members: [ Yaks::Resource.new(type: 'shoe', attributes: {:size => 9, :color => :blue}), Yaks::Resource.new(type: 'shoe', attributes: {:size => 11.5, :color => :red}) ], - members_rel: 'rel:src=collection&dest=shoes' + collection_rel: 'rel:shoes' ) ) end + it 'should map nil to a NullResource collection' do + expect(closet_mapper.call(fake(shoes: nil)).subresources).to eql( + 'http://foo/shoes' => Yaks::NullResource.new(collection: true) + ) + end + + context 'without an explicit mapper' do + let(:dress_mapper) { + Class.new(Yaks::Mapper) { type 'dress' ; attributes :color } + } + + before do + closet_mapper_class.class_eval do + has_many :dresses + end + + stub(closet_mapper.policy).derive_mapper_from_association(any_args) do + dress_mapper + end + end + + it 'should derive it from policy' do + expect(closet_mapper.policy).to equal policy + closet_mapper.call(fake(shoes: [], dresses: [fake(color: 'blue')])) + end + end + + describe '#collection_mapper' do let(:collection_mapper) { Yaks::Undefined } - subject(:has_many) { described_class.new(:name, :mapper, :rel, collection_mapper) } + subject(:has_many) { described_class.new(name: :name, mapper: :mapper, rel: :rel, collection_mapper: collection_mapper) } context 'when the collection mapper is undefined' do it 'should derive one from collection and policy' do expect(has_many.collection_mapper([], Yaks::DefaultPolicy.new)).to equal Yaks::CollectionMapper end