Move #recreate_all_fees! spec to HandleFeesService

Spree::Order just delegate Orders::HandleFeesService so there is no
point testing fees in the order spec
This commit is contained in:
Gaetan Craig-Riou
2025-02-12 15:31:10 +11:00
parent b5bc6b84d7
commit 223faa5569
3 changed files with 45 additions and 58 deletions

View File

@@ -18,6 +18,9 @@ module Orders
order.with_lock do
EnterpriseFee.clear_order_adjustments order
# To prevent issue with fee being removed when a product is not linked to the order cycle
# anymore, we now create or update line item fees.
# Previously fees were deleted and recreated, like we still do for order fees.
create_or_update_line_item_fees!
create_order_fees!
end

View File

@@ -614,64 +614,6 @@ RSpec.describe Spree::Order do
end
end
describe "applying enterprise fees" do
subject { create(:order) }
let(:fee_handler) { Orders::HandleFeesService.new(subject) }
before do
allow(subject).to receive(:fee_handler) { fee_handler }
allow(subject).to receive(:update_order!)
end
it "clears all enterprise fee adjustments on the order" do
expect(EnterpriseFee).to receive(:clear_all_adjustments).with(subject)
subject.recreate_all_fees!
end
it "creates line item and order fee adjustments via Orders::HandleFeesService" do
expect(fee_handler).to receive(:create_line_item_fees!)
expect(fee_handler).to receive(:create_order_fees!)
subject.recreate_all_fees!
end
it "skips order cycle per-order adjustments for orders that don't have an order cycle" do
allow(EnterpriseFee).to receive(:clear_all_adjustments)
allow(subject).to receive(:order_cycle) { nil }
subject.recreate_all_fees!
end
it "ensures the correct adjustment(s) are created for order cycles" do
allow(EnterpriseFee).to receive(:clear_all_adjustments)
line_item = create(:line_item, order: subject)
allow(fee_handler).to receive(:provided_by_order_cycle?) { true }
order_cycle = double(:order_cycle)
expect_any_instance_of(OpenFoodNetwork::EnterpriseFeeCalculator).
to receive(:create_line_item_adjustments_for).
with(line_item)
allow_any_instance_of(OpenFoodNetwork::EnterpriseFeeCalculator)
.to receive(:create_order_adjustments_for)
allow(subject).to receive(:order_cycle) { order_cycle }
subject.recreate_all_fees!
end
it "ensures the correct per-order adjustment(s) are created for order cycles" do
allow(EnterpriseFee).to receive(:clear_all_adjustments)
order_cycle = double(:order_cycle)
expect_any_instance_of(OpenFoodNetwork::EnterpriseFeeCalculator).
to receive(:create_order_adjustments_for).
with(subject)
allow(subject).to receive(:order_cycle) { order_cycle }
subject.recreate_all_fees!
end
end
describe "getting the admin and handling charge" do
let(:o) { create(:order) }
let(:li) { create(:line_item, order: o) }

View File

@@ -19,6 +19,48 @@ RSpec.describe Orders::HandleFeesService do
allow(service).to receive(:calculator) { calculator }
end
describe "#recreate_all_fees!" do
before do
allow(order).to receive(:update_order!)
end
it "clears order enterprise fee adjustments on the order" do
expect(EnterpriseFee).to receive(:clear_order_adjustments).with(order)
service.recreate_all_fees!
end
# both create_or_update_line_item_fees! and create_order_fees! are tested below,
# so it's enough to check they get called
it "creates line item and order fee adjustments" do
expect(service).to receive(:create_or_update_line_item_fees!)
expect(service).to receive(:create_order_fees!)
service.recreate_all_fees!
end
it "updates the order" do
expect(order).to receive(:update_order!)
service.recreate_all_fees!
end
it "doesn't create tax adjustment" do
expect(service).not_to receive(:tax_enterprise_fees!)
service.recreate_all_fees!
end
context "when after payment state" do
it "creates the tax adjustment for the fees" do
expect(service).to receive(:tax_enterprise_fees!)
order.update(state: "confirmation")
service.recreate_all_fees!
end
end
end
describe "#create_or_update_line_item_fees!" do
context "with no existing fee" do
it "creates per line item fee adjustments for line items in the order cylce" do