|
24 | 24 |
|
25 | 25 | # Regression test for https://github.com/solidusio/solidus/pull/1591
|
26 | 26 | context "with unsaved line_item changes" do
|
27 |
| - let(:calculator) { FactoryBot.create :flat_rate_calculator } |
| 27 | + let(:calculator) { FactoryBot.create(:flat_rate_calculator) } |
28 | 28 | let(:line_item) { order.line_items.first }
|
29 | 29 |
|
30 | 30 | before do
|
|
80 | 80 | )
|
81 | 81 | end
|
82 | 82 | 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 |
83 | 117 | end
|
84 | 118 | end
|
85 | 119 |
|
86 | 120 | 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 } |
93 | 122 |
|
94 |
| - let(:calculator) { Spree::Calculator::DistributedAmount.new } |
| 123 | + let(:calculator) { Spree::Calculator::DistributedAmount.new(preferred_amount: 15) } |
95 | 124 | let(:promotion) {
|
96 | 125 | create :promotion,
|
97 | 126 | name: '15 spread'
|
98 | 127 | }
|
99 |
| - let(:order) { |
| 128 | + |
| 129 | + before { |
| 130 | + Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator:, promotion:) |
| 131 | + } |
| 132 | + |
| 133 | + let!(:order) { |
100 | 134 | create :completed_order_with_promotion,
|
101 | 135 | promotion:,
|
102 | 136 | line_items_attributes: [{ price: 20 }, { price: 30 }, { price: 100 }]
|
103 | 137 | }
|
104 | 138 |
|
105 |
| - it 'correctly distributes the entire discount' do |
| 139 | + it 'correctly distributes the entire discount', :aggregate_failures do |
106 | 140 | subject
|
107 | 141 |
|
108 | 142 | expect(order.promo_total).to eq(-15)
|
109 | 143 | expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])
|
110 | 144 | end
|
111 | 145 |
|
112 | 146 | 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) } |
119 | 148 |
|
120 | 149 | around do |example|
|
121 | 150 | default_order_recalculator = Spree::Config.order_recalculator_class.to_s
|
|
128 | 157 | end
|
129 | 158 |
|
130 | 159 | it 'initializes the adjustments but does not persist them' do
|
131 |
| - subject |
| 160 | + expect { |
| 161 | + subject |
| 162 | + }.not_to make_database_queries(manipulative: true) |
132 | 163 |
|
133 | 164 | expect(order.promo_total).to eq(-15)
|
134 | 165 | expect(order.line_items.map(&:adjustment_total)).to eq([-2, -3, -10])
|
|
0 commit comments