Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Improve `TaxRate#label` to display it right (#12060)

Damian Legawiec5b330b27eabc
diff --git a/core/app/models/spree/tax_rate.rb b/core/app/models/spree/tax_rate.rb
index b07b9cc6c3..15ee8f4d27 100644
--- a/core/app/models/spree/tax_rate.rb
+++ b/core/app/models/spree/tax_rate.rb
@@ -115,11 +115,13 @@ module Spree
def amount_for_label
return '' unless show_rate_in_label?
+ return '' if amount.zero?
' ' + ActiveSupport::NumberHelper::NumberToPercentageConverter.convert(
amount * 100,
locale: I18n.locale,
- strip_insignificant_zeros: true
+ strip_insignificant_zeros: true,
+ precision: 2
)
end
end
diff --git a/core/spec/models/spree/tax_rate_spec.rb b/core/spec/models/spree/tax_rate_spec.rb
index b0b8642f84..5325b2a473 100644
--- a/core/spec/models/spree/tax_rate_spec.rb
+++ b/core/spec/models/spree/tax_rate_spec.rb
@@ -548,4 +548,23 @@ describe Spree::TaxRate, type: :model do
end
end
end
+
+ describe '#label' do
+ it 'returns the name and amount for the tax rate' do
+ tax_rate = Spree::TaxRate.new(name: 'Sales Tax', amount: 0.1)
+ expect(tax_rate.send(:label)).to eq('Sales Tax 10%')
+ end
+ end
+
+ describe '#amount_for_label' do
+ it 'returns an empty string when the amount is 0' do
+ tax_rate = Spree::TaxRate.new(amount: 0)
+ expect(tax_rate.send(:amount_for_label)).to eq('')
+ end
+
+ it 'returns a string with the percentage when the amount is not 0' do
+ tax_rate = Spree::TaxRate.new(amount: 0.1)
+ expect(tax_rate.send(:amount_for_label)).to eq(' 10%')
+ end
+ end
end