Integrate tests for #set_id and #attribute to object_serializer_class_methods_spec

This commit is contained in:
Shuhei Kitagawa 2018-03-29 20:22:05 +09:00 committed by Shishir Kakaraddi
parent 0d8bbedcdd
commit fb7d01368a
4 changed files with 67 additions and 53 deletions

View File

@ -11,6 +11,10 @@ describe FastJsonapi::ObjectSerializer do
serializer.has_many *children
end
after do
serializer.relationships_to_serialize = {}
end
context 'with namespace' do
let(:serializer) { AppName::V1::MovieSerializer }
let(:children) { [:roles] }
@ -52,6 +56,10 @@ describe FastJsonapi::ObjectSerializer do
MovieSerializer.belongs_to *parent
end
after do
MovieSerializer.relationships_to_serialize = {}
end
context 'with overrides' do
let(:parent) { [:area, id_method_name: :blah_id, record_type: :awesome_area, serializer: :my_area] }
@ -72,6 +80,10 @@ describe FastJsonapi::ObjectSerializer do
MovieSerializer.has_one *partner
end
after do
MovieSerializer.relationships_to_serialize = {}
end
context 'with overrides' do
let(:partner) { [:area, id_method_name: :blah_id, record_type: :awesome_area, serializer: :my_area] }
@ -85,13 +97,68 @@ describe FastJsonapi::ObjectSerializer do
end
end
describe '#set_id' do
subject(:serializable_hash) { MovieSerializer.new(resource).serializable_hash }
before do
MovieSerializer.set_id :owner_id
end
after do
MovieSerializer.set_id nil
end
context 'when one record is given' do
let(:resource) { movie }
it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id
end
end
context 'when an array of records is given' do
let(:resource) { [movie, movie] }
it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id
expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id
end
end
end
describe '#use_hyphen' do
subject { MovieSerializer.use_hyphen }
after do
MovieSerializer.transform_method = nil
end
it 'sets the correct transform_method when use_hyphen is used' do
warning_message = "DEPRECATION WARNING: use_hyphen is deprecated and will be removed from fast_jsonapi 2.0 use (set_key_transform :dash) instead\n"
expect { subject }.to output(warning_message).to_stderr
expect(MovieSerializer.instance_variable_get(:@transform_method)).to eq :dasherize
end
end
describe '#attribute' do
subject(:serializable_hash) { MovieSerializer.new(movie).serializable_hash }
after do
MovieSerializer.attributes_to_serialize = {}
end
context 'with block' do
before do
movie.release_year = 2008
MovieSerializer.attribute :title_with_year do |record|
"#{record.name} (#{record.release_year})"
end
end
it 'returns correct hash when serializable_hash is called' do
expect(serializable_hash[:data][:attributes][:name]).to eq movie.name
expect(serializable_hash[:data][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})"
end
end
end
end

View File

@ -1,30 +0,0 @@
require 'spec_helper'
describe FastJsonapi::ObjectSerializer do
include_context 'movie class'
context 'when setting id' do
subject(:serializable_hash) { MovieSerializer.new(resource).serializable_hash }
before(:all) do
MovieSerializer.set_id :owner_id
end
context 'when one record is given' do
let(:resource) { movie }
it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][:id].to_i).to eq movie.owner_id
end
end
context 'when an array of records is given' do
let(:resource) { [movie, movie] }
it 'returns correct hash which id equals owner_id' do
expect(serializable_hash[:data][0][:id].to_i).to eq movie.owner_id
expect(serializable_hash[:data][1][:id].to_i).to eq movie.owner_id
end
end
end
end

View File

@ -1,13 +0,0 @@
require 'spec_helper'
describe FastJsonapi::ObjectSerializer do
include_context 'movie class'
context 'when including attribute blocks' do
it 'returns correct hash when serializable_hash is called' do
serializable_hash = MovieSerializerWithAttributeBlock.new([movie]).serializable_hash
expect(serializable_hash[:data][0][:attributes][:name]).to eq movie.name
expect(serializable_hash[:data][0][:attributes][:title_with_year]).to eq "#{movie.name} (#{movie.release_year})"
end
end
end

View File

@ -108,15 +108,6 @@ RSpec.shared_context 'movie class' do
attributes :name
end
class MovieSerializerWithAttributeBlock
include FastJsonapi::ObjectSerializer
set_type :movie
attributes :name, :release_year
attribute :title_with_year do |record|
"#{record.name} (#{record.release_year})"
end
end
class SupplierSerializer
include FastJsonapi::ObjectSerializer
set_type :supplier
@ -169,7 +160,6 @@ RSpec.shared_context 'movie class' do
ActorSerializer
MovieType
MovieTypeSerializer
MovieSerializerWithAttributeBlock
AppName::V1::MovieSerializer
MovieStruct
ActorStruct