|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module ActionController |
| 4 | + module Serialization |
| 5 | + class JsonApiHasOneLinksTest < ActionController::TestCase |
| 6 | + LinkTagSerializer = Class.new(ActiveModel::Serializer) do |
| 7 | + has_one :post, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } }, data: false |
| 8 | + end |
| 9 | + |
| 10 | + class MyController < ActionController::Base |
| 11 | + def render_resource_with_has_one_association |
| 12 | + @tag = Tag.new(id: 1) |
| 13 | + @tag.post = Post.new(id: 1) |
| 14 | + render json: @tag, adapter: :json_api, serializer: LinkTagSerializer |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + tests MyController |
| 19 | + |
| 20 | + def test_render_resource_with_has_one_association |
| 21 | + get :render_resource_with_has_one_association |
| 22 | + expected = { |
| 23 | + data: { |
| 24 | + id: '1', |
| 25 | + type: 'tags', |
| 26 | + relationships: { |
| 27 | + post: { |
| 28 | + links: { |
| 29 | + related: 'http://test.host/tags/1/post' |
| 30 | + } |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + assert_equal expected.to_json, response.body |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + class JsonApiHasOneLinksWithDataTest < ActionController::TestCase |
| 40 | + LinkTagSerializer = Class.new(ActiveModel::Serializer) do |
| 41 | + has_one :post, links: { related: -> (object, name) { "http://test.host/tags/#{object.id}/#{name}" } } |
| 42 | + end |
| 43 | + |
| 44 | + class MyController < ActionController::Base |
| 45 | + def render_resource_with_has_one_association |
| 46 | + @tag = Tag.new(id: 1) |
| 47 | + @tag.post = Post.new(id: 1) |
| 48 | + render json: @tag, adapter: :json_api, serializer: LinkTagSerializer |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + tests MyController |
| 53 | + |
| 54 | + def test_render_resource_with_has_one_association |
| 55 | + get :render_resource_with_has_one_association |
| 56 | + expected = { |
| 57 | + data: { |
| 58 | + id: '1', |
| 59 | + type: 'tags', |
| 60 | + relationships: { |
| 61 | + post: { |
| 62 | + data: { id: '1', type: 'posts' }, |
| 63 | + links: { |
| 64 | + related: 'http://test.host/tags/1/post' |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + assert_equal expected.to_json, response.body |
| 71 | + end |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments