spec/serializer_spec.rb in jsonapi-serializers-0.9.0 vs spec/serializer_spec.rb in jsonapi-serializers-0.10.0
- old
+ new
@@ -1,5 +1,9 @@
+require 'active_model/errors'
+require 'active_model/naming'
+require 'active_model/translation'
+
describe JSONAPI::Serializer do
def serialize_primary(object, options = {})
# Note: intentional high-coupling to protected method for tests.
JSONAPI::Serializer.send(:serialize_primary, object, options)
end
@@ -348,10 +352,69 @@
},
})
end
end
+ # The members data and errors MUST NOT coexist in the same document.
+ describe 'JSONAPI::Serializer.serialize_errors' do
+ it 'can include a top level errors node' do
+ errors = [
+ {
+ 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'title' => 'Invalid Attribute',
+ 'detail' => 'First name must contain at least three characters.'
+ },
+ {
+ 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'title' => 'Invalid Attribute',
+ 'detail' => 'First name must contain an emoji.'
+ }
+ ]
+ expect(JSONAPI::Serializer.serialize_errors(errors)).to eq({'errors' => errors})
+ end
+
+ it 'works for active_record' do
+ # DummyUser exists so we can test calling user.errors.to_hash(full_messages: true)
+ class DummyUser
+ extend ActiveModel::Naming
+ extend ActiveModel::Translation
+
+ def initialize
+ @errors = ActiveModel::Errors.new(self)
+ @errors.add(:email, :invalid, message: 'is invalid')
+ @errors.add(:email, :blank, message: "can't be blank")
+ @errors.add(:first_name, :blank, message: "can't be blank")
+ end
+
+ attr_accessor :first_name, :email
+ attr_reader :errors
+
+ def read_attribute_for_validation(attr)
+ send(attr)
+ end
+ end
+ user = DummyUser.new
+ jsonapi_errors = [
+ {
+ 'source' => { 'pointer' => '/data/attributes/email' },
+ 'detail' => 'Email is invalid'
+ },
+ {
+ 'source' => { 'pointer' => '/data/attributes/email' },
+ 'detail' => "Email can't be blank"
+ },
+ {
+ 'source' => { 'pointer' => '/data/attributes/first-name' },
+ 'detail' => "First name can't be blank"
+ }
+ ]
+ expect(JSONAPI::Serializer.serialize_errors(user.errors)).to eq({
+ 'errors' => jsonapi_errors,
+ })
+ end
+ end
+
describe 'JSONAPI::Serializer.serialize' do
# The following tests rely on the fact that serialize_primary has been tested above, so object
# primary data is not explicitly tested here. If things are broken, look above here first.
it 'can serialize a nil object' do
@@ -379,10 +442,11 @@
expect(JSONAPI::Serializer.serialize(post, meta: meta)).to eq({
'meta' => meta,
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
})
end
+ # TODO: remove this code on next major release
it 'can include a top level errors node - deprecated' do
post = create(:post)
errors = [
{
"source" => { "pointer" => "/data/attributes/first-name" },
@@ -397,24 +461,9 @@
]
expect(JSONAPI::Serializer.serialize(post, errors: errors)).to eq({
'errors' => errors,
'data' => serialize_primary(post, {serializer: MyApp::PostSerializer}),
})
- end
- it 'can include a top level errors node' do
- errors = [
- {
- "source" => { "pointer" => "/data/attributes/first-name" },
- "title" => "Invalid Attribute",
- "detail" => "First name must contain at least three characters."
- },
- {
- "source" => { "pointer" => "/data/attributes/first-name" },
- "title" => "Invalid Attribute",
- "detail" => "First name must contain an emoji."
- }
- ]
- expect(JSONAPI::Serializer.serialize_errors(errors)).to eq({'errors' => errors})
end
it 'can serialize a single object with an `each` method by passing skip_collection_check: true' do
post = create(:post)
post.define_singleton_method(:each) do
"defining this just to defeat the duck-type check"