Skip to content

Commit

Permalink
Add tests for meta on resource objects.
Browse files Browse the repository at this point in the history
  • Loading branch information
rafael authored and beauby committed Jan 19, 2016
1 parent 0bd5c65 commit 207c85f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
63 changes: 63 additions & 0 deletions test/adapter/json_api/resource_meta_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'test_helper'

module ActiveModel
class Serializer
module Adapter
class JsonApi
class ResourceMetaTest < Minitest::Test
class MetaHashPostSerializer < ActiveModel::Serializer
attributes :id
meta stuff: 'value'
end

class MetaBlockPostSerializer < ActiveModel::Serializer
attributes :id
meta do
{ comments_count: object.comments.count }
end
end

def setup
@post = Post.new(id: 1337, comments: [], author: nil)
end

def test_meta_hash_object_resource
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaHashPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
stuff: 'value'
}
assert_equal(expected, hash[:data][:meta])
end

def test_meta_block_object_resource
hash = ActiveModel::SerializableResource.new(
@post,
serializer: MetaBlockPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
comments_count: @post.comments.count
}
assert_equal(expected, hash[:data][:meta])
end

def test_meta_object_resource_in_array
hash = ActiveModel::SerializableResource.new(
[@post, @post],
each_serializer: MetaBlockPostSerializer,
adapter: :json_api
).serializable_hash
expected = {
comments_count: @post.comments.count
}
assert_equal([expected, expected], hash[:data].map { |obj| obj[:meta] })
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions test/serializers/meta_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module ActiveModel
class Serializer
class MetaTest < ActiveSupport::TestCase
MetaBlogSerializer = Class.new(ActiveModel::Serializer)

def setup
@blog = Blog.new(id: 1,
name: 'AMS Hints',
Expand Down Expand Up @@ -125,6 +127,20 @@ def test_meta_is_present_on_arrays_with_root
}
assert_equal(expected, actual)
end

def test_meta_is_set_with_direct_attributes
MetaBlogSerializer.meta stuff: 'value'
blog_meta_serializer = MetaBlogSerializer.new(@blog)
assert_equal(blog_meta_serializer.meta, stuff: 'value')
end

def test_meta_is_set_with_block
MetaBlogSerializer.meta do
{ articles_count: object.articles.count }
end
blog_meta_serializer = MetaBlogSerializer.new(@blog)
assert_equal(blog_meta_serializer.meta, articles_count: @blog.articles.count)
end
end
end
end

0 comments on commit 207c85f

Please sign in to comment.