-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move promotion-related order updater specs
- Loading branch information
Showing
2 changed files
with
74 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
module Spree | ||
RSpec.describe OrderUpdater, type: :model do | ||
let!(:store) { create :store } | ||
let(:order) { Spree::Order.create } | ||
let(:updater) { Spree::OrderUpdater.new(order) } | ||
|
||
context "order totals" do | ||
before do | ||
2.times do | ||
create(:line_item, order: order, price: 10) | ||
end | ||
end | ||
|
||
context 'with order promotion followed by line item addition' do | ||
let(:promotion) { Spree::Promotion.create!(name: "10% off") } | ||
let(:calculator) { Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10) } | ||
|
||
let(:promotion_action) do | ||
Promotion::Actions::CreateAdjustment.create!({ | ||
calculator: calculator, | ||
promotion: promotion | ||
}) | ||
end | ||
|
||
before do | ||
updater.recalculate | ||
create(:adjustment, source: promotion_action, adjustable: order, order: order) | ||
create(:line_item, order: order, price: 10) # in addition to the two already created | ||
order.line_items.reload # need to pick up the extra line item | ||
updater.recalculate | ||
end | ||
|
||
it "updates promotion total" do | ||
expect(order.promo_total).to eq(-3) | ||
end | ||
end | ||
|
||
it "update order adjustments" do | ||
create(:adjustment, adjustable: order, order: order, source: nil, amount: 10) | ||
|
||
expect { | ||
updater.recalculate | ||
}.to change { | ||
order.adjustment_total | ||
}.from(0).to(10) | ||
end | ||
end | ||
|
||
describe 'updating in-memory items' do | ||
let(:order) do | ||
create(:order_with_line_items, line_items_count: 1, line_items_price: 10) | ||
end | ||
let(:line_item) { order.line_items.first } | ||
let(:promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: 1) } | ||
|
||
it 'updates in-memory items' do | ||
promotion.activate(order: order) | ||
|
||
expect(line_item.promo_total).to eq(0) | ||
expect(order.promo_total).to eq(0) | ||
|
||
order.recalculate | ||
|
||
expect(line_item.promo_total).to eq(-1) | ||
expect(order.promo_total).to eq(-1) | ||
end | ||
end | ||
end | ||
end |