Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite test to use SerializableResource. #1092

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions test/adapter/json_api/resource_type_config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,39 @@ def setup
@author.posts = []
end

def with_jsonapi_resource_type type
old_type = ActiveModel::Serializer.config[:jsonapi_resource_type]
ActiveModel::Serializer.config[:jsonapi_resource_type] = type
def with_jsonapi_resource_type(type)
old_type = ActiveModel::Serializer.config.jsonapi_resource_type
ActiveModel::Serializer.config.jsonapi_resource_type = type
yield
ensure
ActiveModel::Serializer.config[:jsonapi_resource_type] = old_type
ActiveModel::Serializer.config.jsonapi_resource_type = old_type
end

def with_adapter(adapter)
old_adapter = ActiveModel::Serializer.config.adapter
ActiveModel::Serializer.config.adapter = adapter
yield
ensure
ActiveModel::Serializer.config.adapter = old_adapter
end

def test_config_plural
with_jsonapi_resource_type :plural do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comments', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :plural do
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
ActionController::Base.cache_store.clear
assert_equal('comments', hash[:data][:type])
end
end
end

def test_config_singular
with_jsonapi_resource_type :singular do
serializer = CommentSerializer.new(@comment)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer)
ActionController::Base.cache_store.clear
assert_equal('comment', adapter.serializable_hash[:data][:type])
with_adapter :json_api do
with_jsonapi_resource_type :singular do
hash = ActiveModel::SerializableResource.serialize(@comment).serializable_hash
ActionController::Base.cache_store.clear
assert_equal('comment', hash[:data][:type])
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or

def serialize_with_adapter_and_jsonapi_resource_type(resource, adapter, jsonapi_resource_type)
              with_adapter adapter do
             with_jsonapi_resource_type json_api_resource_type do
                hash = ActiveModel::SerializableResource.serialize(resource).serializable_hash
                ActionController::Base.cache_store.clear
              yield hash
              end
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to keep it in line with a possible global with_adapter method.

end
end
Expand Down