1
0

localized_spec.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Localized do
  4. controller(ApplicationController) 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 'with a 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 'with a 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 'with a user who has not signed in' do
  52. include_examples 'default locale'
  53. end
  54. end