localized_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. render plain: I18n.locale, status: 200
  8. end
  9. end
  10. before do
  11. routes.draw { get 'success' => 'anonymous#success' }
  12. end
  13. shared_examples 'default locale' do
  14. it 'sets available and preferred language' do
  15. request.headers['Accept-Language'] = 'sr-Latn'
  16. get 'success'
  17. expect(response.body).to eq 'sr-Latn'
  18. end
  19. it 'sets available and preferred language' do
  20. request.headers['Accept-Language'] = 'ca-ES, fa'
  21. get 'success'
  22. expect(response.body).to eq 'ca'
  23. end
  24. it 'sets available and compatible language if none of available languages are preferred' do
  25. request.headers['Accept-Language'] = 'fa-IR'
  26. get 'success'
  27. expect(response.body).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(response.body).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(response.body).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