mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Add order_shipping_method specs to cover select_shipping_method
This commit is contained in:
@@ -23,12 +23,13 @@ module OrderShippingMethod
|
||||
#
|
||||
# @return [Shipment]
|
||||
def select_shipping_method(shipping_method_id)
|
||||
return if shipping_method_id.nil? || shipments.empty?
|
||||
return if shipping_method_id.blank? || shipments.empty?
|
||||
shipment = shipments.first
|
||||
|
||||
shipping_rate = shipment.shipping_rates.find_by_shipping_method_id(shipping_method_id)
|
||||
return unless shipping_rate
|
||||
|
||||
shipment.selected_shipping_rate_id=(shipping_rate.id)
|
||||
shipment.shipping_method
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,15 +3,15 @@ require 'spec_helper'
|
||||
describe OrderShippingMethod do
|
||||
let(:order) { create(:order) }
|
||||
|
||||
describe '#shipping_method' do
|
||||
context 'when order has no shipments' do
|
||||
it 'returns nil' do
|
||||
describe "#shipping_method" do
|
||||
context "when order has no shipments" do
|
||||
it "returns nil" do
|
||||
expect(order.shipping_method).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when order has single shipment' do
|
||||
it 'returns the shipments shipping_method' do
|
||||
context "when order has single shipment" do
|
||||
it "returns the shipments shipping_method" do
|
||||
shipping_method = create(:shipping_method_with, :flat_rate)
|
||||
shipment = create(:shipment_with, :shipping_method, shipping_method: shipping_method)
|
||||
order.shipments = [shipment]
|
||||
@@ -20,4 +20,61 @@ describe OrderShippingMethod do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#select_shipping_method" do
|
||||
let(:shipping_method) { create(:shipping_method_with, :flat_rate) }
|
||||
|
||||
context "when order has no shipment" do
|
||||
it "returns nil" do
|
||||
expect(order.select_shipping_method(shipping_method.id)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when order has a shipment" do
|
||||
let(:shipment) { create(:shipment_with, :shipping_method, shipping_method: shipping_method) }
|
||||
before { order.shipments = [shipment] }
|
||||
|
||||
context "when no shipping_method_id is provided" do
|
||||
it "returns nil for nil shipping_method_id" do
|
||||
expect(order.select_shipping_method(nil)).to be_nil
|
||||
end
|
||||
|
||||
it "returns nil for empty shipping_method_id" do
|
||||
empty_shipping_method_id = ' '
|
||||
expect(shipment.shipping_rates).to_not receive(:find_by_shipping_method_id).with(empty_shipping_method_id)
|
||||
|
||||
expect(order.select_shipping_method(empty_shipping_method_id)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when shipping_method_id is not valid for the order" do
|
||||
it "returns nil" do
|
||||
invalid_shipping_method_id = order.shipment.shipping_method.id + 1000
|
||||
expect(shipment.shipping_rates).to receive(:find_by_shipping_method_id).with(invalid_shipping_method_id) { nil }
|
||||
|
||||
expect(order.select_shipping_method(invalid_shipping_method_id)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context "when shipping_method_id is valid for the order" do
|
||||
it "returns the shipments shipping_method" do
|
||||
expect(shipment).to receive(:selected_shipping_rate_id=)
|
||||
|
||||
expect(order.select_shipping_method(shipping_method.id)).to eq shipping_method
|
||||
end
|
||||
end
|
||||
|
||||
context "when multiple shipping_methods exist in the shipment" do
|
||||
let(:expensive_shipping_method) { create(:shipping_method_with, :expensive_name) }
|
||||
before { shipment.add_shipping_method(expensive_shipping_method, false ) }
|
||||
it "selects a shipping method that was not selected by default" do
|
||||
expect(shipment.shipping_method).to eq shipping_method
|
||||
|
||||
expect(order.select_shipping_method(expensive_shipping_method.id)).to eq expensive_shipping_method
|
||||
|
||||
expect(shipment.shipping_method).to eq expensive_shipping_method
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user