Skip to content

Commit

Permalink
Fix Spree::Promotion#usage_count specs or new discount system
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed Mar 22, 2022
1 parent b0ae6aa commit a3ed286
Showing 1 changed file with 76 additions and 25 deletions.
101 changes: 76 additions & 25 deletions core/spec/models/spree/promotion_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,39 +357,90 @@
end

describe "#usage_count" do
let(:promotion) do
FactoryBot.create(
:promotion,
:with_order_adjustment,
code: "discount"
)
end
context "with legacy promotion system" do
let(:promotion) do
FactoryBot.create(
:promotion,
:with_order_adjustment,
code: "discount"
)
end

subject { promotion.usage_count }
subject { promotion.usage_count }

context "when the code is applied to a non-complete order" do
let(:order) { FactoryBot.create(:order_with_line_items) }
before { promotion.activate(order: order, promotion_code: promotion.codes.first) }
it { is_expected.to eq 0 }
context "when the code is applied to a non-complete order" do
let(:order) { FactoryBot.create(:order_with_line_items) }
before { promotion.activate(order: order, promotion_code: promotion.codes.first) }
it { is_expected.to eq 0 }
end
context "when the code is applied to a complete order" do
let!(:order) do
FactoryBot.create(
:completed_order_with_promotion,
promotion: promotion
)
end
context "and the promo is eligible" do
it { is_expected.to eq 1 }
end
context "and the promo is ineligible" do
before { order.adjustments.promotion.update_all(eligible: false) }
it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end
context "when the code is applied to a complete order" do
let!(:order) do

context "with discount system" do
around do |example|
with_unfrozen_spree_preference_store do
Spree::Config.promotion_system = :discounts
example.run
Spree::Config.promotion_system = :adjustments
end
end

let(:promotion) do
FactoryBot.create(
:completed_order_with_promotion,
promotion: promotion
:promotion,
:with_line_item_adjustment,
)
end
context "and the promo is eligible" do
it { is_expected.to eq 1 }
end
context "and the promo is ineligible" do
before { order.adjustments.promotion.update_all(eligible: false) }

subject { promotion.usage_count }

context "when the code is applied to a non-complete order" do
let(:order) { FactoryBot.create(:order_with_line_items) }
before do
order.order_promotions << Spree::OrderPromotion.new(promotion_code: promotion.codes.first, promotion: promotion)
order.recalculate
end

it { is_expected.to eq 0 }
end
context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }

context "when the code is applied to a complete order" do
let!(:order) { FactoryBot.create(:order_ready_to_complete) }

before do
order.order_promotions << Spree::OrderPromotion.new(promotion_code: promotion.codes.first, promotion: promotion)
order.recalculate
order.complete!
end

context "and the promo is eligible" do
it { is_expected.to eq 1 }
end

context "and the order is canceled" do
before { order.cancel! }
it { is_expected.to eq 0 }
it { expect(order.state).to eq 'canceled' }
end
end
end
end
Expand Down

0 comments on commit a3ed286

Please sign in to comment.