active_model_serializers/test/adapter/json_api/links_test.rb
Noah Silas 94db09b3f6 Fix RuboCop 0.40 linter errors (#1722)
These errors are breaking the build, which seems to use RuboCop 0.40 [1]
despite the Gemfile.lock pinning rubocop to 0.38.

New lints that I am updating the code style to reflect:

- Style/EmptyCaseCondition: Do not use empty case condition, instead use
  an if expression.

- Style/MultilineArrayBraceLayout: Closing array brace must be on the
  same line as the last array element when opening brace is on the same
  line as the first array element.

- Style/MultilineHashBraceLayout: Closing hash brace must be on the same
  line as the last hash element when opening brace is on the same line
  as the first hash element.

- Style/MultilineMethodCallBraceLayout: Closing method call brace must
  be on the line after the last argument when opening brace is on a
  separate line from the first argument.

[1] https://github.com/bbatsov/rubocop/releases/tag/v0.40.0
2016-05-26 12:58:05 -04:00

95 lines
2.9 KiB
Ruby

require 'test_helper'
module ActiveModelSerializers
module Adapter
class JsonApi
class LinksTest < ActiveSupport::TestCase
LinkAuthor = Class.new(::Model)
class LinkAuthorSerializer < ActiveModel::Serializer
link :self do
href "http://example.com/link_author/#{object.id}"
meta stuff: 'value'
end
link(:author) { link_author_url(object.id) }
link(:link_authors) { url_for(controller: 'link_authors', action: 'index', only_path: false) }
link(:posts) { link_author_posts_url(object.id) }
link :resource, 'http://example.com/resource'
link :yet_another do
"http://example.com/resource/#{object.id}"
end
end
def setup
Rails.application.routes.draw do
resources :link_authors do
resources :posts
end
end
@post = Post.new(id: 1337, comments: [], author: nil)
@author = LinkAuthor.new(id: 1337, posts: [@post])
end
def test_toplevel_links
hash = ActiveModelSerializers::SerializableResource.new(
@post,
adapter: :json_api,
links: {
self: {
href: 'http://example.com/posts',
meta: {
stuff: 'value'
}
}
}
).serializable_hash
expected = {
self: {
href: 'http://example.com/posts',
meta: {
stuff: 'value'
}
}
}
assert_equal(expected, hash[:links])
end
def test_nil_toplevel_links
hash = ActiveModelSerializers::SerializableResource.new(
@post,
adapter: :json_api,
links: nil
).serializable_hash
refute hash.key?(:links), 'No links key to be output'
end
def test_nil_toplevel_links_json_adapter
hash = ActiveModelSerializers::SerializableResource.new(
@post,
adapter: :json,
links: nil
).serializable_hash
refute hash.key?(:links), 'No links key to be output'
end
def test_resource_links
hash = serializable(@author, adapter: :json_api).serializable_hash
expected = {
self: {
href: 'http://example.com/link_author/1337',
meta: {
stuff: 'value'
}
},
author: 'http://example.com/link_authors/1337',
:"link-authors" => 'http://example.com/link_authors',
resource: 'http://example.com/resource',
posts: 'http://example.com/link_authors/1337/posts',
:"yet-another" => 'http://example.com/resource/1337'
}
assert_equal(expected, hash[:data][:links])
end
end
end
end
end