From f67a8c1f2dc5cc3d4734051e837ed9cbd04f0025 Mon Sep 17 00:00:00 2001 From: Pierre de Lacroix Date: Wed, 29 Nov 2017 16:51:30 +0100 Subject: [PATCH] Add tests --- spec/lib/spree/localized_number_spec.rb | 43 +++++++++++++++++++ .../calculator/flat_percent_per_item_spec.rb | 6 +++ spec/models/spree/adjustment_spec.rb | 4 ++ .../flat_percent_item_total_spec.rb | 6 ++- .../models/spree/calculator/flat_rate_spec.rb | 11 +++++ .../spree/calculator/flexi_rate_spec.rb | 4 ++ spec/models/spree/calculator/per_item_spec.rb | 4 ++ .../spree/calculator/price_sack_spec.rb | 4 ++ spec/models/spree/payment_spec.rb | 4 ++ spec/models/spree/variant_spec.rb | 6 +++ spec/models/variant_override_spec.rb | 4 ++ spec/support/localized_number_helper.rb | 17 ++++++++ 12 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 spec/lib/spree/localized_number_spec.rb create mode 100644 spec/models/spree/calculator/flat_rate_spec.rb create mode 100644 spec/support/localized_number_helper.rb diff --git a/spec/lib/spree/localized_number_spec.rb b/spec/lib/spree/localized_number_spec.rb new file mode 100644 index 0000000000..8f2bbc7794 --- /dev/null +++ b/spec/lib/spree/localized_number_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe Spree::LocalizedNumber do + context ".parse" do + context "with decimal point" do + it "captures the proper amount for a formatted string" do + expect(described_class.parse('1,599.99')).to eql 1599.99 + end + end + + context "with decimal comma" do + it "captures the proper amount for a formatted string" do + expect(described_class.parse('1.599,99')).to eql 1599.99 + end + end + + context "with a numeric input" do + it "uses the amount as is" do + expect(described_class.parse(1599.99)).to eql 1599.99 + end + end + end + + context ".valid_localizable_number?" do + context "with a properly formatted string" do + it "returns true" do + expect(described_class.valid_localizable_number?('1.599,99')).to eql true + end + end + + context "with a string having 2 digits between separators" do + it "returns false" do + expect(described_class.valid_localizable_number?('1,59.99')).to eql false + end + end + + context "with a numeric input" do + it "returns true" do + expect(described_class.valid_localizable_number?(1599.99)).to eql true + end + end + end +end diff --git a/spec/models/calculator/flat_percent_per_item_spec.rb b/spec/models/calculator/flat_percent_per_item_spec.rb index aaeff2a328..8a2a962ef0 100644 --- a/spec/models/calculator/flat_percent_per_item_spec.rb +++ b/spec/models/calculator/flat_percent_per_item_spec.rb @@ -1,3 +1,5 @@ +require 'spec_helper' + describe Calculator::FlatPercentPerItem do let(:calculator) { Calculator::FlatPercentPerItem.new preferred_flat_percent: 20 } @@ -10,4 +12,8 @@ describe Calculator::FlatPercentPerItem do line_item = Spree::LineItem.new price: 0.86, quantity: 8 expect(calculator.compute(line_item)).to eq 1.36 end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_flat_percent] + end end diff --git a/spec/models/spree/adjustment_spec.rb b/spec/models/spree/adjustment_spec.rb index 2292381009..fa24de3122 100644 --- a/spec/models/spree/adjustment_spec.rb +++ b/spec/models/spree/adjustment_spec.rb @@ -297,5 +297,9 @@ module Spree end end end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:amount] + end end end diff --git a/spec/models/spree/calculator/flat_percent_item_total_spec.rb b/spec/models/spree/calculator/flat_percent_item_total_spec.rb index edef70b5bc..998b5fca2a 100644 --- a/spec/models/spree/calculator/flat_percent_item_total_spec.rb +++ b/spec/models/spree/calculator/flat_percent_item_total_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' - describe Spree::Calculator::FlatPercentItemTotal do +describe Spree::Calculator::FlatPercentItemTotal do let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new } let(:line_item) { instance_double(Spree::LineItem, amount: 10) } @@ -9,4 +9,8 @@ require 'spec_helper' it "should compute amount correctly for a single line item" do calculator.compute(line_item).should == 1.0 end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_flat_percent] + end end diff --git a/spec/models/spree/calculator/flat_rate_spec.rb b/spec/models/spree/calculator/flat_rate_spec.rb new file mode 100644 index 0000000000..42b594b30f --- /dev/null +++ b/spec/models/spree/calculator/flat_rate_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Spree::Calculator::FlatRate do + let(:calculator) { Spree::Calculator::FlatRate.new } + + before { calculator.stub :preferred_amount => 10 } + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_amount] + end +end diff --git a/spec/models/spree/calculator/flexi_rate_spec.rb b/spec/models/spree/calculator/flexi_rate_spec.rb index 276e09b4b6..49c2371e13 100644 --- a/spec/models/spree/calculator/flexi_rate_spec.rb +++ b/spec/models/spree/calculator/flexi_rate_spec.rb @@ -14,4 +14,8 @@ describe Spree::Calculator::FlexiRate do it "allows creation of new object with all the attributes" do Spree::Calculator::FlexiRate.new(preferred_first_item: 1, preferred_additional_item: 1, preferred_max_items: 1) end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_first_item, :preferred_additional_item] + end end diff --git a/spec/models/spree/calculator/per_item_spec.rb b/spec/models/spree/calculator/per_item_spec.rb index 09c2fb02f6..7f483372d5 100644 --- a/spec/models/spree/calculator/per_item_spec.rb +++ b/spec/models/spree/calculator/per_item_spec.rb @@ -9,4 +9,8 @@ describe Spree::Calculator::PerItem do calculator.stub(calculable: shipping_calculable) calculator.compute(line_item).to_f.should == 50 # 5 x 10 end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_amount] + end end diff --git a/spec/models/spree/calculator/price_sack_spec.rb b/spec/models/spree/calculator/price_sack_spec.rb index 627e21589b..9cdde0a662 100644 --- a/spec/models/spree/calculator/price_sack_spec.rb +++ b/spec/models/spree/calculator/price_sack_spec.rb @@ -14,4 +14,8 @@ describe Spree::Calculator::PriceSack do it "computes with a line item object" do calculator.compute(line_item) end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:preferred_minimal_amount, :preferred_normal_amount, :preferred_discount_amount] + end end diff --git a/spec/models/spree/payment_spec.rb b/spec/models/spree/payment_spec.rb index 36fb1ceaea..6f84fb5cc4 100644 --- a/spec/models/spree/payment_spec.rb +++ b/spec/models/spree/payment_spec.rb @@ -208,5 +208,9 @@ module Spree end end end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:amount] + end end end diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index 202ba2afbe..046be2bfd2 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -511,6 +511,12 @@ module Spree }.to change(Spree::OptionValue, :count).by(0) end end + + context "extends LocalizedNumber" do + subject! { build(:variant) } + + it_behaves_like "a model using the LocalizedNumber module", [:price, :cost_price, :weight] + end end describe "destruction" do diff --git a/spec/models/variant_override_spec.rb b/spec/models/variant_override_spec.rb index 91956a082c..0a07fdd1af 100644 --- a/spec/models/variant_override_spec.rb +++ b/spec/models/variant_override_spec.rb @@ -147,4 +147,8 @@ describe VariantOverride do vo.reload.count_on_hand.should == 12 end end + + context "extends LocalizedNumber" do + it_behaves_like "a model using the LocalizedNumber module", [:price] + end end diff --git a/spec/support/localized_number_helper.rb b/spec/support/localized_number_helper.rb new file mode 100644 index 0000000000..b726073de0 --- /dev/null +++ b/spec/support/localized_number_helper.rb @@ -0,0 +1,17 @@ +shared_examples "a model using the LocalizedNumber module" do |attributes| + attributes.each do |attribute| + setter = "#{attribute}=" + + it "uses the LocalizedNumber.parse method when setting #{attribute}" do + allow(Spree::LocalizedNumber).to receive(:parse).and_return(nil) + expect(Spree::LocalizedNumber).to receive(:parse).with('1.599,99') + subject.send(setter, '1.599,99') + end + + it "creates an error if the input to #{attribute} is invalid" do + subject.send(setter, '1.59,99') + subject.valid? + expect(subject.errors[attribute]).to include(I18n.t('spree.localized_number.invalid_format')) + end + end +end