jsonapi-deserializable/spec/resource/DSL/attribute_spec.rb
2017-07-14 15:07:02 +02:00

68 lines
1.9 KiB
Ruby

require 'spec_helper'
describe JSONAPI::Deserializable::Resource, '.attribute' do
context 'when attribute is present' do
context 'when a block is specified' do
it 'creates corresponding field' 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' }
expect(actual).to eq(expected)
end
end
context 'when no block is specified' do
it 'defaults to creating a field with same name' do
payload = {
'data' => {
'type' => 'foo',
'attributes' => { 'foo' => 'bar' }
}
}
klass = Class.new(JSONAPI::Deserializable::Resource) do
attribute(:foo)
end
actual = klass.call(payload)
expected = { foo: 'bar' }
expect(actual).to eq(expected)
end
end
end
context 'when attribute is absent' do
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 = {}
expect(actual).to eq(expected)
end
end
context 'when attributes member is absent' do
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 = {}
expect(actual).to eq(expected)
end
end
end