Add controller tests for switch_locale

This way it'll be easier to change it while checking we haven't broken
existing behavior.

While writing the tests, I noticed we were sometimes storing a symbol in
the session while sometimes we were storing a string. So we're adding a
`to_s` call so we always store a string in the session.
This commit is contained in:
Javi Martín
2024-05-22 00:01:18 +02:00
parent a2177b4575
commit 3e13f93ebd
6 changed files with 110 additions and 4 deletions

View File

@@ -1,6 +1,14 @@
require "rails_helper"
describe ApplicationController do
controller do
skip_authorization_check
def index
render plain: I18n.locale
end
end
describe "#current_budget" do
it "returns the last budget that is not in draft phase" do
create(:budget, :finished, created_at: 2.years.ago, name: "Old")
@@ -12,4 +20,50 @@ describe ApplicationController do
expect(budget.name).to eq("Current")
end
end
describe "#switch_locale" do
it "uses the default locale by default" do
get :index
expect(response.body).to eq "en"
end
it "uses the locale in the parameters when it's there" do
get :index, params: { locale: :es }
expect(response.body).to eq "es"
end
it "uses the locale in the session if there are no parameters" do
get :index, params: { locale: :es }
expect(response.body).to eq "es"
get :index
expect(response.body).to eq "es"
end
it "uses the locale in the parameters even when it's in the session" do
get :index
expect(response.body).to eq "en"
get :index, params: { locale: :es }
expect(response.body).to eq "es"
end
context "authenticated user" do
let(:user) { create(:user) }
before { sign_in(user) }
it "updates the prefered locale when it's in the parameters" do
get :index, params: { locale: :es }
expect(user.reload.locale).to eq "es"
expect(response.body).to eq "es"
end
end
end
end