Skip to content

Commit a0c1494

Browse files
Test custom Allocator coordinator options integration
This commit ensures that the simple coordinator and a custom allocator work when custom coordinator options are passed in. Co-authored-by: Benjamin Willems <benjamin@super.gd>
1 parent 88872de commit a0c1494

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

core/spec/models/spree/stock/simple_coordinator_integration_spec.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@
235235
end
236236

237237
context "when custom coordinator options are passed" do
238-
let(:order) { create :order_with_line_items }
238+
let(:order) {
239+
create :order_with_line_items, line_items_attributes: [{variant: create(:product_in_stock).master }]
240+
}
239241

240242
subject {
241243
Spree::Stock::SimpleCoordinator.new(order, coordinator_options:)
@@ -273,5 +275,33 @@ def shipping_rates(package, _frontend_only = true)
273275
.to eq my_shipping_rate
274276
end
275277
end
278+
279+
describe "to customize the allocator's behavior" do
280+
let(:coordinator_options) { {force_backordered: true} }
281+
282+
around do |example|
283+
MyAllocator = Class.new(Spree::Stock::Allocator::OnHandFirst) do
284+
def allocate_inventory(desired)
285+
if coordinator_options[:force_backordered]
286+
backordered = allocate(availability.backorderable_by_stock_location_id, desired)
287+
desired -= backordered.values.reduce(&:+) if backordered.present?
288+
[{}, backordered, desired]
289+
else
290+
super
291+
end
292+
end
293+
end
294+
295+
original_allocator_class = Spree::Config.stock.allocator_class
296+
Spree::Config.stock.allocator_class = MyAllocator.to_s
297+
example.run
298+
Spree::Config.stock.allocator_class =
299+
original_allocator_class.to_s
300+
end
301+
302+
it "uses the options to force backordered allocation" do
303+
expect(subject.shipments.flat_map(&:inventory_units).all?(&:backordered?)).to be true
304+
end
305+
end
276306
end
277307
end

0 commit comments

Comments
 (0)