Sha256: c2d2e80ea0d586f217b5522f0335aa0298296cd0a7a9fb9bc19b5d76f4a1c0a8

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

require 'test_helper'

module ActiveModel
  class Serializer
    class Adapter
      class JsonApi
        class HasOneTest < Minitest::Test
          def setup
            @author = Author.new(id: 1, name: 'Steve K.')
            @bio = Bio.new(id: 43, content: 'AMS Contributor')
            @author.bio = @bio
            @bio.author = @author
            @post = Post.new(id: 42, title: 'New Post', body: 'Body')
            @anonymous_post = Post.new(id: 43, title: 'Hello!!', body: 'Hello, world!!')
            @comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
            @post.comments = [@comment]
            @anonymous_post.comments = []
            @comment.post = @post
            @comment.author = nil
            @post.author = @author
            @anonymous_post.author = nil
            @blog = Blog.new(id: 1, name: "My Blog!!")
            @blog.writer = @author
            @blog.articles = [@post, @anonymous_post]
            @author.posts = []
            @author.roles = []

            @serializer = AuthorSerializer.new(@author)
            @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio,posts')
          end

          def test_includes_bio_id
            expected = { linkage: { type: "bios", id: "43" } }

            assert_equal(expected, @adapter.serializable_hash[:data][:links][:bio])
          end

          def test_includes_linked_bio
            @adapter = ActiveModel::Serializer::Adapter::JsonApi.new(@serializer, include: 'bio')

            expected = [
              {
                id: "43",
                rating: nil,
                type: "bios",
                content:"AMS Contributor",
                links: {
                  author: { linkage: { type: "authors", id: "1" } }
                }
              }
            ]

            assert_equal(expected, @adapter.serializable_hash[:included])
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
cm-active_model_serializers-0.10.0.rc1.1 test/adapter/json_api/has_one_test.rb
active_model_serializers-0.10.0.rc1 test/adapter/json_api/has_one_test.rb