Sha256: a1f855018da834e0af14b549d8682ce1f4530f5d2cfe74c253707f8dd1969a51

Contents?: true

Size: 1.94 KB

Versions: 3

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'

RSpec.describe Yaks::Mapper::AssociationMapper do
  include_context 'yaks context'

  subject(:association_mapper) { described_class.new(parent_mapper, association, yaks_context) }

  let(:parent_mapper_class) { Yaks::Mapper }
  let(:parent_mapper)       { parent_mapper_class.new(yaks_context) }

  fake(:association) { Yaks::Mapper::Association }

  its(:policy) { should be policy }

  let(:mapper_stack) { [:bottom_mapper] }

  describe '#call' do
    context 'when the association should be rendered as link' do
      before do
        stub(association).render_as_link?(parent_mapper) { true }
        stub(association).map_rel(policy) { 'rels:the_rel' }
        stub(association).href { 'http://this/is_where_the_associated_thing_can_be_found' }
      end

      it 'should render a link' do
        expect(association_mapper.call(Yaks::Resource.new)).to eql Yaks::Resource.new(
          links: [
            Yaks::Resource::Link.new(
              rel: 'rels:the_rel',
              uri: 'http://this/is_where_the_associated_thing_can_be_found'
            )
          ]
        )
      end
    end

    context 'when the association should be rendered as a subresource' do
      before do
        stub(association).render_as_link?(parent_mapper) { false }
        stub(association).map_rel(policy) { 'rels:the_rel' }
        stub(association).name { :the_name }
        stub(association).map_resource(:the_object, association_mapper.context) { :the_resource }

        stub(parent_mapper).load_association(:the_name) { :the_object }
      end

      it 'should render a link' do
        expect(association_mapper.call(Yaks::Resource.new)).to eql Yaks::Resource.new(
          subresources: {
            'rels:the_rel' => :the_resource
          }
        )
      end

      it 'should add the mapper to the mapper_stack' do
        expect(association_mapper.context[:mapper_stack]).to eql [:bottom_mapper, parent_mapper]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
yaks-0.6.2 spec/unit/yaks/mapper/association_mapper_spec.rb
yaks-0.6.1 spec/unit/yaks/mapper/association_mapper_spec.rb
yaks-0.6.0 spec/unit/yaks/mapper/association_mapper_spec.rb