Skip to content

Commit

Permalink
Test Estimator#coordinator_options integration
Browse files Browse the repository at this point in the history
This commit ensures that the simple coordinator and a custom estimator
work when custom coordinator options are passed in.

Co-authored-by: Noah Silvera <[email protected]>
  • Loading branch information
benjaminwil and Noah-Silvera committed Oct 29, 2024
1 parent 2ccef90 commit b4eed42
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/app/models/spree/stock/estimator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def shipping_rates(package, frontend_only = true)

private

attr_reader :coordinator_options

def choose_default_shipping_rate(shipping_rates)
unless shipping_rates.empty?
default_shipping_rate = Spree::Config.shipping_rate_selector_class.new(shipping_rates).find_default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,40 @@
end
end
end

describe "passing custom coordinator options" do
subject {
Spree::Stock::SimpleCoordinator.new(order, coordinator_options:)
}

let(:coordinator_options) { {arbitrary_shipping_rates: [my_shipping_rate]} }
let(:order) { create :order_with_line_items }
let(:my_shipping_rate) { create(:shipping_method).shipping_rates.new }

it "uses coordinator options passed in to the simple coordinator to the stock estimator" do
MyEstimator = Class.new(Spree::Stock::Estimator) do
def shipping_rates(package, _frontend_only = true)
raise ShipmentRequired if package.shipment.nil?
raise OrderRequired if package.shipment.order.nil?

first_shipping_rate = coordinator_options[:arbitrary_shipping_rates]&.first

if first_shipping_rate
first_shipping_rate.selected = true
return [first_shipping_rate]
else
raise StandardError, "no shipping rate!"
end
end
end

original_estimator_class = Spree::Config.stock.estimator_class.to_s
Spree::Config.stock.estimator_class = MyEstimator.to_s

expect(subject.shipments.first.selected_shipping_rate)
.to eq my_shipping_rate
ensure
Spree::Config.stock.estimator_class = original_estimator_class
end
end
end

0 comments on commit b4eed42

Please sign in to comment.