Sha256: 8f7d138f76631bc6fd6189404d94eeea765f69548a646564178e8a7938b7f82a

Contents?: true

Size: 1.62 KB

Versions: 11

Compression:

Stored size: 1.62 KB

Contents

require 'rails_helper'

describe LHS::Item do

  before(:each) do
    class SomeService < LHS::Service
      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, SomeService)
  end

  let(:item) do
    data[0]
  end

  context 'update' do

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

    it 'returns false if persisting went wrong' do
      stub_request(:post, item.href).to_return(status: 500)
      result = item.update(name: 'Steve')
      expect(result).to eq false
    end

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

    it 'updates local version of an object even if BE request fails' do
      stub_request(:post, item.href)
        .to_return(status: 400, body: item._raw.merge(likes: 'Banana').to_json)
      item.update(name: 'Andrea')
      expect(item.name).to eq 'Andrea'
      expect(item.likes).to_not eq 'Banana'
    end
  end

  context 'update!' do

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
lhs-2.2.2 spec/item/update_spec.rb
lhs-2.2.1 spec/item/update_spec.rb
lhs-2.2.0 spec/item/update_spec.rb
lhs-2.1.1 spec/item/update_spec.rb
lhs-2.1.0 spec/item/update_spec.rb
lhs-2.0.5 spec/item/update_spec.rb
lhs-2.0.4 spec/item/update_spec.rb
lhs-2.0.3 spec/item/update_spec.rb
lhs-2.0.2 spec/item/update_spec.rb
lhs-2.0.1 spec/item/update_spec.rb
lhs-2.0.0 spec/item/update_spec.rb