jsonapi-deserializable/spec/resource/DSL/attribute_spec.rb
Lucas Hosseini 538e09c5b3 Configurable deserialization + reverse mapping (#10)
* Deserialize all fields.
* Make blocks return hash instead of using fields method.
* Ensure valid payload.
* Make reverse mapping available.
2016-11-27 03:56:42 +01:00

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