Skip to content

Commit

Permalink
Merge pull request #4682 from RyanofWoods/ryanofwoods/make-promotion-…
Browse files Browse the repository at this point in the history
…factory-with-line-item-adjustment-trait-more-flexible

Make Promotion factory adjustment trait more flexible
  • Loading branch information
waiting-for-dev authored Oct 24, 2022
2 parents 052234c + 0b81b52 commit 55245ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
42 changes: 28 additions & 14 deletions core/lib/spree/testing_support/factories/promotion_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,40 @@
end

trait :with_action do
after(:create) do |promotion, _evaluator|
promotion.actions << Spree::Promotion::Actions::CreateAdjustment.new
transient do
promotion_action_class { Spree::Promotion::Actions::CreateAdjustment }
end

after(:create) do |promotion, evaluator|
promotion.actions << evaluator.promotion_action_class.new
end
end

trait :with_line_item_adjustment do
trait :with_adjustable_action do
transient do
adjustment_rate { 10 }
preferred_amount { 10 }
calculator_class { Spree::Calculator::FlatRate }
promotion_action_class { Spree::Promotion::Actions::CreateItemAdjustments }
end

after(:create) do |promotion, evaluator|
calculator = Spree::Calculator::FlatRate.new
calculator.preferred_amount = evaluator.adjustment_rate
Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion)
calculator = evaluator.calculator_class.new
calculator.preferred_amount = evaluator.preferred_amount
evaluator.promotion_action_class.create!(calculator: calculator, promotion: promotion)
end
end

factory :promotion_with_action_adjustment, traits: [:with_adjustable_action]

trait :with_line_item_adjustment do
transient do
adjustment_rate { 10 }
end

with_adjustable_action
preferred_amount { adjustment_rate }
end

factory :promotion_with_item_adjustment, traits: [:with_line_item_adjustment]

trait :with_free_shipping do
Expand All @@ -52,14 +69,11 @@
weighted_order_adjustment_amount { 10 }
end

after(:create) do |promotion, evaluator|
calculator = Spree::Calculator::FlatRate.new
calculator.preferred_amount = evaluator.weighted_order_adjustment_amount
action = Spree::Promotion::Actions::CreateAdjustment.create!(calculator: calculator)
promotion.actions << action
promotion.save!
end
with_adjustable_action
preferred_amount { weighted_order_adjustment_amount }
promotion_action_class { Spree::Promotion::Actions::CreateAdjustment }
end

factory :promotion_with_order_adjustment, traits: [:with_order_adjustment]

trait :with_item_total_rule do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
it_behaves_like 'a working factory'
end

describe 'promotion with action adjustment' do
let(:factory) { :promotion_with_action_adjustment }

it_behaves_like 'a working factory'
end

describe 'promotion with item adjustment' do
let(:factory) { :promotion_with_item_adjustment }

Expand Down

0 comments on commit 55245ea

Please sign in to comment.