Created
September 13, 2018 19:26
-
-
Save cored/309a4771e617ab8436b7ae6b924cfe9f 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 "rails_helper" | |
RSpec.describe Groupments::OrderSerializer do | |
subject(:payload) do | |
described_class.new( | |
grouped_shipments: shipments, | |
unused_shipment_numbers: unused_shipment_numbers, | |
order_number: order_number | |
).serializable_hash | |
end | |
let(:shipments) { [shipment] } | |
let(:unused_shipment_numbers) { ["H00000"] } | |
let(:order_number) { shipment.order_number } | |
let(:shipment) do | |
create(:shipment, | |
order_number: "ORDER_NUMBER", | |
number: "SHIPMENT_NUMBER", | |
products: products) | |
end | |
let(:product) do | |
create(:product, sku: "PRODUCT-SKU", bundled_products: []) | |
end | |
let(:products) do | |
[product] | |
end | |
let(:expected_payload) do | |
{ | |
shipments: [ | |
{ | |
number: shipment.number, | |
inventory_units: expected_inventory_units, | |
}, | |
], | |
bundled_variants: expected_bundled_variants, | |
shipments_to_delete: unused_shipment_numbers, | |
order_number: order_number, | |
} | |
end | |
describe "#serializable_hash" do | |
context "when a shipment does not contains a product bundle" do | |
let(:expected_inventory_units) { [{ sku: product.sku }] } | |
let(:expected_bundled_variants) { [] } | |
it "serializes the persisted shipments with numbers on shipments to delete" do | |
expect(payload).to eq expected_payload | |
end | |
context "when the shipment contains an adjustable base" do | |
it "serializes the order with 1 instance of the product (not for shipment items)" do | |
expect(payload).to eq expected_payload | |
end | |
context "when the shipment contains multiple adjustable bases" do | |
let(:products) { [product, product, product] } | |
let(:expected_inventory_units) do | |
Array.new(3) { { sku: product.sku } } | |
end | |
let(:expected_bundled_variants) { [] } | |
it "serializes the order with 1 instance for each product (not for shipment items)" do | |
expect(payload).to eq expected_payload | |
end | |
end | |
end | |
end | |
context "when a shipment contains a product bundle" do | |
let(:bundle_product_a) do | |
create(:product, sku: "BUNDLE_PROD_A") | |
end | |
let(:bundle_product_b) do | |
create(:product, sku: "BUNDLE_PROD_B") | |
end | |
let(:product) do | |
create(:product, | |
sku: "BUNDLE_SKU", | |
bundled_products: [bundle_product_a, bundle_product_b]) | |
end | |
let(:products) do | |
[product] | |
end | |
let(:expected_inventory_units) { [{ sku: product.sku }] } | |
let(:expected_bundled_variants) do | |
[{ "BUNDLE_SKU" => ["BUNDLE_PROD_A", "BUNDLE_PROD_B"] }] | |
end | |
it "include the bundled variants sku's for the bundled products" do | |
expect(payload).to eq expected_payload | |
end | |
end | |
context "when a shipment contains more than one product bundle" do | |
let(:bundle_product_a) do | |
create(:product, sku: "BUNDLE_PROD_A") | |
end | |
let(:bundle_product_b) do | |
create(:product, sku: "BUNDLE_PROD_B") | |
end | |
let(:product_a) do | |
create(:product, | |
sku: "BUNDLE_SKU_A", | |
bundled_products: [bundle_product_a, bundle_product_b]) | |
end | |
let(:product_b) do | |
create(:product, | |
sku: "BUNDLE_SKU_B", | |
bundled_products: [bundle_product_a, bundle_product_b]) | |
end | |
let(:products) do | |
[product_a, product_b] | |
end | |
let(:expected_inventory_units) do | |
[{ sku: product_b.sku }, {sku: product_a.sku }] | |
end | |
let(:expected_bundled_variants) do | |
[ | |
{"BUNDLE_SKU_B" => ["BUNDLE_PROD_A", "BUNDLE_PROD_B"]}, | |
{"BUNDLE_SKU_A" => ["BUNDLE_PROD_A", "BUNDLE_PROD_B"]}, | |
] | |
end | |
it "includes all the bundles in the bundled variants" do | |
expect(payload).to eq(expected_payload) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment