digest_mailer_worker_spec.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. describe DigestMailerWorker do
  4. describe 'perform' do
  5. let(:user) { Fabricate(:user, last_emailed_at: 3.days.ago) }
  6. context 'for a user who receives digests' do
  7. it 'sends the email' do
  8. service = double(deliver_now!: nil)
  9. allow(NotificationMailer).to receive(:digest).and_return(service)
  10. update_user_digest_setting(true)
  11. described_class.perform_async(user.id)
  12. expect(NotificationMailer).to have_received(:digest)
  13. expect(user.reload.last_emailed_at).to be_within(1).of(Time.now.utc)
  14. end
  15. end
  16. context 'for a user who does not receive digests' do
  17. it 'does not send the email' do
  18. allow(NotificationMailer).to receive(:digest)
  19. update_user_digest_setting(false)
  20. described_class.perform_async(user.id)
  21. expect(NotificationMailer).not_to have_received(:digest)
  22. expect(user.last_emailed_at).to be_within(1).of(3.days.ago)
  23. end
  24. end
  25. def update_user_digest_setting(value)
  26. user.settings['notification_emails'] = user.settings['notification_emails'].merge('digest' => value)
  27. end
  28. end
  29. end