Skip to content

Commit 8252473

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 <[email protected]> Co-authored-by: Benjamin <[email protected]> Co-authored-by: An <[email protected]> Co-authored-by: Jared <[email protected]>
1 parent 64770cf commit 8252473

File tree

1 file changed

+48
-17
lines changed

1 file changed

+48
-17
lines changed

legacy_promotions/spec/models/spree/promotion_integration_spec.rb

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
# Regression test for https://github.com/solidusio/solidus/pull/1591
2626
context "with unsaved line_item changes" do
27-
let(:calculator) { FactoryBot.create :flat_rate_calculator }
27+
let(:calculator) { FactoryBot.create(:flat_rate_calculator) }
2828
let(:line_item) { order.line_items.first }
2929

3030
before do
@@ -80,42 +80,71 @@
8080
)
8181
end
8282
end
83+
84+
context "with the in-memory order updater" do
85+
subject { order.recalculate(persist: false) }
86+
87+
before {
88+
calculator.update!(preferred_amount: 5)
89+
}
90+
91+
around do |example|
92+
default_order_recalculator = Spree::Config.order_recalculator_class.to_s
93+
94+
Spree::Config.order_recalculator_class = 'Spree::InMemoryOrderUpdater'
95+
96+
example.run
97+
98+
Spree::Config.order_recalculator_class = default_order_recalculator
99+
end
100+
101+
it "updates the adjustment total but does not persist it" do
102+
expect(order.adjustment_total).to eq(-10.0)
103+
104+
expect { subject }.
105+
to_not make_database_queries(manipulative: true)
106+
107+
expect(order).to have_attributes(
108+
total: 105,
109+
adjustment_total: -5
110+
)
111+
112+
order.reload
113+
114+
expect(order.adjustment_total).to eq(-10)
115+
end
116+
end
83117
end
84118
end
85119

86120
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-
}
121+
subject { order.recalculate }
93122

94-
let(:calculator) { Spree::Calculator::DistributedAmount.new }
123+
let(:calculator) { Spree::Calculator::DistributedAmount.new(preferred_amount: 15) }
95124
let(:promotion) {
96125
create :promotion,
97126
name: '15 spread'
98127
}
99-
let(:order) {
128+
129+
before {
130+
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:)
131+
}
132+
133+
let!(:order) {
100134
create :completed_order_with_promotion,
101135
promotion:,
102136
line_items_attributes: [{ price: 20 }, { price: 30 }, { price: 100 }]
103137
}
104138

105-
it 'correctly distributes the entire discount' do
139+
it 'correctly distributes the entire discount', :aggregate_failures do
106140
subject
107141

108142
expect(order.promo_total).to eq(-15)
109143
expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])
110144
end
111145

112146
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-
}
147+
subject { order.recalculate(persist: false) }
119148

120149
around do |example|
121150
default_order_recalculator = Spree::Config.order_recalculator_class.to_s
@@ -128,7 +157,9 @@
128157
end
129158

130159
it 'initializes the adjustments but does not persist them' do
131-
subject
160+
expect {
161+
subject
162+
}.not_to make_database_queries(manipulative: true)
132163

133164
expect(order.promo_total).to eq(-15)
134165
expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])

0 commit comments

Comments
 (0)