You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.4 KiB

  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. before do
  11. routes.draw { get 'success' => 'anonymous#success' }
  12. end
  13. shared_examples 'default locale' do
  14. after { I18n.locale = I18n.default_locale }
  15. it 'sets available and preferred language' do
  16. request.headers['Accept-Language'] = 'ca-ES, fa'
  17. get 'success'
  18. expect(I18n.locale).to eq :fa
  19. end
  20. it 'sets available and compatible langauge if none of available languages are preferred' do
  21. request.headers['Accept-Language'] = 'fa-IR'
  22. get 'success'
  23. expect(I18n.locale).to eq :fa
  24. end
  25. it 'sets default locale if none of available languages are compatible' do
  26. request.headers['Accept-Language'] = ''
  27. get 'success'
  28. expect(I18n.locale).to eq :en
  29. end
  30. end
  31. context 'user with valid locale has signed in' do
  32. it "sets user's locale" do
  33. user = Fabricate(:user, locale: :ca)
  34. sign_in(user)
  35. get 'success'
  36. expect(I18n.locale).to eq :ca
  37. end
  38. end
  39. context 'user with invalid locale has signed in' do
  40. before do
  41. user = Fabricate.build(:user, locale: :invalid)
  42. user.save!(validate: false)
  43. sign_in(user)
  44. end
  45. include_examples 'default locale'
  46. end
  47. context 'user has not signed in' do
  48. include_examples 'default locale'
  49. end
  50. end