
require 'spec_helper'

describe SaleLineItem do

  let(:user) {FactoryGirl.create(:user)}
  let(:user2) {FactoryGirl.create(:user)}
  let(:jam_track) {FactoryGirl.create(:jam_track)}
  let(:gift_card) {FactoryGirl.create(:gift_card_type, card_type: GiftCardType::JAM_TRACKS_10)}

  describe "associations" do

    it "can find associated recurly transaction web hook" do
      sale = Sale.create_jam_track_sale(user)
      shopping_cart = ShoppingCart.create(user, jam_track)
      sale_line_item = SaleLineItem.create_from_shopping_cart(sale, shopping_cart, 'some_recurly_uuid', nil, nil)
      transaction = FactoryGirl.create(:recurly_transaction_web_hook, subscription_id: 'some_recurly_uuid')

      sale_line_item.reload
      sale_line_item.recurly_transactions.should eq([transaction])
    end
  end


  describe "state" do

    it "jam track success" do
      sale = Sale.create_jam_track_sale(user)
      shopping_cart = ShoppingCart.create(user, jam_track)
      sale_line_item = SaleLineItem.create_from_shopping_cart(sale, shopping_cart, 'some_recurly_uuid', nil, nil)
      transaction = FactoryGirl.create(:recurly_transaction_web_hook, subscription_id: 'some_recurly_uuid')

      sale_line_item.reload
      sale_line_item.state.should eq({
                                         void: false,
                                         refund: false,
                                         fail: false,
                                         success: true
                                     })
    end

    it "gift card success" do
      sale = Sale.create_jam_track_sale(user)
      shopping_cart = ShoppingCart.create(user, gift_card)
      sale_line_item = SaleLineItem.create_from_shopping_cart(sale, shopping_cart, 'some_recurly_uuid', nil, nil)
      transaction = FactoryGirl.create(:recurly_transaction_web_hook, subscription_id: 'some_recurly_uuid')

      sale_line_item.reload
      sale_line_item.state.should eq({
                                         void: false,
                                         refund: false,
                                         fail: false,
                                         success: true
                                     })
    end
  end
end
