|
18 | 18 | describe '.call' do
|
19 | 19 | subject { described_class.call(object, env) }
|
20 | 20 | let(:jsonapi_serializer_options) { nil }
|
21 |
| - let(:env) { { 'jsonapi_serializer_options' => jsonapi_serializer_options } } |
| 21 | + let(:meta) { { pagination: { page: 1, total: 2 } } } |
| 22 | + let(:links) { { self: 'https://example/org' } } |
| 23 | + let(:env) { { 'jsonapi_serializer_options' => jsonapi_serializer_options, 'meta' => meta, 'links' => links } } |
22 | 24 |
|
23 | 25 | context 'when the object is a string' do
|
24 | 26 | let(:object) { 'I am a string' }
|
| 27 | + let(:response) { ::Grape::Json.dump({ data: object, meta: meta, links: links }) } |
25 | 28 |
|
26 |
| - it { is_expected.to eq object } |
| 29 | + it { is_expected.to eq response } |
27 | 30 | end
|
28 | 31 |
|
29 | 32 | context 'when the object is serializable' do
|
|
33 | 36 |
|
34 | 37 | context 'when the object has a model_name defined' do
|
35 | 38 | let(:object) { admin }
|
36 |
| - it { is_expected.to eq ::Grape::Json.dump(user_serializer.serializable_hash) } |
| 39 | + let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) } |
| 40 | + |
| 41 | + it { is_expected.to eq response } |
37 | 42 | end
|
38 | 43 |
|
39 | 44 | context 'when the object is a active serializable model instance' do
|
40 | 45 | let(:object) { user }
|
| 46 | + let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) } |
41 | 47 |
|
42 |
| - it { is_expected.to eq ::Grape::Json.dump(user_serializer.serializable_hash) } |
| 48 | + it { is_expected.to eq response } |
43 | 49 | end
|
44 | 50 |
|
45 |
| - context 'when the object is an array of active serializable model instances' do |
46 |
| - let(:object) { [user, another_user] } |
47 |
| - |
48 |
| - it { is_expected.to eq ::Grape::Json.dump(user_serializer.serializable_hash) } |
49 |
| - end |
| 51 | + context 'when the object is an array' do |
| 52 | + context 'when the object is an array of active serializable model instances' do |
| 53 | + let(:object) { [user, another_user] } |
| 54 | + let(:response) { ::Grape::Json.dump(user_serializer.serializable_hash.merge(meta: meta, links: links)) } |
50 | 55 |
|
51 |
| - context 'when the array contains instances of different models' do |
52 |
| - let(:object) { [user, blog_post] } |
| 56 | + it { is_expected.to eq response } |
| 57 | + end |
53 | 58 |
|
54 |
| - it 'returns an array of jsonapi serialialized objects' do |
55 |
| - expect(subject).to eq(::Grape::Json.dump([ |
56 |
| - UserSerializer.new(user, {}).serializable_hash, |
57 |
| - BlogPostSerializer.new(blog_post, {}).serializable_hash |
58 |
| - ])) |
| 59 | + context 'when the array contains instances of different models' do |
| 60 | + let(:object) { [user, blog_post] } |
| 61 | + let(:response) do |
| 62 | + ::Grape::Json.dump({ |
| 63 | + data: [ |
| 64 | + UserSerializer.new(user, {}).serializable_hash[:data], |
| 65 | + BlogPostSerializer.new(blog_post, {}).serializable_hash[:data] |
| 66 | + ], |
| 67 | + meta: meta, |
| 68 | + links: links |
| 69 | + }) |
| 70 | + end |
| 71 | + |
| 72 | + it 'returns an array of jsonapi serialialized objects' do |
| 73 | + expect(subject).to eq response |
| 74 | + end |
59 | 75 | end
|
60 |
| - end |
61 | 76 |
|
62 |
| - context 'when the object is an empty array ' do |
63 |
| - let(:object) { [] } |
| 77 | + context 'when the object is an empty array' do |
| 78 | + let(:object) { [] } |
64 | 79 |
|
65 |
| - it { is_expected.to eq ::Grape::Json.dump(object) } |
66 |
| - end |
| 80 | + it { is_expected.to eq({ data: [], meta: meta, links: links }.to_json) } |
| 81 | + end |
67 | 82 |
|
68 |
| - context 'when the object is an array of null objects ' do |
69 |
| - let(:object) { [nil, nil] } |
| 83 | + context 'when the object is an array of null objects' do |
| 84 | + let(:object) { [nil, nil] } |
70 | 85 |
|
71 |
| - it { is_expected.to eq ::Grape::Json.dump(object) } |
| 86 | + it { is_expected.to eq({ data: [nil, nil], meta: meta, links: links }.to_json) } |
| 87 | + end |
72 | 88 | end
|
73 | 89 |
|
74 |
| - context 'when the object is a Hash of plain values' do |
75 |
| - let(:object) { user.as_json } |
| 90 | + context 'when the object is a hash' do |
| 91 | + context 'when the object is an empty hash' do |
| 92 | + let(:object) { {} } |
76 | 93 |
|
77 |
| - it { is_expected.to eq ::Grape::Json.dump(object) } |
78 |
| - end |
| 94 | + it { is_expected.to eq({ data: {}, meta: meta, links: links }.to_json) } |
| 95 | + end |
79 | 96 |
|
80 |
| - context 'when the object is a Hash with serializable object values' do |
81 |
| - let(:object) do |
82 |
| - { |
83 |
| - user: user, |
84 |
| - blog_post: blog_post |
85 |
| - } |
| 97 | + context 'when the object is a Hash of plain values' do |
| 98 | + let(:object) { user.as_json } |
| 99 | + |
| 100 | + it { is_expected.to eq ::Grape::Json.dump({ data: user.as_json, meta: meta, links: links }) } |
86 | 101 | end
|
87 | 102 |
|
88 |
| - it 'returns an hash of with jsonapi serialialized objects values' do |
89 |
| - expect(subject).to eq(::Grape::Json.dump({ |
90 |
| - user: UserSerializer.new(user, {}).serializable_hash, |
91 |
| - blog_post: BlogPostSerializer.new(blog_post, {}).serializable_hash |
92 |
| - })) |
| 103 | + context 'when the object is a Hash with serializable object values' do |
| 104 | + let(:object) do |
| 105 | + { user: user, blog_post: blog_post } |
| 106 | + end |
| 107 | + |
| 108 | + let(:response) do |
| 109 | + ::Grape::Json.dump({ |
| 110 | + data: { |
| 111 | + user: UserSerializer.new(user, {}).serializable_hash[:data], |
| 112 | + blog_post: BlogPostSerializer.new(blog_post, {}).serializable_hash[:data] |
| 113 | + }, |
| 114 | + meta: meta, |
| 115 | + links: links |
| 116 | + }) |
| 117 | + end |
| 118 | + |
| 119 | + it 'returns an hash of with jsonapi serialialized objects values' do |
| 120 | + expect(subject).to eq response |
| 121 | + end |
93 | 122 | end
|
94 | 123 | end
|
95 | 124 |
|
96 | 125 | context 'when the object is nil' do
|
97 | 126 | let(:object) { nil }
|
98 | 127 |
|
99 |
| - it { is_expected.to eq 'null' } |
| 128 | + it { is_expected.to eq({ data: nil, meta: meta, links: links }.to_json) } |
100 | 129 | end
|
101 | 130 |
|
102 | 131 | context 'when the object is a number' do
|
103 | 132 | let(:object) { 42 }
|
104 | 133 |
|
105 |
| - it { is_expected.to eq '42' } |
| 134 | + it { is_expected.to eq({ data: 42, meta: meta, links: links }.to_json) } |
106 | 135 | end
|
107 | 136 |
|
108 | 137 | context 'when a custom serializer is passed as an option' do
|
|
113 | 142 | }
|
114 | 143 | end
|
115 | 144 |
|
116 |
| - it { is_expected.to eq ::Grape::Json.dump(another_user_serializer.serializable_hash) } |
| 145 | + it { is_expected.to eq ::Grape::Json.dump(another_user_serializer.serializable_hash.merge(meta: meta, links: links)) } |
117 | 146 | end
|
118 | 147 | end
|
119 | 148 | end
|
|
0 commit comments