mirror of
https://github.com/ditkrg/jsonapi-deserializable.git
synced 2026-01-23 06:16:47 +00:00
* Deserialize all fields. * Make blocks return hash instead of using fields method. * Ensure valid payload. * Make reverse mapping available.
35 lines
996 B
Ruby
35 lines
996 B
Ruby
require 'spec_helper'
|
|
|
|
describe JSONAPI::Deserializable::Resource, '.id' do
|
|
it 'creates corresponding field if id is present' do
|
|
payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } }
|
|
klass = Class.new(JSONAPI::Deserializable::Resource) do
|
|
id { |i| Hash[id: i] }
|
|
end
|
|
actual = klass.call(payload)
|
|
expected = { id: 'bar', type: 'foo' }
|
|
|
|
expect(actual).to eq(expected)
|
|
end
|
|
|
|
it 'does not create corresponding field if id is absent' do
|
|
payload = { 'data' => { 'type' => 'foo' } }
|
|
klass = Class.new(JSONAPI::Deserializable::Resource) do
|
|
id { |i| Hash[id: i] }
|
|
end
|
|
actual = klass.call(payload)
|
|
expected = { type: 'foo' }
|
|
|
|
expect(actual).to eq(expected)
|
|
end
|
|
|
|
it 'defaults to creating an id field' do
|
|
payload = { 'data' => { 'type' => 'foo', 'id' => 'bar' } }
|
|
klass = JSONAPI::Deserializable::Resource
|
|
actual = klass.call(payload)
|
|
expected = { id: 'bar', type: 'foo' }
|
|
|
|
expect(actual).to eq(expected)
|
|
end
|
|
end
|