spec/serializer_spec.rb in jsonapi-serializers-0.5.0 vs spec/serializer_spec.rb in jsonapi-serializers-0.6.0
- old
+ new
@@ -321,10 +321,39 @@
'links' => {
'self' => '/posts/1',
},
})
end
+ it 'can find the correct serializer by object class name' do
+ post = create(:post)
+ primary_data = serialize_primary(post)
+ expect(primary_data).to eq({
+ 'id' => '1',
+ 'type' => 'posts',
+ 'attributes' => {
+ 'title' => 'Title for Post 1',
+ 'long-content' => 'Body for Post 1',
+ },
+ 'links' => {
+ 'self' => '/posts/1',
+ },
+ 'relationships' => {
+ 'author' => {
+ 'links' => {
+ 'self' => '/posts/1/relationships/author',
+ 'related' => '/posts/1/author',
+ },
+ },
+ 'long-comments' => {
+ 'links' => {
+ 'self' => '/posts/1/relationships/long-comments',
+ 'related' => '/posts/1/long-comments',
+ },
+ },
+ },
+ })
+ end
end
describe 'JSONAPI::Serializer.serialize' do
# The following tests rely on the fact that serialize_primary has been tested above, so object
# primary data is not explicitly tested here. If things are broken, look above here first.
@@ -763,9 +792,41 @@
data = JSONAPI::Serializer.serialize(post, serializer: MyApp::PostSerializerWithBaseUrl)
expect(data['data']['links']['self']).to eq('http://example.com/posts/1')
expect(data['data']['relationships']['author']['links']).to eq({
'self' => 'http://example.com/posts/1/relationships/author',
'related' => 'http://example.com/posts/1/author'
+ })
+ end
+ end
+
+ describe 'inheritance through subclassing' do
+ it 'inherits attributes' do
+ tagged_post = create(:tagged_post)
+ options = {serializer: MyApp::PostSerializerWithInheritedProperties}
+ data = JSONAPI::Serializer.serialize(tagged_post, options);
+ expect(data['data']['attributes']['title']).to eq('Title for TaggedPost 1');
+ expect(data['data']['attributes']['tag']).to eq('Tag for TaggedPost 1');
+ end
+
+ it 'inherits relations' do
+ long_comments = create_list(:long_comment, 2)
+ tagged_post = create(:tagged_post, :with_author, long_comments: long_comments)
+ options = {serializer: MyApp::PostSerializerWithInheritedProperties}
+ data = JSONAPI::Serializer.serialize(tagged_post, options);
+
+ expect(data['data']['relationships']).to eq({
+ 'author' => {
+ 'links' => {
+ 'self' => '/tagged-posts/1/relationships/author',
+ 'related' => '/tagged-posts/1/author',
+ },
+ },
+ 'long-comments' => {
+ 'links' => {
+ 'self' => '/tagged-posts/1/relationships/long-comments',
+ 'related' => '/tagged-posts/1/long-comments',
+ }
+ }
})
end
end
end