Skip to content

Commit

Permalink
fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abdellani committed Nov 1, 2023
1 parent 12d1f69 commit 0267549
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/services/weights_and_measures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def system
}.freeze

def scales_for_variant_unit
@scales_for_variant_unit ||= @units[@variant.product.variant_unit].reject { |_scale, unit_info|
@scales_for_variant_unit ||= @units[@variant.product.variant_unit]&.reject { |_scale, unit_info|
available_units.exclude?(unit_info['name'])
}
end
Expand Down
7 changes: 7 additions & 0 deletions spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,13 @@ module Spree
let(:p2) { create(:product, name: 'Clear United States Honey', variant_unit_scale: 453.6) }
let(:v2) { create(:variant, product: p2, unit_value: 453.6) }
let(:li2) { create(:line_item, order: o, product: p2, variant: v2) }
let(:available_units) {
["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",")
}

before do
Spree::Config.set_preference(:available_units, available_units)
end

it "returns options_text" do
li = build_stubbed(:line_item)
Expand Down
4 changes: 4 additions & 0 deletions spec/services/unit_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
subject { UnitPrice.new(variant) }
let(:variant) { Spree::Variant.new }
let(:product) { instance_double(Spree::Product) }
let(:available_units) {
["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",")
}

before do
allow(variant).to receive(:product) { product }
Spree::Config.set_preference(:available_units, available_units)
end

describe "#unit" do
Expand Down
7 changes: 7 additions & 0 deletions spec/services/variant_units/option_value_namer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

module VariantUnits
describe OptionValueNamer do
let(:available_units) {
["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",")
}

before do
Spree::Config.set_preference(:available_units, available_units)
end
describe "generating option value name" do
let(:v) { Spree::Variant.new }
let(:p) { Spree::Product.new }
Expand Down
33 changes: 32 additions & 1 deletion spec/services/weights_and_measures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
subject { WeightsAndMeasures.new(variant) }
let(:variant) { Spree::Variant.new }
let(:product) { instance_double(Spree::Product) }
let(:available_units) {
["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",")
}

before do
allow(variant).to receive(:product) { product }
Spree::Config.set_preference(:available_units, available_units)
end

describe "#system" do
Expand Down Expand Up @@ -69,16 +73,43 @@
allow(variant).to receive(:unit_value) { 1500 }
expect(subject.scale_for_unit_value).to eq([1000.0, "kg"])
end

describe "should not display in kg if this unit is not selected" do
let(:available_units) { ["mg", "g", "T"].join(",") }

it "should display in g" do
allow(product).to receive(:variant_unit_scale) { 1.0 }
allow(variant).to receive(:unit_value) { 1500 }
expect(subject.scale_for_unit_value).to eq([1.0, "g"])
end
end
end
end

context "volume" do
it "for a unit value that should display in L" do
it "for a unit value that should display in kL" do
allow(product).to receive(:variant_unit) { "volume" }
allow(product).to receive(:variant_unit_scale) { 1.0 }
allow(variant).to receive(:unit_value) { 1500 }
expect(subject.scale_for_unit_value).to eq([1000, "kL"])
end

it "for a unit value that should display in dL" do
allow(product).to receive(:variant_unit) { "volume" }
allow(product).to receive(:variant_unit_scale) { 1.0 }
allow(variant).to receive(:unit_value) { 0.5 }
expect(subject.scale_for_unit_value).to eq([0.1, "dL"])
end

context "should not display in dL/cL if those units are not selected" do
let(:available_units){ ["mL", "L", "kL", "gal"].join(",") }
it "for a unit value that should display in mL" do
allow(product).to receive(:variant_unit) { "volume" }
allow(product).to receive(:variant_unit_scale) { 1.0 }
allow(variant).to receive(:unit_value) { 0.5 }
expect(subject.scale_for_unit_value).to eq([0.001, "mL"])
end
end
end

context "items" do
Expand Down
8 changes: 8 additions & 0 deletions spec/system/admin/product_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@

let(:shipping_category_id_str) { Spree::ShippingCategory.all.first.id.to_s }

let(:available_units) {
["mg", "g", "kg", "T", "oz", "lb", "mL", "cL", "dL", "L", "kL", "gal"].join(",")
}

before do
Spree::Config.set_preference(:available_units, available_units)
end

describe "when importing products from uploaded file" do
before do
allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL")
Expand Down

0 comments on commit 0267549

Please sign in to comment.