-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for meta on resource objects.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters