# frozen_string_literal: true

require 'spec_helper'

describe 'present using status' do
  subject do
    Class.new(Grape::API)
  end

  def app
    subject
  end

  let(:entity_mock) do
    entity_mock = Object.new
    allow(entity_mock).to receive(:represent).and_return('dummy')
    entity_mock
  end

  it '使用 status 声明的实体渲染' do
    subject.status 200, entity_mock
    subject.get do
      present 'any'
    end

    get '/'
    expect(last_response.body).to eq('dummy')
  end

  it '状态码未定义时时抛出错误' do
    subject.status 200, entity_mock
    subject.get do
      status 201
      present 'any'
    end

    expect { get '/' }.to raise_error('状态码 201 下未发现实体定义')
  end

  it '使用 key 对应的实体渲染' do
    the_entity_mock = entity_mock
    subject.status 200 do
      expose :foo, using: the_entity_mock
    end

    subject.get do
      present :foo, 'any'
    end

    get '/'
    expect(last_response.body).to eq '{:foo=>"dummy"}'
  end

  it 'key 声明时未定义 `using` 亦应该正常' do
    subject.status 200 do
      expose :foo
    end
    subject.get do
      present :foo, 'foo'
    end

    expect { get '/' }.not_to raise_error
  end

  it '未声明 status 宏时恢复为原来的行为' do
    subject.get do
      present 'any'
    end

    get '/'
    expect(last_response.body).to eq('any')
  end
end
