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