Skip to content

Commit

Permalink
Use named config method
Browse files Browse the repository at this point in the history
And mock the preference value, rather than setting it. Before, the set preference could have leaked to other tests.
(I noticed that it was already like this in product_import_spec.rb)
  • Loading branch information
dacook committed Nov 3, 2023
1 parent 0121747 commit 65aa6f5
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 26 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 @@ -67,6 +67,6 @@ def find_largest_unit(scales, product_scale_system)
end

def available_units
Spree::Config.preferences[:available_units].split(",")
Spree::Config.available_units.split(",")
end
end
7 changes: 2 additions & 5 deletions spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,9 @@ 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)
allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL")
end

it "returns options_text" do
Expand All @@ -709,7 +706,7 @@ module Spree
expect(li.unit_to_display).to eq("ponies")
end

it "returns options_text based on units" do
it "returns options_text based on configured units" do
expect(li1.options_text).to eq("500g")
expect(li2.options_text).to eq("1lb")
end
Expand Down
5 changes: 1 addition & 4 deletions spec/services/unit_prices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
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)
allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL")
end

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

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 Expand Up @@ -69,6 +62,10 @@ module VariantUnits
let(:v) { Spree::Variant.new }
let(:subject) { OptionValueNamer.new v }

before do
allow(Spree::Config).to receive(:available_units).and_return("g,lb,oz,kg,T,mL,L,kL")
end

it "generates simple values" do
p = double(:product, variant_unit: 'weight', variant_unit_scale: 1.0)
allow(v).to receive(:product) { p }
Expand Down
2 changes: 1 addition & 1 deletion spec/services/weights_and_measures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

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

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

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 65aa6f5

Please sign in to comment.