Sha256: 5c1ac0fad10cb210b17d4ac82c155bb73d8c0bb5050927d5bc77504824a19bf2

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

require 'spec_helper'

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

  AuthorMapper = Class.new(Yaks::Mapper) { attributes :name }

  subject(:has_one)  do
    described_class.new(
      name: :author,
      item_mapper: association_mapper,
      rel: 'http://rel'
    )
  end

  let(:association_mapper) { AuthorMapper }
  let(:name)               { 'William S. Burroughs' }
  let(:author)             { fake(:name => name) }

  fake(:policy,
    derive_type_from_mapper_class: 'author',
    derive_mapper_from_association: AuthorMapper
  ){ Yaks::DefaultPolicy }

  its(:singular_name) { should eq 'author' }

  it 'should map to a single Resource' do
    expect(has_one.map_resource(author, yaks_context)).to eq Yaks::Resource.new(type: 'author', attributes: {name: name})
  end

  context 'with no mapper specified' do
    subject(:subresource)    { has_one.add_to_resource(Yaks::Resource.new, parent_mapper, yaks_context) }
    let(:association_mapper) { Yaks::Undefined }
    fake(:parent_mapper) { Yaks::Mapper }

    before do
      stub(parent_mapper).load_association(:author) { author }
    end

    it 'should derive one based on policy' do
      expect(subresource).to eql(
        Yaks::Resource.new(
          subresources: {
            'http://rel' => Yaks::Resource.new(type: 'author', attributes: {name: name})
          }
        )
      )
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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