Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flexible with_adjustable_action trait to Promotion factory #4682

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
RyanofWoods marked this conversation as resolved.
Show resolved Hide resolved
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 }
RyanofWoods marked this conversation as resolved.
Show resolved Hide resolved
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