Skip to content

Commit f4ad7e1

Browse files
sofiabesenski4forkatabenjaminwilstewartjarednorman
committed
Test InMemoryOrder updater in legacy promotions
Integration level test using the InMemoryOrderUpdater to ensure we are not persisting changes during the promotion recalculations. Co-authored-by: Chris <chris@super.gd> Co-authored-by: Benjamin <benjamin@super.gd> Co-authored-by: An <andrew@super.gd> Co-authored-by: Jared <jared@super.gd>
1 parent d3a6e95 commit f4ad7e1

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

legacy_promotions/spec/models/spree/promotion/order_adjustments_recalculator_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# frozen_string_literal: true
22

33
require 'rails_helper'
4-
require 'db-query-matchers'
54

65
RSpec.describe Spree::Promotion::OrderAdjustmentsRecalculator do
76
subject { described_class.new(order).call(persist:) }

legacy_promotions/spec/models/spree/promotion_integration_spec.rb

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484
end
8585

8686
describe "distributing amount across line items" do
87+
subject {
88+
calculator.preferred_amount = 15
89+
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:)
90+
91+
order.recalculate
92+
}
93+
8794
let(:calculator) { Spree::Calculator::DistributedAmount.new }
8895
let(:promotion) {
8996
create :promotion,
@@ -95,21 +102,52 @@
95102
line_items_attributes: [{ price: 20 }, { price: 30 }, { price: 100 }]
96103
}
97104

98-
before do
99-
calculator.preferred_amount = 15
100-
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:)
101-
order.recalculate
102-
end
103-
104105
it 'correctly distributes the entire discount' do
106+
subject
107+
105108
expect(order.promo_total).to eq(-15)
106109
expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])
107110
end
108111

112+
context 'with the in memory order updater' do
113+
subject {
114+
calculator.preferred_amount = 15
115+
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:)
116+
117+
order.recalculate(persist: false)
118+
}
119+
120+
around do |example|
121+
default_order_recalculator = Spree::Config.order_recalculator_class.to_s
122+
123+
Spree::Config.order_recalculator_class = 'Spree::InMemoryOrderUpdater'
124+
125+
example.run
126+
127+
Spree::Config.order_recalculator_class = default_order_recalculator
128+
end
129+
130+
it 'initializes the adjustments but does not persist them' do
131+
subject
132+
133+
expect(order.promo_total).to eq(-15)
134+
expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])
135+
136+
order.reload
137+
138+
expect(order.promo_total).to eq(0)
139+
expect(order.line_items.map(&:adjustment_total)).to eq([0, 0, 0])
140+
end
141+
end
142+
109143
context 'with product promotion rule' do
144+
subject { order.recalculate }
145+
110146
let(:first_product) { order.line_items.first.product }
111147

112148
before do
149+
calculator.preferred_amount = 15
150+
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:)
113151
rule = Spree::Promotion::Rules::Product.create!(
114152
promotion:,
115153
product_promotion_rules: [
@@ -118,10 +156,11 @@
118156
)
119157
promotion.rules << rule
120158
promotion.save!
121-
order.recalculate
122159
end
123160

124161
it 'still distributes the entire discount' do
162+
subject
163+
125164
expect(order.promo_total).to eq(-15)
126165
expect(order.line_items.map(&:adjustment_total)).to eq([-15, 0, 0])
127166
end

legacy_promotions/spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
require 'rspec/rails'
2828
require 'rspec-activemodel-mocks'
2929
require 'database_cleaner'
30+
require 'db-query-matchers'
3031

3132
Dir["./spec/support/**/*.rb"].sort.each { |f| require f }
3233

0 commit comments

Comments
 (0)