1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
# frozen_string_literal: true
require 'test_helper'
module ActiveModelSerializers
module Adapter
class JsonApi
# Test 'has_many :assocs, serializer: AssocXSerializer'
class HasManyExplicitSerializerTest < ActiveSupport::TestCase
def setup
@post = Post.new(title: 'New Post', body: 'Body')
@author = Author.new(name: 'Jane Blogger')
@author.posts = [@post]
@post.author = @author
@first_comment = Comment.new(id: 1, body: 'ZOMG A COMMENT')
@second_comment = Comment.new(id: 2, body: 'ZOMG ANOTHER COMMENT')
@post.comments = [@first_comment, @second_comment]
@first_comment.post = @post
@first_comment.author = nil
@second_comment.post = @post
@second_comment.author = nil
@blog = Blog.new(id: 23, name: 'AMS Blog')
@post.blog = @blog
@serializer = PostPreviewSerializer.new(@post)
@adapter = ActiveModelSerializers::Adapter::JsonApi.new(
@serializer,
include: [:comments, :author]
)
end
def test_includes_comment_ids
expected = {
data: [
{ type: 'comments', id: '1' },
{ type: 'comments', id: '2' }
]
}
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
end
def test_includes_linked_data
# If CommentPreviewSerializer is applied correctly the body text will not be present in the output
expected = [
{
id: '1',
type: 'comments',
relationships: {
post: { data: { type: 'posts', id: @post.id.to_s } }
}
},
{
id: '2',
type: 'comments',
relationships: {
post: { data: { type: 'posts', id: @post.id.to_s } }
}
},
{
id: @author.id.to_s,
type: 'authors',
relationships: {
posts: { data: [{ type: 'posts', id: @post.id.to_s }] }
}
}
]
assert_equal(expected, @adapter.serializable_hash[:included])
end
def test_includes_author_id
expected = {
data: { type: 'authors', id: @author.id.to_s }
}
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
end
def test_explicit_serializer_with_null_resource
@post.author = nil
expected = { data: nil }
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
end
def test_explicit_serializer_with_null_collection
@post.comments = []
expected = { data: [] }
assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:comments])
end
end
end
end
end
|