localized_spec.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe ApplicationController, type: :controller do
  4. controller do
  5. include Localized
  6. def success
  7. head 200
  8. end
  9. end
  10. around do |example|
  11. current_locale = I18n.locale
  12. example.run
  13. I18n.locale = current_locale
  14. end
  15. before do
  16. routes.draw { get 'success' => 'anonymous#success' }
  17. end
  18. shared_examples 'default locale' do
  19. it 'sets available and preferred language' do
  20. request.headers['Accept-Language'] = 'ca-ES, fa'
  21. get 'success'
  22. expect(I18n.locale).to eq :fa
  23. end
  24. it 'sets available and compatible langauge if none of available languages are preferred' do
  25. request.headers['Accept-Language'] = 'fa-IR'
  26. get 'success'
  27. expect(I18n.locale).to eq :fa
  28. end
  29. it 'sets default locale if none of available languages are compatible' do
  30. request.headers['Accept-Language'] = ''
  31. get 'success'
  32. expect(I18n.locale).to eq :en
  33. end
  34. end
  35. context 'user with valid locale has signed in' do
  36. it "sets user's locale" do
  37. user = Fabricate(:user, locale: :ca)
  38. sign_in(user)
  39. get 'success'
  40. expect(I18n.locale).to eq :ca
  41. end
  42. end
  43. context 'user with invalid locale has signed in' do
  44. before do
  45. user = Fabricate.build(:user, locale: :invalid)
  46. user.save!(validate: false)
  47. sign_in(user)
  48. end
  49. include_examples 'default locale'
  50. end
  51. context 'user has not signed in' do
  52. include_examples 'default locale'
  53. end
  54. end