jsonapi-deserializable/spec/resource/DSL/attribute_spec.rb
Lucas Hosseini dca9420f7a Proper tests. (#1)
* Add tests for Deserializable::Resource.

* Add codecov integration.

* Add tests for Deserializable::Relationship + fix bad arity.
2016-11-19 22:13:33 +01:00

58 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| field foo: foo }
end
actual = klass.call(payload)
expected = { foo: 'bar' }
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| field foo: foo }
end
actual = klass.call(payload)
expected = {}
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| field foo: foo }
end
actual = klass.call(payload)
expected = {}
expect(actual).to eq(expected)
end
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