From 1f59538d4da3da09c5ad7bc1f8fbd84821a49552 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 09:51:58 +0200 Subject: [PATCH 1/7] Add spec for Spree::Variant#on_backorder --- core/spec/models/spree/variant_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 0885fc9d51b..131f67389d1 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -958,4 +958,18 @@ end end end + + describe "#on_backorder" do + let(:variant) { create(:variant) } + + subject { variant.on_backorder } + + it { is_expected.to be_zero } + + context "with a backordered inventory_unit" do + let!(:backordered_inventory_unit) { create(:inventory_unit, variant: variant, state: :backordered) } + + it { is_expected.to eq(1) } + end + end end From 825a7607a4100934874731f79560124053cacbf7 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 09:54:01 +0200 Subject: [PATCH 2/7] Add spec for Spree::Variant#deleted? --- core/spec/models/spree/variant_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 131f67389d1..637423058bb 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -972,4 +972,20 @@ it { is_expected.to eq(1) } end end + + describe "#deleted?" do + let(:variant) { create(:variant) } + + subject { variant.deleted? } + + it { is_expected.to be false } + + context "if the variant is discarded" do + before do + variant.discard + end + + it { is_expected.to be true } + end + end end From 9acd88ae932ef00fb507b3a68eee305f18d56189 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 10:03:44 +0200 Subject: [PATCH 3/7] Add spec for Spree::Variant#options= Also fixes the type of the default argument to empty array as the documentation says. --- core/app/models/spree/variant.rb | 2 +- core/spec/models/spree/variant_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 97408a1a1c8..3af834326fd 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -211,7 +211,7 @@ def deleted? # Assign given options hash to option values. # # @param options [Array] array of hashes with a name and value. - def options=(options = {}) + def options=(options = []) options.each do |option| set_option_value(option[:name], option[:value]) end diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 637423058bb..cda3cbfb43e 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -164,6 +164,18 @@ }.to change(multi_variant.option_values, :count).by(1) end + context "setting using #options=" do + it "should set option value" do + expect(multi_variant.option_value('media_type')).to be_nil + + multi_variant.options = [{ name: "media_type", value: 'DVD' }] + expect(multi_variant.option_value('media_type')).to eql 'DVD' + + multi_variant.options = [{ name: "media_type", value: 'CD' }] + expect(multi_variant.option_value('media_type')).to eql 'CD' + end + end + context "and a variant is soft-deleted" do let!(:old_options_text) { variant.options_text } From 4198bccabc890ca34d410bd09f9680345746674e Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 10:07:24 +0200 Subject: [PATCH 4/7] Add spec for Spree::Variant#name_and_sku --- core/spec/models/spree/variant_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index cda3cbfb43e..7454ac4992d 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -1000,4 +1000,13 @@ it { is_expected.to be true } end end + + describe "#name_and_sku" do + let(:product) { build(:product, name: "Ernie and Bert" )} + let(:variant) { build(:variant, product: product, sku: "EB1") } + + subject { variant.name_and_sku } + + it { is_expected.to eq("Ernie and Bert - EB1") } + end end From 65e14729ec3d3b6ac099a0834bdcd1e06176eeb2 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 10:11:44 +0200 Subject: [PATCH 5/7] Add spec for Spree::Variant#sku_and_options_text --- core/spec/models/spree/variant_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 7454ac4992d..1ae53bb9c17 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -1009,4 +1009,11 @@ it { is_expected.to eq("Ernie and Bert - EB1") } end + + describe "#sku_and_options_text" do + let(:variant) { create(:variant, sku: "EB1") } + + subject { variant.sku_and_options_text } + it { is_expected.to eq("EB1 Size: S") } + end end From b35432e5dbd16e91de50d35d8e5debe2269f93ec Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Mon, 26 Sep 2022 10:18:47 +0200 Subject: [PATCH 6/7] Add spec for Variant#check_price validation --- core/spec/models/spree/variant_spec.rb | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index 1ae53bb9c17..c8f600b20d8 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -45,6 +45,28 @@ variant_with_same_sku = build(:variant, sku: variant.sku) expect(variant_with_same_sku).to be_invalid end + + context "if it is a master variant" do + let(:product) { create(:product) } + subject(:variant) { product.master } + + before do + variant.price = nil + end + + it "is invalid without a price" do + variant.valid? + expect(subject.errors.full_messages).to include("Price Must supply price for variant or master.price for product.") + end + + context "if it has a price" do + before do + variant.price = 10 + end + + it { is_expected.to be_valid } + end + end end context "after create" do From fda2b924ac29d4b9b8d8bb3e4eb3bd83cdbf32b3 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 27 Sep 2022 09:24:10 +0200 Subject: [PATCH 7/7] Further specify param type for Spree::Variant#options= --- core/app/models/spree/variant.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index 3af834326fd..affebf0fc3c 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -210,7 +210,7 @@ def deleted? # Assign given options hash to option values. # - # @param options [Array] array of hashes with a name and value. + # @param options [Array] array of hashes with a name and value. def options=(options = []) options.each do |option| set_option_value(option[:name], option[:value])