Skip to content

Commit cc524c6

Browse files
committed
Rework and reuse inventory units shared example
The existing scenario is reworked in order to expose an issue when moving a few items of a variant (not all of them) to a shipment with same stock location as the original one.
1 parent fa4635f commit cc524c6

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

core/spec/models/spree/fulfilment_changer_spec.rb

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,63 @@
8282
end
8383

8484
shared_examples_for "properly manages inventory units" do
85-
context "when the stock location is empty" do
86-
let(:stock_item) { variant.stock_items.find_by!(stock_location: current_shipment.stock_location) }
85+
let(:stock_item) { variant.stock_items.find_by!(stock_location: current_shipment.stock_location) }
8786

88-
before { stock_item.update_column(:count_on_hand, 0) }
87+
context "when there are backordered inventory units" do
88+
let(:backordered_units_count) { 1 }
8989

90-
context "when there are backordered inventory units" do
90+
before do
91+
current_shipment.inventory_units.limit(backordered_units_count).update!(state: :backordered)
92+
end
93+
94+
context "when the stock is zero or negative" do
9195
before do
92-
current_shipment.inventory_units.first.update(state: :backordered)
96+
stock_item.update_column(:count_on_hand, -backordered_units_count)
9397
end
9498

95-
it "doesn't change the order inventory units state" do
96-
expect { subject }.not_to change { order.inventory_units.map(&:state).sort }
99+
it "doesn't change inventory units state" do
100+
expect { subject }
101+
.not_to change { order.inventory_units.map(&:state).sort}
102+
.from(%w[backordered on_hand])
97103
end
98104
end
99105

100-
context "when all inventory units are on hand" do
106+
context "when backordered items can become on hand" do
107+
before do
108+
stock_item.update_column(:count_on_hand, backordered_units_count)
109+
end
110+
111+
it "makes all inventory units on hand" do
112+
expect { subject }
113+
.to change { order.inventory_units.map(&:state).sort }
114+
.from(%w[backordered on_hand ]).to(%w[on_hand on_hand])
115+
end
116+
end
117+
end
118+
119+
context "when all inventory units are on hand" do
120+
before do
121+
current_shipment.inventory_units.update_all(state: :on_hand)
122+
end
123+
124+
context "when the stock is negative" do
101125
before do
102-
current_shipment.inventory_units.update_all(state: :on_hand)
126+
stock_item.update_column(:count_on_hand, -1)
103127
end
104128

105129
it "doesn't change the order inventory units state" do
106130
expect { subject }.not_to change { order.inventory_units.map(&:state).sort }
107131
end
108132
end
109133
end
134+
135+
context "when the stock location is empty" do
136+
before { stock_item.update_column(:count_on_hand, 0) }
137+
138+
it "doesn't change the order inventory units state" do
139+
expect { subject }.not_to change { order.inventory_units.map(&:state).sort }
140+
end
141+
end
110142
end
111143

112144
subject { shipment_splitter.run! }
@@ -115,10 +147,11 @@
115147
variant.stock_items.first.update_column(:count_on_hand, 100)
116148
end
117149

118-
context "when the current shipment stock location is the same of the target shipment" do
119-
let(:current_shipment_inventory_unit_count) { 1 }
120-
let(:quantity) { current_shipment_inventory_unit_count }
150+
context "when tracking inventory (default behavior)" do
151+
let(:current_shipment_inventory_unit_count) { 2 }
152+
let(:quantity) { 1 }
121153

154+
it_behaves_like "moves inventory units between shipments"
122155
it_behaves_like "properly manages inventory units"
123156
end
124157

@@ -366,6 +399,7 @@
366399
let(:desired_shipment) { order.shipments.build(stock_location: current_shipment.stock_location) }
367400

368401
it_behaves_like "moves inventory units between shipments"
402+
it_behaves_like "properly manages inventory units"
369403

370404
context "if the desired shipment is invalid" do
371405
let(:desired_shipment) { order.shipments.build(stock_location_id: 99_999_999) }

0 commit comments

Comments
 (0)