Sha256: 7b9b695dc10c7898358689d615d2ee2f36965d5f9094ee4fcf24891da2f98d7a

Contents?: true

Size: 1.62 KB

Versions: 13

Compression:

Stored size: 1.62 KB

Contents

require 'rails_helper'

describe LHS::Item do
  before(:each) do
    class Record < LHS::Record
      endpoint ':datastore/v2/:campaign_id/feedbacks'
      endpoint ':datastore/v2/feedbacks'
    end
  end

  let(:json) do
    load_json(:feedbacks)
  end

  let(:data) do
    LHS::Data.new(json, nil, Record)
  end

  let(:item) do
    data[0]
  end

  context 'save' do
    it 'persists changes on the backend' do
      stub_request(:post, item.href).with(body: item._raw.merge(name: 'Steve').to_json)
      item.name = 'Steve'
      expect(item.save).to eq true
    end

    it 'returns false if persting goes wrong' do
      stub_request(:post, item.href)
        .with(body: item._raw.to_json)
        .to_return(status: 500)
      expect(item.save).to eq false
    end

    it 'merges reponse data with object' do
      stub_request(:post, item.href)
        .with(body: item._raw.to_json)
        .to_return(status: 200, body: item._raw.merge(name: 'Steve').to_json)
      item.save
      expect(item.name).to eq 'Steve'
    end
  end

  context 'save!' do
    it 'raises if something goes wrong' do
      stub_request(:post, item.href)
        .with(body: item._raw.to_json)
        .to_return(status: 500)
      expect(-> { item.save! }).to raise_error LHC::ServerError
    end

    it 'keeps header psassed in the options' do
      headers = { 'Stats' => 'first-access' }
      request = stub_request(:post, item.href)
        .with(
          body: item._raw.to_json,
          headers: headers)
        .to_return(status: 200, body: item._raw.to_json)

      item.save!(headers: headers)
      expect(request).to have_been_requested
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
lhs-6.4.0 spec/item/save_spec.rb
lhs-6.3.1 spec/item/save_spec.rb
lhs-6.3.0 spec/item/save_spec.rb
lhs-6.2.0 spec/item/save_spec.rb
lhs-6.1.0 spec/item/save_spec.rb
lhs-6.0.0 spec/item/save_spec.rb
lhs-5.7.1 spec/item/save_spec.rb
lhs-5.7.0 spec/item/save_spec.rb
lhs-5.6.6 spec/item/save_spec.rb
lhs-5.6.5 spec/item/save_spec.rb
lhs-5.6.4 spec/item/save_spec.rb
lhs-5.6.3 spec/item/save_spec.rb
lhs-5.6.2 spec/item/save_spec.rb