From c2c7910357ed2ff6562cc997e6941cd50bc71d2e Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 11 Apr 2024 10:05:49 +1000 Subject: [PATCH] Reset I18n.local for each spec This avoids a locale setting leaking from one spec to another. It also means that we don't have to reset the locale in individual specs. Also: - `cookies` is reset automatically and we don't need to do that. - Removed some unused code (German number format and helper methods). --- spec/base_spec_helper.rb | 5 +++++ .../user_registrations_controller_spec.rb | 13 +++---------- spec/helpers/i18n_helper_spec.rb | 11 ----------- spec/models/spree/variant_spec.rb | 10 ---------- spec/system/admin/order_cycles/list_spec.rb | 9 --------- 5 files changed, 8 insertions(+), 40 deletions(-) diff --git a/spec/base_spec_helper.rb b/spec/base_spec_helper.rb index f835bc883b..1238281f00 100644 --- a/spec/base_spec_helper.rb +++ b/spec/base_spec_helper.rb @@ -96,6 +96,11 @@ RSpec.configure do |config| expectations.syntax = :expect end + # Reset locale for all specs. + config.around(:each) do |example| + I18n.with_locale(:en) { example.run } + end + # Reset all feature toggles to prevent leaking. config.before(:suite) do Flipper.features.each(&:remove) diff --git a/spec/controllers/user_registrations_controller_spec.rb b/spec/controllers/user_registrations_controller_spec.rb index fb948cfa4e..d25b811d69 100644 --- a/spec/controllers/user_registrations_controller_spec.rb +++ b/spec/controllers/user_registrations_controller_spec.rb @@ -48,16 +48,9 @@ describe UserRegistrationsController, type: :controller do end it "sets user.locale from cookie on create" do - original_i18n_locale = I18n.locale - original_locale_cookie = cookies[:locale] - - # changes to +I18n.locale+ will only persist within the +with_locale+ block - I18n.with_locale(original_i18n_locale) do - cookies[:locale] = "pt" - post :create, xhr: true, params: { spree_user: user_params, use_route: :spree } - expect(assigns[:user].locale).to eq("pt") - cookies[:locale] = original_locale_cookie - end + cookies[:locale] = "pt" + post :create, xhr: true, params: { spree_user: user_params, use_route: :spree } + expect(assigns[:user].locale).to eq("pt") end end end diff --git a/spec/helpers/i18n_helper_spec.rb b/spec/helpers/i18n_helper_spec.rb index 546719180e..a4117097d4 100644 --- a/spec/helpers/i18n_helper_spec.rb +++ b/spec/helpers/i18n_helper_spec.rb @@ -10,17 +10,6 @@ describe I18nHelper, type: :helper do allow(helper).to receive(:cookies) { cookies } end - # In the real world, the helper is called in every request and sets - # I18n.locale to the chosen locale or the default. For testing purposes we - # have to restore I18n.locale for unit tests that don't call the helper, but - # rely on translated strings. - around do |example| - original_locale = I18n.locale - I18n.with_locale(original_locale) do - example.run - end - end - context "as guest" do before do allow(helper).to receive(:spree_current_user) { nil } diff --git a/spec/models/spree/variant_spec.rb b/spec/models/spree/variant_spec.rb index b081c355f8..1771e860a1 100644 --- a/spec/models/spree/variant_spec.rb +++ b/spec/models/spree/variant_spec.rb @@ -59,16 +59,6 @@ describe Spree::Variant do end context "price parsing" do - around(:each) do |spec| - default_locale = I18n.default_locale - I18n.with_locale(default_locale) do - I18n.backend.store_translations(:de, - { number: { currency: { format: { delimiter: '.', - separator: ',' } } } }) - spec.run - end - end - context "price=" do context "with decimal point" do it "captures the proper amount for a formatted price" do diff --git a/spec/system/admin/order_cycles/list_spec.rb b/spec/system/admin/order_cycles/list_spec.rb index 87cef27c14..308f95a36f 100644 --- a/spec/system/admin/order_cycles/list_spec.rb +++ b/spec/system/admin/order_cycles/list_spec.rb @@ -194,15 +194,6 @@ describe ' private - def wait_for_edit_form_to_load_order_cycle(order_cycle) - expect(page).to have_field "order_cycle_name", with: order_cycle.name - end - - def select_incoming_variant(supplier, exchange_no, variant) - page.find("table.exchanges tr.supplier-#{supplier.id} td.products").click - check "order_cycle_incoming_exchange_#{exchange_no}_variants_#{variant.id}" - end - def date_warning_msg(nbr = 1) "This order cycle is linked to %d open subscription orders. Changing this date now will not " \ "affect any orders which have already been placed, but should be avoided if possible. " \