Sha256: 7cc2796e614310ee250eec4a4bb36dacfcafd33817a147f510d7e5be9df36e5c

Contents?: true

Size: 1.93 KB

Versions: 9

Compression:

Stored size: 1.93 KB

Contents

require "spec_helper"
require "active_model"

describe Shamu::JsonApi::Response do
  let( :context )  { Shamu::JsonApi::Context.new }
  let( :response ) { Shamu::JsonApi::Response.new context }

  it "uses presenter if given" do
    presenter = double
    expect( presenter ).to receive( :new ) do |resource, builder|
      instance = Shamu::JsonApi::Presenter.new resource, builder

      expect( instance ).to receive( :present ) do
        builder.identifier :response, 9
      end

      instance
    end

    response.resource double, presenter
  end

  it "expects a block if no presenter" do
    expect do
      response.resource double
    end.to raise_error Shamu::JsonApi::NoPresenter
  end

  it "appends included resources" do

    response.resource double do |builder|
      builder.identifier :example, 4
      builder.relationship :parent do |rel|
        rel.identifier :suite, 10
        rel.include_resource double do |res|
          res.identifier :suite, 10
        end
      end
    end

    expect( response.compile ).to include included: [ hash_including( type: "suite" ) ]
  end

  it "includes errors" do
    response.error NotImplementedError.new

    expect( response.compile ).to include errors: [ hash_including( code: "not_implemented" ) ]
  end

  it "writes validation errors" do
    klass = Class.new do
      include ActiveModel::Validations

      attr_reader :title
      validates :title, presence: true

      validate :general_problem

      def general_problem
        errors.add :base, "nope"
      end
    end

    allow( klass ).to receive( :name ).and_return "Example"
    record = klass.new
    record.valid?

    response.validation_errors record.errors
    expect( response.compile ).to include errors: include( hash_including( source: { pointer: "/data/attributes/title" } ) ) # rubocop:disable Metrics/LineLength
    expect( response.compile ).to include errors: include( hash_including( source: { pointer: "/data" } ) )
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
shamu-0.0.24 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.21 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.20 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.19 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.18 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.17 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.15 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.14 spec/lib/shamu/json_api/response_spec.rb
shamu-0.0.13 spec/lib/shamu/json_api/response_spec.rb