Created
May 21, 2018 19:09
-
-
Save cored/0144bb3b1db167990d1babaee271e2db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require_relative "../../../app/services/groupments/group_shipments" | |
FakeShipment = Struct.new(:parcel, :courier, :shipment_items) do | |
def courier? | |
courier | |
end | |
def parcel? | |
parcel | |
end | |
end | |
describe Groupments::GroupShipments do | |
subject(:group_shipments) { described_class } | |
let(:shipment_item) { instance_double "ShipmentItem" } | |
describe ".call" do | |
context "when no groupment it's required" do | |
let(:shipment) do | |
instance_double "Shipment", courier?: false, shipment_items: [], parcel?: false | |
end | |
let(:shipments) do | |
[ | |
shipment, | |
shipment, | |
] | |
end | |
it "return shipments without grouping" do | |
expect( | |
group_shipments.call(shipments: shipments) | |
).to match_array [shipment, shipment] | |
end | |
end | |
context "when one or more shipments will be deliver through parcel" do | |
let(:courier_shipment) { FakeShipment.new(false, true, [shipment_item]) } | |
let(:another_shipment_item) { instance_double "ShipmentItem" } | |
let(:another_parcel_shipment) { FakeShipment.new(true, false, [another_shipment_item]) } | |
let(:parcel_shipment) do | |
instance_double("Shipment", | |
courier?: false, | |
shipment_items: [shipment_item], parcel?: true) | |
end | |
let(:shipments) do | |
[ | |
courier_shipment, | |
another_parcel_shipment, | |
parcel_shipment, | |
] | |
end | |
it "group courier shipments in just one shipment" do | |
expect( | |
group_shipments.call(shipments: shipments) | |
).to match_array [courier_shipment, another_parcel_shipment] | |
end | |
it "assigns the shipment items to a single courier shipment" do | |
expect( | |
group_shipments.call(shipments: shipments) | |
.find(&:parcel?).shipment_items | |
).to match_array [shipment_item, another_shipment_item] | |
end | |
end | |
context "when one or more shipments will be deliver through local courier" do | |
let(:courier_shipment) { FakeShipment.new(false, true, [shipment_item]) } | |
let(:another_shipment_item) { instance_double "ShipmentItem" } | |
let(:another_courier_shipment) do | |
instance_double("Shipment", | |
shipment_items: [another_shipment_item], | |
courier?: true, parcel?: false) | |
end | |
let(:shipment) do | |
instance_double("Shipment", | |
courier?: false, | |
shipment_items: [], parcel?: false) | |
end | |
let(:shipments) do | |
[ | |
courier_shipment, | |
another_courier_shipment, | |
shipment, | |
] | |
end | |
it "group courier shipments in just one shipment" do | |
expect( | |
group_shipments.call(shipments: shipments) | |
).to match_array [courier_shipment, shipment] | |
end | |
it "assigns the shipment items to a single courier shipment" do | |
expect( | |
group_shipments.call(shipments: shipments) | |
.find(&:courier?).shipment_items | |
).to match_array [shipment_item, another_shipment_item] | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment