Skip to content

Commit 5d8e1ce

Browse files
Shuhei Kitagawashishirmk
authored andcommitted
Refactor tests for key_transform method
1 parent fe5ecb5 commit 5d8e1ce

3 files changed

Lines changed: 69 additions & 84 deletions

File tree

spec/lib/object_serializer_class_methods_spec.rb

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,6 @@
143143
describe '#attribute' do
144144
subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash }
145145

146-
after do
147-
MovieSerializer.attributes_to_serialize = {}
148-
end
149-
150146
context 'with block' do
151147
before do
152148
movie.release_year = 2008
@@ -155,10 +151,70 @@
155151
end
156152
end
157153

154+
after do
155+
MovieSerializer.attributes_to_serialize.delete(:title_with_year)
156+
end
157+
158158
it 'returns correct hash when serializable_hash is called' do
159159
expect(serializable_hash[:data][:attributes][:name]).to eq movie.name
160160
expect(serializable_hash[:data][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})"
161161
end
162162
end
163163
end
164+
165+
describe '#key_transform' do
166+
subject(:hash) { movie_serializer_class.new([movie, movie], include: [:movie_type]).serializable_hash }
167+
168+
let(:movie_serializer_class) { "#{key_transform}_movie_serializer".classify.constantize }
169+
170+
before(:context) do
171+
[:dash, :camel, :camel_lower, :underscore].each do |key_transform|
172+
movie_serializer_name = "#{key_transform}_movie_serializer".classify
173+
movie_type_serializer_name = "#{key_transform}_movie_type_serializer".classify
174+
# https://stackoverflow.com/questions/4113479/dynamic-class-definition-with-a-class-name
175+
movie_serializer_class = Object.const_set(movie_serializer_name, Class.new)
176+
# https://rubymonk.com/learning/books/5-metaprogramming-ruby-ascent/chapters/24-eval/lessons/67-instance-eval
177+
movie_serializer_class.instance_eval do
178+
include FastJsonapi::ObjectSerializer
179+
set_type :movie
180+
set_key_transform key_transform
181+
attributes :name, :release_year
182+
has_many :actors
183+
belongs_to :owner, record_type: :user
184+
belongs_to :movie_type, serializer: "#{key_transform}_movie_type"
185+
end
186+
movie_type_serializer_class = Object.const_set(movie_type_serializer_name, Class.new)
187+
movie_type_serializer_class.instance_eval do
188+
include FastJsonapi::ObjectSerializer
189+
set_key_transform key_transform
190+
set_type :movie_type
191+
attributes :name
192+
end
193+
end
194+
end
195+
196+
context 'when key_transform is dash' do
197+
let(:key_transform) { :dash }
198+
199+
it_behaves_like 'returning key transformed hash', :'movie-type', :'release-year'
200+
end
201+
202+
context 'when key_transform is camel' do
203+
let(:key_transform) { :camel }
204+
205+
it_behaves_like 'returning key transformed hash', :MovieType, :ReleaseYear
206+
end
207+
208+
context 'when key_transform is camel_lower' do
209+
let(:key_transform) { :camel_lower }
210+
211+
it_behaves_like 'returning key transformed hash', :movieType, :releaseYear
212+
end
213+
214+
context 'when key_transform is underscore' do
215+
let(:key_transform) { :underscore }
216+
217+
it_behaves_like 'returning key transformed hash', :movie_type, :release_year
218+
end
219+
end
164220
end

spec/lib/object_serializer_key_transform_spec.rb

Lines changed: 0 additions & 80 deletions
This file was deleted.

spec/shared/examples/object_serializer_class_methods_examples.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@
77
expect(relationship[:record_type]).to be record_type
88
end
99
end
10+
11+
RSpec.shared_examples 'returning key transformed hash' do |movie_type, release_year|
12+
it 'returns correctly transformed hash' do
13+
expect(hash[:data][0][:attributes]).to have_key(release_year)
14+
expect(hash[:data][0][:relationships]).to have_key(movie_type)
15+
expect(hash[:data][0][:relationships][movie_type][:data][:type]).to eq(movie_type)
16+
expect(hash[:included][0][:type]).to eq(movie_type)
17+
end
18+
end

0 commit comments

Comments
 (0)