Skip to content

Commit

Permalink
Move promotion-related order updater specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed May 16, 2024
1 parent fb4f10a commit 636e27b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 45 deletions.
46 changes: 1 addition & 45 deletions core/spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,6 @@ module Spree
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)

Expand All @@ -86,7 +62,7 @@ module Spree
describe 'promotion recalculation' do
it "calls the Promotion Adjustments Recalculator" do
adjuster = double(:call)
expect(Spree::Promotion::OrderAdjustmentsRecalculator).to receive(:new).and_return(adjuster)
expect(Spree::Config.promotion_adjuster_class).to receive(:new).and_return(adjuster)
expect(adjuster).to receive(:call)
order.recalculate
end
Expand Down Expand Up @@ -358,26 +334,6 @@ module Spree
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

context "with item with no adjustment and incorrect totals" do
let!(:line_item) { create(:line_item, order: order, price: 10) }

Expand Down
73 changes: 73 additions & 0 deletions legacy_promotions/spec/models/spree/order_updater_spec.rb
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

0 comments on commit 636e27b

Please sign in to comment.