From 62907696a5634591ff524d99abc9397154cab35e Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Sat, 19 Mar 2022 12:13:19 +0100 Subject: [PATCH] Fix Spree::Promotion#usage_count specs or new discount system --- core/spec/models/spree/promotion_spec.rb | 101 +++++++++++++++++------ 1 file changed, 76 insertions(+), 25 deletions(-) diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb index a1d1ce20d64..1580681cebf 100644 --- a/core/spec/models/spree/promotion_spec.rb +++ b/core/spec/models/spree/promotion_spec.rb @@ -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