user_mailer_spec.rb 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe UserMailer, type: :mailer do
  4. let(:receiver) { Fabricate(:user) }
  5. shared_examples 'localized subject' do |*args, **kwrest|
  6. it 'renders subject localized for the locale of the receiver' do
  7. locale = I18n.available_locales.sample
  8. receiver.update!(locale: locale)
  9. expect(mail.subject).to eq I18n.t(*args, kwrest.merge(locale: locale))
  10. end
  11. it 'renders subject localized for the default locale if the locale of the receiver is unavailable' do
  12. receiver.update!(locale: nil)
  13. expect(mail.subject).to eq I18n.t(*args, kwrest.merge(locale: I18n.default_locale))
  14. end
  15. end
  16. describe 'confirmation_instructions' do
  17. let(:mail) { UserMailer.confirmation_instructions(receiver, 'spec') }
  18. it 'renders confirmation instructions' do
  19. receiver.update!(locale: nil)
  20. expect(mail.body.encoded).to include I18n.t('devise.mailer.confirmation_instructions.title')
  21. expect(mail.body.encoded).to include 'spec'
  22. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  23. end
  24. include_examples 'localized subject',
  25. 'devise.mailer.confirmation_instructions.subject',
  26. instance: Rails.configuration.x.local_domain
  27. end
  28. describe 'reconfirmation_instructions' do
  29. let(:mail) { UserMailer.confirmation_instructions(receiver, 'spec') }
  30. it 'renders reconfirmation instructions' do
  31. receiver.update!(email: 'new-email@example.com', locale: nil)
  32. expect(mail.body.encoded).to include I18n.t('devise.mailer.reconfirmation_instructions.title')
  33. expect(mail.body.encoded).to include 'spec'
  34. expect(mail.body.encoded).to include Rails.configuration.x.local_domain
  35. expect(mail.subject).to eq I18n.t('devise.mailer.reconfirmation_instructions.subject',
  36. instance: Rails.configuration.x.local_domain,
  37. locale: I18n.default_locale)
  38. end
  39. end
  40. describe 'reset_password_instructions' do
  41. let(:mail) { UserMailer.reset_password_instructions(receiver, 'spec') }
  42. it 'renders reset password instructions' do
  43. receiver.update!(locale: nil)
  44. expect(mail.body.encoded).to include I18n.t('devise.mailer.reset_password_instructions.title')
  45. expect(mail.body.encoded).to include 'spec'
  46. end
  47. include_examples 'localized subject',
  48. 'devise.mailer.reset_password_instructions.subject'
  49. end
  50. describe 'password_change' do
  51. let(:mail) { UserMailer.password_change(receiver) }
  52. it 'renders password change notification' do
  53. receiver.update!(locale: nil)
  54. expect(mail.body.encoded).to include I18n.t('devise.mailer.password_change.title')
  55. end
  56. include_examples 'localized subject',
  57. 'devise.mailer.password_change.subject'
  58. end
  59. describe 'email_changed' do
  60. let(:mail) { UserMailer.email_changed(receiver) }
  61. it 'renders email change notification' do
  62. receiver.update!(locale: nil)
  63. expect(mail.body.encoded).to include I18n.t('devise.mailer.email_changed.title')
  64. end
  65. include_examples 'localized subject',
  66. 'devise.mailer.email_changed.subject'
  67. end
  68. end