summaryrefslogtreecommitdiff
path: root/spec/support/email_helpers.rb
blob: 3e979f2f470c99a2f7cc4ff90e2d40a5f585425e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module EmailHelpers
  def sent_to_user?(user, recipients = email_recipients)
    recipients.include?(user.notification_email)
  end

  def reset_delivered_emails!
    ActionMailer::Base.deliveries.clear
  end

  def should_only_email(*users, kind: :to)
    recipients = email_recipients(kind: kind)

    users.each { |user| should_email(user, recipients) }

    expect(recipients.count).to eq(users.count)
  end

  def should_email(user, recipients = email_recipients)
    expect(sent_to_user?(user, recipients)).to be_truthy
  end

  def should_not_email(user, recipients = email_recipients)
    expect(sent_to_user?(user, recipients)).to be_falsey
  end

  def should_not_email_anyone
    expect(ActionMailer::Base.deliveries).to be_empty
  end

  def email_recipients(kind: :to)
    ActionMailer::Base.deliveries.flat_map(&kind)
  end
end