Files
openfoodnetwork/spec/helpers/i18n_helper_spec.rb
Maikel Linke 63988fff4f Configure test locales like other envs
The locale config is set in application.rb from environment variables
already. We don't need to repeat that logic in test.rb. And because it
was outdated, the language switcher was actually broken in the test
environment. We did have an English selector for the fallback `en` even
though we were already displaying English as en_TST. And after
switchting to Spanish, we could switch back because en_TST was not in
the available locales.

I now fixed the test with the right assumption and the config to solve
the problem.
2026-03-18 14:49:15 +11:00

173 lines
4.7 KiB
Ruby

# frozen_string_literal: true
RSpec.describe I18nHelper do
let(:user) { create(:user) }
let(:cookies) { {} }
before do
allow(helper).to receive(:cookies) { cookies }
end
context "as guest" do
before do
allow(helper).to receive(:spree_current_user) { nil }
end
it "sets the default locale" do
helper.set_locale
expect(I18n.locale).to eq :en_TST
end
it "sets the chosen locale" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "remembers the chosen locale" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "ignores unavailable locales" do
allow(helper).to receive(:params) { { locale: "xx" } }
helper.set_locale
expect(I18n.locale).to eq :en_TST
end
it "remembers the last chosen locale" do
allow(helper).to receive(:params) { { locale: "en_TST" } }
helper.set_locale
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "remembers the chosen locale after logging in" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
# log in
allow(helper).to receive(:spree_current_user) { user }
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "forgets the chosen locale without cookies" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
# clean up cookies
cookies.delete :locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :en_TST
end
end
context "logged in" do
before do
allow(helper).to receive(:spree_current_user) { user }
end
it "sets the default locale" do
helper.set_locale
expect(I18n.locale).to eq :en_TST
end
it "sets the chosen locale" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
expect(I18n.locale).to eq :es
expect(user.locale).to eq "es"
end
it "remembers the chosen locale" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "remembers the last chosen locale" do
allow(helper).to receive(:params) { { locale: "en_TST" } }
helper.set_locale
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "remembers the chosen locale after logging out" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
# log out
allow(helper).to receive(:spree_current_user) { nil }
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
it "remembers the chosen locale on another computer" do
allow(helper).to receive(:params) { { locale: "es" } }
helper.set_locale
expect(cookies.fetch(:locale)).to eq "es"
# switch computer / browser or loose cookies
cookies.delete :locale
allow(helper).to receive(:params) { {} }
helper.set_locale
expect(I18n.locale).to eq :es
end
end
context "#valid_locale" do
around do |example|
original_default_locale = I18n.default_locale
original_available_locales = Rails.application.config.i18n.available_locales
I18n.default_locale = "es"
Rails.application.config.i18n.available_locales = ["es", "pt"]
example.run
I18n.default_locale = original_default_locale
Rails.application.config.i18n.available_locales = original_available_locales
end
let(:user) { build(:user) }
it "returns default locale if given user is nil" do
expect(helper.valid_locale(nil)).to eq I18n.default_locale
end
it "returns default locale if locale of given user is nil" do
expect(helper.valid_locale(user)).to eq I18n.default_locale
end
it "returns default locale if locale of given user is not available" do
user.locale = "cn"
expect(helper.valid_locale(user)).to eq I18n.default_locale
end
it "returns the locale of the given user if available" do
user.locale = "pt"
expect(helper.valid_locale(user)).to eq "pt"
end
end
end