-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 ability to remove promotions via Promotion#remove_from #1451
Changes from all commits
f74c9a0
0be285f
da8efd6
4ad15d5
8f8a2f0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,11 @@ module Spree | |
class Promotion | ||
module Actions | ||
describe CreateItemAdjustments, type: :model do | ||
let(:order) { create(:order) } | ||
let(:order) { create(:order_with_line_items, line_items_count: 1) } | ||
let(:promotion) { create(:promotion, :with_line_item_adjustment, adjustment_rate: adjustment_amount) } | ||
let(:adjustment_amount) { 10 } | ||
let(:action) { promotion.actions.first! } | ||
let!(:line_item) { create(:line_item, order: order) } | ||
let(:line_item) { order.line_items.to_a.first } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gmacdougall this ensures that Our current implementation of the order factory happens to preload There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm also prefer keeping the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sounds good |
||
let(:payload) { { order: order, promotion: promotion } } | ||
|
||
before do | ||
|
@@ -122,32 +122,56 @@ module Actions | |
end | ||
end | ||
|
||
describe '#remove_from' do | ||
# this adjustment should not get removed | ||
let!(:other_adjustment) { create(:adjustment, adjustable: line_item, order: order, source: nil) } | ||
|
||
before do | ||
action.perform(payload) | ||
@action_adjustment = line_item.adjustments.where(source: action).first! | ||
end | ||
|
||
it 'removes the action adjustment' do | ||
expect(line_item.adjustments).to match_array([other_adjustment, @action_adjustment]) | ||
|
||
action.remove_from(order) | ||
|
||
expect(line_item.adjustments).to eq([other_adjustment]) | ||
end | ||
end | ||
|
||
context "#destroy" do | ||
let!(:action) { promotion.actions.first } | ||
let(:other_action) { other_promotion.actions.first } | ||
let(:promotion) { create(:promotion, :with_line_item_adjustment) } | ||
let(:other_promotion) { create(:promotion, :with_line_item_adjustment) } | ||
|
||
it "destroys adjustments for incompleted orders" do | ||
order = Order.create | ||
action.adjustments.create!(label: "Check", amount: 0, order: order, adjustable: order) | ||
context 'with incomplete orders' do | ||
let(:order) { create(:order) } | ||
|
||
expect { | ||
action.destroy | ||
}.to change { Adjustment.count }.by(-1) | ||
it 'destroys adjustments' do | ||
order.adjustments.create!(label: 'Check', amount: 0, order: order, source: action) | ||
|
||
expect { | ||
action.destroy | ||
}.to change { Adjustment.count }.by(-1) | ||
end | ||
end | ||
|
||
it "nullifies adjustments for completed orders" do | ||
order = Order.create(completed_at: Time.current) | ||
adjustment = action.adjustments.create!(label: "Check", amount: 0, order: order, adjustable: order) | ||
context 'with complete orders' do | ||
let(:order) { create(:completed_order_with_totals) } | ||
|
||
expect { | ||
action.destroy | ||
}.to change { adjustment.reload.source_id }.from(action.id).to nil | ||
it 'nullifies adjustments for completed orders' do | ||
adjustment = order.adjustments.create!(label: 'Check', amount: 0, order: order, source: action) | ||
|
||
expect { | ||
action.destroy | ||
}.to change { adjustment.reload.source_id }.from(action.id).to nil | ||
end | ||
end | ||
|
||
it "doesnt mess with unrelated adjustments" do | ||
other_action.adjustments.create!(label: "Check", amount: 0, order: order, adjustable: order) | ||
order.adjustments.create!(label: "Check", amount: 0, order: order, source: action) | ||
|
||
expect { | ||
action.destroy | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'spec_helper' | ||
|
||
describe Spree::PromotionAction, type: :model do | ||
describe '#remove_from' do | ||
class MyPromotionAction < Spree::PromotionAction | ||
def perform(options = {}) | ||
order = options[:order] | ||
order.adjustments.create!(amount: 1, order: order, source: self, label: 'foo') | ||
true | ||
end | ||
end | ||
|
||
let(:action) { promotion.actions.first! } | ||
let!(:promotion) { create(:promotion, promotion_actions: [MyPromotionAction.new]) } | ||
let(:order) { create(:order) } | ||
|
||
# this adjustment should not get removed | ||
let!(:other_adjustment) { create(:adjustment, order: order, source: nil) } | ||
|
||
before do | ||
action.perform(order: order) | ||
@action_adjustment = order.adjustments.where(source: action).first! | ||
end | ||
|
||
it 'removes the action adjustment' do | ||
expect(order.adjustments).to match_array([other_adjustment, @action_adjustment]) | ||
|
||
expect(Spree::Deprecation).to( | ||
receive(:warn). | ||
with(/"MyPromotionAction" does not define #remove_from/, anything) | ||
) | ||
|
||
action.remove_from(order) | ||
|
||
expect(order.adjustments).to eq([other_adjustment]) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated. Thanks.