From 596b318778ebfee54dc7ae945cc4c188e3a99142 Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Fri, 12 May 2023 20:14:46 +0100 Subject: [PATCH 1/4] Adds spec on negative adjustment with different localization settings Using shared examples and setting failing ones as pending --- spec/system/admin/adjustments_spec.rb | 94 +++++++++++++++++++++++---- 1 file changed, 80 insertions(+), 14 deletions(-) diff --git a/spec/system/admin/adjustments_spec.rb b/spec/system/admin/adjustments_spec.rb index a88b0b4f15..688377a285 100644 --- a/spec/system/admin/adjustments_spec.rb +++ b/spec/system/admin/adjustments_spec.rb @@ -23,6 +23,10 @@ describe ' zone: create(:zone_with_member), tax_category: tax_category) } + let!(:tax_category_included) { create(:tax_category, name: 'TVA 20%', is_default: true) } + let!(:default_tax_zone) { create(:zone, default_tax: true) } + let!(:tax_rate2) { create(:tax_rate, name: "TVA 20%", amount: 0.2, zone: default_tax_zone, included_in_price: true, tax_category: tax_category_included, calculator: Calculator::DefaultTax.new ) } + before do order.finalize! create(:check_payment, order: order, amount: order.total) @@ -30,22 +34,84 @@ describe ' visit spree.admin_orders_path end - it "adding taxed adjustments to an order" do - # When I go to the adjustments page for the order - page.find('td.actions a.icon-edit').click - click_link 'Adjustments' + shared_examples "when the enable_localized_number preference" do |adjustment_label, adjustment_amount, tax_category, tax, tax_total| + it "creates the adjustment and calculates taxes" do + # When I go to the adjustments page for the order + page.find('td.actions a.icon-edit').click + click_link 'Adjustments' - # And I create a new adjustment with tax - click_link 'New Adjustment' - fill_in 'adjustment_amount', with: 110 - fill_in 'adjustment_label', with: 'Late fee' - select 'GST', from: 'adjustment_tax_category_id' - click_button 'Continue' + # And I create a new adjustment with tax + click_link 'New Adjustment' + fill_in 'adjustment_amount', with: adjustment_amount + fill_in 'adjustment_label', with: adjustment_label + select tax_category.to_s, from: 'adjustment_tax_category_id' + click_button 'Continue' - # Then I should see the adjustment, with the correct tax - expect(page).to have_selector 'td.label', text: 'Late fee' - expect(page).to have_selector 'td.amount', text: '110.00' - expect(page).to have_selector 'td.tax', text: '10.00' + # Then I should see the adjustment, with tax included in the amount + expect(page).to have_selector 'td.label', text: adjustment_label.to_s + expect(page).to have_selector 'td.amount', text: adjustment_amount.to_s + expect(page).to have_selector 'td.tax-category', text: tax_category.to_s + expect(page).to have_selector 'td.tax', text: tax.to_s + expect(page).to have_selector 'td.total', text: tax_total.to_s + end + end + + context "is active" do + before do + allow(Spree::Config).to receive(:enable_localized_number?).and_return(true) + end + + context "included tax" do + context "adding negative, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "TVA 20%", "$0.33", "$-1.67" do + before { pending("#10837") } + end + end + + context "adding positive, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Late fee", "100", "TVA 20%", "$-16.67", "$83.33" do + before { pending("#10837") } + end + end + end + + context "added tax" do + context "adding negative, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "GST", "$10.00", "$8.00" do + before { pending("#10837") } + end + end + + context "adding positive, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Late fee", "110", "GST", "$10.00", "$120" + end + end + end + + context "is not active" do + before do + allow(Spree::Config).to receive(:enable_localized_number?).and_return(false) + end + + context "included tax" do + context "adding negative, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "TVA 20%", "$0.33", "$-1.67" + end + + context "adding positive, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Late fee", "100", "TVA 20%", "$-16.67", "$83.33" + end + end + + context "added tax" do + context "adding negative, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "GST", "$10.00", "$8.00" + end + + context "adding positive, taxed adjustments to an order" do + it_behaves_like "when the enable_localized_number preference", "Late fee", "110", "GST", "$10.00", "$120" + end + end end it "modifying taxed adjustments on an order" do From e7ab839fb3a3c4a6eb91334065403b420212fa4c Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Sun, 14 May 2023 22:51:28 +0100 Subject: [PATCH 2/4] Adds minus sign as validation criteria Removes pending from passing examples --- lib/spree/localized_number.rb | 2 +- spec/system/admin/adjustments_spec.rb | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/spree/localized_number.rb b/lib/spree/localized_number.rb index a1ba18accd..05cc7e7e2e 100644 --- a/lib/spree/localized_number.rb +++ b/lib/spree/localized_number.rb @@ -45,7 +45,7 @@ module Spree def self.valid_localizable_number?(number) return true unless number.is_a?(String) || number.respond_to?(:to_d) # Invalid if only two digits between dividers, or if any non-number characters - return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.]+/ + return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.-]+/ true end diff --git a/spec/system/admin/adjustments_spec.rb b/spec/system/admin/adjustments_spec.rb index 688377a285..bb94ce013f 100644 --- a/spec/system/admin/adjustments_spec.rb +++ b/spec/system/admin/adjustments_spec.rb @@ -63,23 +63,17 @@ describe ' context "included tax" do context "adding negative, taxed adjustments to an order" do - it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "TVA 20%", "$0.33", "$-1.67" do - before { pending("#10837") } - end + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "TVA 20%", "$0.33", "$-1.67" end context "adding positive, taxed adjustments to an order" do - it_behaves_like "when the enable_localized_number preference", "Late fee", "100", "TVA 20%", "$-16.67", "$83.33" do - before { pending("#10837") } - end + it_behaves_like "when the enable_localized_number preference", "Late fee", "100", "TVA 20%", "$-16.67", "$83.33" end end context "added tax" do context "adding negative, taxed adjustments to an order" do - it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "GST", "$10.00", "$8.00" do - before { pending("#10837") } - end + it_behaves_like "when the enable_localized_number preference", "Discount", "-2", "GST", "$10.00", "$8.00" end context "adding positive, taxed adjustments to an order" do From 0442f2da7ee1325d6b1063ff17002bbe89311b0b Mon Sep 17 00:00:00 2001 From: filipefurtad0 Date: Sun, 14 May 2023 22:57:01 +0100 Subject: [PATCH 3/4] Fixes spec warning An example was being ignored due to legacy syntax - WARNING: ignoring the provided expectation message argument since it is not a string or a proc. --- spec/system/admin/adjustments_spec.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/system/admin/adjustments_spec.rb b/spec/system/admin/adjustments_spec.rb index bb94ce013f..f1a181561f 100644 --- a/spec/system/admin/adjustments_spec.rb +++ b/spec/system/admin/adjustments_spec.rb @@ -165,8 +165,8 @@ describe ' it "displays adjustments" do click_link 'Adjustments' - expect(page).to_not have_selector('tr a.icon-edit') - expect(page).to_not have_selector('a.icon-plus'), text: 'New Adjustment' + expect(page).to_not have_selector 'tr a.icon-edit' + expect(page).to_not have_selector 'a.icon-plus', text: 'New Adjustment' end end end From d715b6b3bb0145fe7eab5bc65b3c31f9c53a87bb Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 12 May 2023 16:08:12 +1000 Subject: [PATCH 4/4] Spec fix LocalisedNumber validation to allow negative number As part of this PR https://github.com/openfoodfoundation/openfoodnetwork/pull/10329 LocalisedNumber validation was tightened, but that means negative number were not valid anymore. This commit has been cherry-picked after this fix has already been applied. Now we just change the order of characters in the regex because humans are used to reading the minus at the beginning of the number. But this change doesn't change the logic at all. --- lib/spree/localized_number.rb | 2 +- spec/lib/spree/localized_number_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/spree/localized_number.rb b/lib/spree/localized_number.rb index 05cc7e7e2e..f972399b0a 100644 --- a/lib/spree/localized_number.rb +++ b/lib/spree/localized_number.rb @@ -45,7 +45,7 @@ module Spree def self.valid_localizable_number?(number) return true unless number.is_a?(String) || number.respond_to?(:to_d) # Invalid if only two digits between dividers, or if any non-number characters - return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^0-9,.-]+/ + return false if number.to_s =~ /[.,]\d{2}[.,]/ || number.to_s =~ /[^-0-9,.]+/ true end diff --git a/spec/lib/spree/localized_number_spec.rb b/spec/lib/spree/localized_number_spec.rb index bafe0c822d..b19df7e7ef 100644 --- a/spec/lib/spree/localized_number_spec.rb +++ b/spec/lib/spree/localized_number_spec.rb @@ -58,6 +58,12 @@ describe Spree::LocalizedNumber do it "returns true" do expect(described_class.valid_localizable_number?(1599.99)).to eql true end + + context "with a negative number" do + it "returns true" do + expect(described_class.valid_localizable_number?(-1599.99)).to eql true + end + end end context "with letters" do