mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #3429 from luisramos0/2-0-in-stock
[Spree Upgrade] 3428 Fix in_stock? and shopping/variant_overrides_spec
This commit is contained in:
@@ -125,11 +125,6 @@ module VariantStock
|
||||
on_demand || total_on_hand >= quantity
|
||||
end
|
||||
|
||||
# We override Spree::Variant.in_stock? to avoid depending on the non-overidable method Spree::Stock::Quantifier.can_supply?
|
||||
def in_stock?(quantity = 1)
|
||||
can_supply?(quantity)
|
||||
end
|
||||
|
||||
# Moving Spree::StockLocation.fill_status to the variant enables us to override this behaviour for variant overrides
|
||||
# We can have this responsibility here in the variant because there is only one stock item per variant
|
||||
#
|
||||
|
||||
@@ -87,6 +87,12 @@ Spree::Variant.class_eval do
|
||||
]
|
||||
end
|
||||
|
||||
# We override in_stock? to avoid depending on the non-overridable method Spree::Stock::Quantifier.can_supply?
|
||||
# VariantStock implements can_supply? itself which depends on overridable methods
|
||||
def in_stock?(quantity = 1)
|
||||
can_supply?(quantity)
|
||||
end
|
||||
|
||||
def price_with_fees(distributor, order_cycle)
|
||||
price + fees_for(distributor, order_cycle)
|
||||
end
|
||||
|
||||
@@ -39,12 +39,10 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do
|
||||
end
|
||||
|
||||
describe "viewing products" do
|
||||
it "shows the overridden price" do
|
||||
page.should_not have_price with_currency(12.22) # product1_variant1.price ($11.11) + 10% fee
|
||||
it "shows price and stock from the override" do
|
||||
page.should have_price with_currency(61.11) # product1_variant1_override.price ($55.55) + 10% fee
|
||||
end
|
||||
page.should_not have_price with_currency(12.22) # product1_variant1.price ($11.11) + 10% fee
|
||||
|
||||
it "looks up stock from the override" do
|
||||
# Product should appear but one of the variants is out of stock
|
||||
page.should_not have_content product1_variant2.options_text
|
||||
|
||||
@@ -150,7 +148,7 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do
|
||||
end
|
||||
|
||||
it "does not show out of stock flags on order confirmation page" do
|
||||
product1_variant3.update_attribute :count_on_hand, 0
|
||||
product1_variant3.count_on_hand = 0
|
||||
fill_in "variants[#{product1_variant3.id}]", with: "2"
|
||||
click_checkout
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
require 'spec_helper'
|
||||
require 'open_food_network/scope_variant_to_hub'
|
||||
|
||||
module OpenFoodNetwork
|
||||
@@ -107,6 +108,58 @@ module OpenFoodNetwork
|
||||
end
|
||||
end
|
||||
|
||||
# in_stock? is indirectly overridden through can_supply?
|
||||
# can_supply? is indirectly overridden by on_demand and total_on_hand
|
||||
# these tests validate this chain is working correctly
|
||||
describe "overriding in_stock?" do
|
||||
before { v.on_demand = false }
|
||||
|
||||
context "when an override exists" do
|
||||
before { vo }
|
||||
|
||||
context "when variant in stock" do
|
||||
it "returns true if VO in stock" do
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false if VO out of stock" do
|
||||
vo.update_attribute :count_on_hand, 0
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context "when variant out of stock" do
|
||||
before { v.count_on_hand = 0 }
|
||||
|
||||
it "returns true if VO in stock" do
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false if VO out of stock" do
|
||||
vo.update_attribute :count_on_hand, 0
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when there's no override" do
|
||||
it "returns true if variant in stock" do
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false if variant out of stock" do
|
||||
v.count_on_hand = 0
|
||||
scoper.scope v
|
||||
expect(v.in_stock?).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "overriding sku" do
|
||||
context "when an override exists" do
|
||||
before { vo }
|
||||
|
||||
@@ -159,4 +159,44 @@ describe VariantStock do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#can_supply?' do
|
||||
context 'when variant on_demand' do
|
||||
let(:variant) { create(:variant, on_demand: true) }
|
||||
|
||||
it "returns true for zero" do
|
||||
expect(variant.can_supply?(0)).to eq(true)
|
||||
end
|
||||
it "returns true for large quantity" do
|
||||
expect(variant.can_supply?(100000)).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant not on_demand' do
|
||||
context 'when variant in stock' do
|
||||
it "returns true for zero" do
|
||||
expect(variant.can_supply?(0)).to eq(true)
|
||||
end
|
||||
it "returns true for number equal to stock level" do
|
||||
expect(variant.can_supply?(variant.total_on_hand)).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false for number above stock level" do
|
||||
expect(variant.can_supply?(variant.total_on_hand + 1)).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when variant out of stock' do
|
||||
before { variant.count_on_hand = 0 }
|
||||
|
||||
it "returns true for zero" do
|
||||
expect(variant.can_supply?(0)).to eq(true)
|
||||
end
|
||||
|
||||
it "returns false for one" do
|
||||
expect(variant.can_supply?(1)).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user