Sha256: 267415e257a7f2686a91fb2fd9d58af530ba00b139de33f8ba02407499f8b064

Contents?: true

Size: 1.73 KB

Versions: 5

Compression:

Stored size: 1.73 KB

Contents

# frozen_string_literal: true

require 'spec_helper'

describe Azeroth::RequestHandler do
  describe '#process' do
    subject(:handler) do
      handler_class.new(controller, model, options)
    end

    let(:controller) { controller_class.new }
    let(:params)     { ActionController::Parameters.new(parameters) }
    let(:model)      { Azeroth::Model.new(:document, options) }
    let(:options)    { Azeroth::Options.new }

    let(:decorator)       { Document::Decorator.new(Document.all) }
    let(:expected_json)   { decorator.as_json }
    let(:documents_count) { 3 }

    let(:controller_class) { RequestHandlerController }

    let(:parameters) do
      { format: format }
    end

    let(:handler_class) do
      Class.new(described_class) do
        private

        def resource
          Document.all
        end
      end
    end

    before do
      create_list(:document, documents_count)

      allow(controller).to receive(:params)
        .and_return(params)

      allow(controller).to receive(:render)
        .with(json: expected_json, status: :ok)
        .and_return(expected_json)
    end

    context 'with format json' do
      let(:format) { 'json' }

      it 'returns json for resource' do
        expect(handler.process).to eq(expected_json)
      end

      it 'renders the json' do
        handler.process

        expect(controller).to have_received(:render)
      end
    end

    context 'with format html' do
      let(:format) { 'html' }

      it do
        handler.process

        expect(controller).not_to have_received(:render)
      end
    end

    context 'with format js' do
      let(:format) { 'js' }

      it do
        handler.process

        expect(controller).not_to have_received(:render)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
azeroth-1.0.0 spec/lib/azeroth/request_handler_spec.rb
azeroth-0.10.1 spec/lib/azeroth/request_handler_spec.rb
azeroth-0.10.0 spec/lib/azeroth/request_handler_spec.rb
azeroth-0.9.0 spec/lib/azeroth/request_handler_spec.rb
azeroth-0.8.2 spec/lib/azeroth/request_handler_spec.rb