mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-22 00:57:26 +00:00
88 lines
2.6 KiB
Ruby
88 lines
2.6 KiB
Ruby
require 'spec_helper'
|
|
|
|
module Spree
|
|
describe Variant do
|
|
context "when the product has variants" do
|
|
let!(:product) { create(:simple_product) }
|
|
let!(:variant) { create(:variant, product: product) }
|
|
|
|
%w(weight volume).each do |unit|
|
|
context "when the product's unit is #{unit}" do
|
|
before do
|
|
product.update_attribute :variant_unit, unit
|
|
product.reload
|
|
end
|
|
|
|
it "is valid when unit value is set and unit description is not" do
|
|
variant.unit_value = 1
|
|
variant.unit_description = nil
|
|
variant.should be_valid
|
|
end
|
|
|
|
it "is invalid when unit value is not set" do
|
|
variant.unit_value = nil
|
|
variant.should_not be_valid
|
|
end
|
|
|
|
it "has a valid master variant" do
|
|
product.master.should be_valid
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when the product's unit is items" do
|
|
before do
|
|
product.update_attribute :variant_unit, 'items'
|
|
product.reload
|
|
end
|
|
|
|
it "is valid with only unit value set" do
|
|
variant.unit_value = 1
|
|
variant.unit_description = nil
|
|
variant.should be_valid
|
|
end
|
|
|
|
it "is valid with only unit description set" do
|
|
variant.unit_value = nil
|
|
variant.unit_description = 'Medium'
|
|
variant.should be_valid
|
|
end
|
|
|
|
it "is invalid when neither unit value nor unit description are set" do
|
|
variant.unit_value = nil
|
|
variant.unit_description = nil
|
|
variant.should_not be_valid
|
|
end
|
|
|
|
it "has a valid master variant" do
|
|
product.master.should be_valid
|
|
end
|
|
end
|
|
end
|
|
|
|
context "when the product does not have variants" do
|
|
let(:product) { create(:simple_product, variant_unit: nil) }
|
|
let(:variant) { product.master }
|
|
|
|
it "does not require unit value or unit description when the product's unit is empty" do
|
|
variant.unit_value = nil
|
|
variant.unit_description = nil
|
|
variant.should be_valid
|
|
end
|
|
end
|
|
|
|
describe "deleting unit option values" do
|
|
it "deletes option values for unit option types" do
|
|
p = create(:simple_product, variant_unit: 'weight', variant_unit_scale: 1)
|
|
ot = Spree::OptionType.find_by_name 'unit_weight'
|
|
ov = create(:option_value, option_type: ot, name: '1 kg', presentation: '1 kg')
|
|
v = create(:variant, product: p, option_values: [ov])
|
|
|
|
expect {
|
|
v.delete_unit_option_values
|
|
}.to change(Spree::OptionValue, :count).by(-1)
|
|
end
|
|
end
|
|
end
|
|
end
|