From 90fdf594156a6adf39fba8de344290edb2a60e90 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 12 Jul 2024 09:39:19 +1000 Subject: [PATCH] Test current stock logic on shipment level During checkout, stock is adjusted when a shipment is finalised. The chain is: * Order state change to complete. * Trigger Order#finalize! which updates shipments. * Trigger Shipment#finalize! which adjusts stock on the variant. * A variant holds stock in stock items or in a variant override. --- spec/models/spree/shipment_spec.rb | 31 ++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/spec/models/spree/shipment_spec.rb b/spec/models/spree/shipment_spec.rb index a559d8b855..ecaba70205 100644 --- a/spec/models/spree/shipment_spec.rb +++ b/spec/models/spree/shipment_spec.rb @@ -264,6 +264,37 @@ RSpec.describe Spree::Shipment do end end + describe "#finalize!" do + subject(:shipment) { order.shipments.first } + let(:variant) { order.variants.first } + let(:order) { create(:order_ready_for_confirmation) } + + it "reduces stock" do + variant.on_hand = 5 + + expect { shipment.finalize! } + .to change { variant.on_hand }.from(5).to(4) + end + + it "reduces stock of a variant override" do + variant.on_hand = 5 + variant_override = VariantOverride.create!( + variant:, + hub: order.distributor, + count_on_hand: 7, + on_demand: false, + ) + + expect { + shipment.finalize! + variant.reload + variant_override.reload + } + .to change { variant_override.count_on_hand }.from(7).to(6) + .and change { variant.on_hand }.by(0) + end + end + context "when order is completed" do before do allow(order).to receive_messages completed?: true