mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-18 04:39:14 +00:00
Add tests
This commit is contained in:
committed by
Rob Harrington
parent
f4624ead42
commit
f67a8c1f2d
43
spec/lib/spree/localized_number_spec.rb
Normal file
43
spec/lib/spree/localized_number_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
11
spec/models/spree/calculator/flat_rate_spec.rb
Normal file
11
spec/models/spree/calculator/flat_rate_spec.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
17
spec/support/localized_number_helper.rb
Normal file
17
spec/support/localized_number_helper.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user