# frozen_string_literal: true
require 'rails_helper'
describe ApplicationController, type: :controller do
controller do
include Localized
def success
head 200
end
around do |example|
current_locale = I18n.locale
example.run
I18n.locale = current_locale
before do
routes.draw { get 'success' => 'anonymous#success' }
shared_examples 'default locale' do
it 'sets available and preferred language' do
request.headers['Accept-Language'] = 'ca-ES, fa'
get 'success'
expect(I18n.locale).to eq :fa
it 'sets available and compatible language if none of available languages are preferred' do
request.headers['Accept-Language'] = 'fa-IR'
it 'sets default locale if none of available languages are compatible' do
request.headers['Accept-Language'] = ''
expect(I18n.locale).to eq :en
context 'user with valid locale has signed in' do
it "sets user's locale" do
user = Fabricate(:user, locale: :ca)
sign_in(user)
expect(I18n.locale).to eq :ca
context 'user with invalid locale has signed in' do
user = Fabricate.build(:user, locale: :invalid)
user.save!(validate: false)
include_examples 'default locale'
context 'user has not signed in' do