require 'spec_helper'

describe Spree::Order do
  let(:subscription_product) { create :subscription_product }
  let(:variant) { create :variant, product: subscription_product }
  let(:line_item) { create :line_item, variant: variant }
  let(:subject) { line_item.order }

  before do
    create :product

    subject.update_totals
  end

  describe '#check_subscriptions!' do
    context 'creates a subscription' do
      let!(:completed_payment){ create :payment, order: subject, state: 'completed' }

      it 'adds a subscription' do
        expect { subject.check_subscriptions! }.to change {
          Spree::Subscription.count
        }.by(1)
      end
    end

    context 'updated a subscription' do
      let!(:subscription) do
        create(:subscription,
               subscription_product: subscription_product,
               user: subject.user
              )
      end

      it 'updates subscription limit' do
        expect { subject.check_subscriptions! }.to change {
          subscription.reload
          subscription.limit
        }.by(12)
      end
    end
  end
end
