|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActiveModel |
| 4 | + class Serializer |
| 5 | + class Adapter |
| 6 | + class JsonApi |
| 7 | + class TopLevelLinksTest < Minitest::Test |
| 8 | + URI = 'http://example.com' |
| 9 | + |
| 10 | + def setup |
| 11 | + ActionController::Base.cache_store.clear |
| 12 | + @blog = Blog.new(id: 1, |
| 13 | + name: 'AMS Hints', |
| 14 | + writer: Author.new(id: 2, name: "Steve"), |
| 15 | + articles: [Post.new(id: 3, title: "AMS")]) |
| 16 | + end |
| 17 | + |
| 18 | + def load_adapter(paginated_collection, options = {}) |
| 19 | + options = options.merge(adapter: :json_api) |
| 20 | + ActiveModel::SerializableResource.new(paginated_collection, options) |
| 21 | + end |
| 22 | + |
| 23 | + def test_links_is_not_present_when_not_defined |
| 24 | + adapter = load_adapter(@blog) |
| 25 | + |
| 26 | + expected = { |
| 27 | + :data => { |
| 28 | + :id => "1", |
| 29 | + :type => "blogs", |
| 30 | + :attributes => { |
| 31 | + :name => "AMS Hints" |
| 32 | + }, |
| 33 | + :relationships => { |
| 34 | + :writer=> {:data => {:type => "authors", :id => "2"}}, |
| 35 | + :articles => {:data => [{:type => "posts", :id => "3"}]} |
| 36 | + } |
| 37 | + }} |
| 38 | + |
| 39 | + assert_equal expected, adapter.serializable_hash(@options) |
| 40 | + end |
| 41 | + |
| 42 | + def test_links_is_present_when_defined |
| 43 | + adapter = load_adapter(@blog, {links: links}) |
| 44 | + |
| 45 | + expected = { |
| 46 | + :data => { |
| 47 | + :id => "1", |
| 48 | + :type => "blogs", |
| 49 | + :attributes => { |
| 50 | + :name => "AMS Hints" |
| 51 | + }, |
| 52 | + :relationships => { |
| 53 | + :writer=> {:data => {:type => "authors", :id => "2"}}, |
| 54 | + :articles => {:data => [{:type => "posts", :id => "3"}]} |
| 55 | + } |
| 56 | + }, |
| 57 | + :links => {:self => "http://example.com/blogs/1"} |
| 58 | + } |
| 59 | + |
| 60 | + assert_equal expected, adapter.serializable_hash(@options) |
| 61 | + end |
| 62 | + |
| 63 | + def links |
| 64 | + { |
| 65 | + self: "#{URI}/blogs/1" |
| 66 | + } |
| 67 | + end |
| 68 | + |
| 69 | + # def test_links_is_not_present_when_not_declared |
| 70 | + # serializer = AlternateBlogSerializer.new(@blog) |
| 71 | + # adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer) |
| 72 | + # expected = { |
| 73 | + # data: { |
| 74 | + # id: "1", |
| 75 | + # type: "blogs", |
| 76 | + # attributes: { |
| 77 | + # title: "AMS Hints" |
| 78 | + # } |
| 79 | + # } |
| 80 | + # } |
| 81 | + # assert_equal expected, adapter.as_json |
| 82 | + # end |
| 83 | + |
| 84 | + # def test_links_is_not_present_on_flattenjson_adapter |
| 85 | + # serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"}) |
| 86 | + # adapter = ActiveModel::Serializer::Adapter::FlattenJson.new(serializer) |
| 87 | + # expected = {:id=>1, :title=>"AMS Hints"} |
| 88 | + # assert_equal expected, adapter.as_json |
| 89 | + # end |
| 90 | + |
| 91 | + # def test_links_is_not_present_on_json_adapter |
| 92 | + # serializer = AlternateBlogSerializer.new(@blog, :links => {:self => "/blogs/1"}) |
| 93 | + # adapter = ActiveModel::Serializer::Adapter::Json.new(serializer) |
| 94 | + # expected = {:blog=>{:id=>1, :title=>"AMS Hints"}} |
| 95 | + # assert_equal expected, adapter.as_json |
| 96 | + # end |
| 97 | + end |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments