Sha256: d6168dbce539b87e9f4e45b76e9ea565ec957e222397bf112b17e61ada5132de

Contents?: true

Size: 1.97 KB

Versions: 12

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

describe DHS::Record do
  before do
    class Location < DHS::Record
      configuration item_key: %i[response location]
      endpoint 'http://uberall/location'
      endpoint 'http://uberall/location/{id}'
    end
  end

  let(:location_response) do
    {
      response: {
        location: {
          id: 1
        }
      }
    }.to_json
  end

  let(:stub_request_by_id) do
    stub_request(:get, 'http://uberall/location/1')
      .to_return(body: location_response)
  end

  let(:stub_request_by_get_parameters) do
    stub_request(:get, 'http://uberall/location?identifier=1&limit=1')
      .to_return(body: location_response)
  end

  it 'uses configured item_key to unwrap response data for find' do
    stub_request_by_id
    location = Location.find(1)
    expect(location.id).to eq 1
  end

  it 'uses configured item_key to unwrap response data for find_by' do
    stub_request_by_get_parameters
    location = Location.find_by(identifier: 1)
    expect(location.id).to eq 1
  end

  describe 'Holding on to request object' do
    let(:account_response) do
      { id: 1 }.to_json
    end

    let(:stub_location_by_id) do
      stub_request(:get, 'http://uberall/location/1')
        .to_return(headers: { 'X-Custom-Header' => 'rspec' }, body: location_response)
    end

    let(:stub_account_by_id) do
      stub_request(:get, 'http://yext/account/1')
        .to_return(headers: { 'X-Custom-Header' => 'rspec' }, body: account_response)
    end

    before do
      class Account < DHS::Record
        endpoint 'http://yext/account'
        endpoint 'http://yext/account/{id}'
      end

      stub_location_by_id
      stub_account_by_id
    end

    it 'preserves request object for nested items' do
      location = Location.find(1)
      expect(location._request).to be_present
    end

    it 'preserves request object for unnested items' do
      account = Account.find(1)
      expect(account._request).to be_present
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dhs-1.6.0 spec/record/item_key_spec.rb
dhs-1.5.0 spec/record/item_key_spec.rb
dhs-1.4.2 spec/record/item_key_spec.rb
dhs-1.4.1 spec/record/item_key_spec.rb
dhs-1.4.0 spec/record/item_key_spec.rb
dhs-1.3.0 spec/record/item_key_spec.rb
dhs-1.2.0 spec/record/item_key_spec.rb
dhs-1.1.0 spec/record/item_key_spec.rb
dhs-1.0.3 spec/record/item_key_spec.rb
dhs-1.0.2 spec/record/item_key_spec.rb
dhs-1.0.1 spec/record/item_key_spec.rb
dhs-1.0.0 spec/record/item_key_spec.rb