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.

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