Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve variant spec coverage #4642

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ def deleted?

# Assign given options hash to option values.
#
# @param options [Array] array of hashes with a name and value.
def options=(options = {})
# @param options [Array<Hash{name: String, value: String}>] array of hashes with a name and value.
def options=(options = [])
options.each do |option|
set_option_value(option[:name], option[:value])
end
Expand Down
80 changes: 80 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +186,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 }

Expand Down Expand Up @@ -958,4 +992,50 @@
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

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

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

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