user_mailer_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 receiver.email
  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 'reset_password_instructions' do
  29. let(:mail) { UserMailer.reset_password_instructions(receiver, 'spec') }
  30. it 'renders reset password instructions' do
  31. receiver.update!(locale: nil)
  32. expect(mail.body.encoded).to include receiver.email
  33. expect(mail.body.encoded).to include 'spec'
  34. end
  35. include_examples 'localized subject',
  36. 'devise.mailer.reset_password_instructions.subject'
  37. end
  38. describe 'password_change' do
  39. let(:mail) { UserMailer.password_change(receiver) }
  40. it 'renders password change notification' do
  41. receiver.update!(locale: nil)
  42. expect(mail.body.encoded).to include receiver.email
  43. end
  44. include_examples 'localized subject',
  45. 'devise.mailer.password_change.subject'
  46. end
  47. end